Template:Tsinghua-A/JS/solid game/pagemain

init(50, "mylegend", 1000, 600, main); // ,LEvent.INIT //声明变量 //进度条显示层,背景层,方块绘制层,方块预览层 var loadingLayer,backLayer,graphicsMap,nextLayer; var imglist = {}; var nowTime; var rrok = 0; var rjok = 0; var last_A = 0; var last_B = 0; var A =0; var A1 =0; var A2 =0; var A3 =0; var B =0; var B1 =0; var B2 =0; var B3 =0; var choose_num = 8; //初始化8个 var nowchoose = 0;//A=0 B=1 var start = 0; var back_volume = 0.15; var people_volume = 0.15;


var imgDatapage = new Array(

                           {name:"skip-2",path:"TsinghuaA-GameImg-skip-2.png"},
                           {name:"xijun1",path:"TsinghuaA-GameImg-xijun1.png"},
                           {name:"xijun2",path:"TsinghuaA-GameImg-xijun2.png"},
                           {name:"xijun3",path:"TsinghuaA-GameImg-xijun3.png"},
                           {name:"xijun4",path:"TsinghuaA-GameImg-xijun4.png"},
                           {name:"xijun5",path:"TsinghuaA-GameImg-xijun5.png"},
                           {name:"xijun6",path:"TsinghuaA-GameImg-xijun6.png"},
                           {name:"xijun7",path:"TsinghuaA-GameImg-xijun7.png"},
                           {name:"xijun8",path:"TsinghuaA-GameImg-xijun8.png"},

{name:"xijun9",path:"TsinghuaA-GameImg-xijun9.png"},

                           {name:"ks-2",path:"TsinghuaA-GameImg-ks-2.png"},
                           {name:"restart-2",path:"TsinghuaA-GameImg-restart-2.png"},
                           {name:"clock",path:"TsinghuaA-GameImg-clock.png"},
                           {name:"stop",path:"TsinghuaA-GameImg-stop.png"},
                           {name:"setting",path:"TsinghuaA-GameImg-setting.png"},
                           {name:"Awin",path:"TsinghuaA-GameImg-Awin.png"},
                           {name:"Bwin",path:"TsinghuaA-GameImg-Bwin.png"},
                           {name:"pingju",path:"TsinghuaA-GameImg-pingju.png"},
                           {name:"renji",path:"TsinghuaA-GameImg-renji.png"},
                           {name:"renren",path:"TsinghuaA-GameImg-renren.png"},

{name:"tuihui",path:"TsinghuaA-GameImg-tuihui.png"}, {name:"back",path:"TsinghuaA-back.png"}, {name:"WB",path:"TsinghuaA-WB.png"}, {name:"WR",path:"TsinghuaA-WR.png"}, {name:"RB",path:"TsinghuaA-RB.png"}, {name:"BB",path:"TsinghuaA-BB.png"}, {name:"RF",path:"TsinghuaA-RF.png"},

                        	{name:"BF",path:"TsinghuaA-BF.png"},

{name:"repeat",path:"TsinghuaA-repeat.png"}, {name:"back_2",path:"TsinghuaA-back_2.png"}, {name:"question",path:"TsinghuaA-question.png"}, {name:"finger",path:"TsinghuaA-finger.png"}, {name:"option",path:"TsinghuaA-option.png"}, {name:"help",path:"TsinghuaA-help.png"}, {name:"option_slide",path:"TsinghuaA-GameImg-option_slide.png"}, {name:"option_apply",path:"TsinghuaA-GameImg-option_apply.png"}, {name:"option_cancel",path:"TsinghuaA-GameImg-option_cancel.png"}

                           );


var backlayer; var radius = 100; var imglist = {}; var start_x=290; var start_y=150; //记录细菌存活情况矩阵 var cells = []; var ahlA = []; var ahlB = []; var nutrition = [];

//记录每种细菌存活位置 var cells_farmerA = []; var cells_farmerB = []; var cells_soldierA = []; var cells_soldierB = []; //var cells_plan_farmer = []; //var cells_plan_soldier = [];

var benefit_record = []; //Record the state of benefit matrix after each click. First element benefit_record[0] is benefit of farmer, second element benefit_record[1] is benefit of soldier.

var maplist =[]; var LBitmapDatalist = []; var scale = 4; //图片大小 var midradius = Math.round((radius-1)/2); var days = 0; var NEGinfinity = -9999999;


function main(){

   //背景层初始化
   backLayer = new LSprite();
   
   //在背景层上绘制蓝色背景
   backLayer.graphics.drawRect(1,"#0072E3",[0,0,1000,667],true,"#0072E3");
   
   //背景显示
   addChild(backLayer);
   
   //进度条读取层初始化
   loadingLayer = new LoadingSample1();
   
   //进度条读取层显示
   backLayer.addChild(loadingLayer);

//读取声音 var url = "./music_back."; sound_init.load(url+"mp3,"+url+"ogg,"+url+"wav"); var url2 = "./music_fight."; sound_fight.load(url+"mp3,"+url+"ogg,"+url+"wav"); var url3 = "./framer."; sound_framer.load(url+"mp3,"+url+"ogg,"+url+"wav"); var url4 = "./soldier."; sound_soldier.load(url+"mp3,"+url+"ogg,"+url+"wav"); var url5 = "./bagger."; sound_bagger.load(url+"mp3,"+url+"ogg,"+url+"wav");

   //利用LLoadManage类,读取所有图片,并显示进度条进程
   LLoadManage.load(
                    imgDatapage,
                    function(progress){
                    loadingLayer.setProgress(progress);
                    },
                    gamepage
                    );

}

function stop_sound(){

}

//读取完所有图片,进行游戏标题画面的初始化工作 function gamepage(result){

   //取得图片读取结果
   imglist = result;
   
   //移除进度条层
   backLayer.removeChild(loadingLayer);
   loadingLayer = null;
   
   //显示游戏标题
   var title = new LTextField();
   title.x = 170;
   title.y = 190;
   title.size = 40;
   title.color = "#ffffff";
   title.text = "Game for Solid Medium";
   title.weight = "bolder";
   title.textBaseline = "alphabetic";
   title.font = "Georgia";
   title.style = "italic";
   backLayer.addChild(title);
   
   //显示说明文
   backLayer.graphics.drawRect(1,"#ffffff",[390,393,220,40]);
   var txtClick = new LTextField();
   txtClick.size = 20;
   txtClick.color = "#ffffff";
   txtClick.text = "CLICK TO BIGIN";
   txtClick.x = (LGlobal.width - txtClick.getWidth())/2;
   txtClick.y = 400;
   backLayer.addChild(txtClick);
   
   //添加点击事件,点击画面则游戏开始
   backLayer.addEventListener(LMouseEvent.MOUSE_UP,gameToStart);

}

var sound_init = new LSound(); var sound_fight = new LSound(); var sound_framer = new LSound(); var sound_soldier = new LSound(); var sound_bagger = new LSound(); var sound_framer1 = new LSound(); var sound_soldier1 = new LSound(); var sound_bagger1 = new LSound();



function play_sound_init(){ var url = "https://static.igem.org/mediawiki/2017/a/a4/TsinghuaA-GameMusic-music_back.mp3"; sound_init.load(url); sound_init.setVolume(back_volume ); sound_init.play(); }

function play_sound_fight(){ var url = "https://static.igem.org/mediawiki/2017/c/cb/TsinghuaA-GameMusic-music_fight.mp3"; sound_fight.load(url); sound_fight.setVolume(back_volume ); sound_fight.play(); }

function play_sound_framer(){ var url = "https://static.igem.org/mediawiki/2017/c/ca/TsinghuaA-GameMusic-framer.mp3"; sound_framer.load(url); sound_framer.setVolume(people_volume); sound_framer.play(); }

function play_sound_soldier(){ var url = "https://static.igem.org/mediawiki/2017/5/5d/TsinghuaA-GameMusic-soldier.mp3"; sound_soldier.load(url); sound_soldier.setVolume(people_volume); sound_soldier.play(); }

function play_sound_bagger(){ var url = "https://static.igem.org/mediawiki/2017/a/ae/TsinghuaA-GameMusic-bagger.mp3"; sound_bagger.load(url); sound_bagger.setVolume(people_volume); sound_bagger.play(); }

function play_sound_framer1(){ var url = "https://static.igem.org/mediawiki/2017/9/9d/TsinghuaA-GameMusic-framer1.mp3"; sound_framer1.load(url); sound_framer1.setVolume(people_volume); sound_framer1.play(); }

function play_sound_soldier1(){ var url = "https://static.igem.org/mediawiki/2017/a/aa/TsinghuaA-GameMusic-soldier1.mp3"; sound_soldier1.load(url); sound_soldier1.setVolume(people_volume); sound_soldier1.play(); }

function play_sound_bagger1(){ var url = "https://static.igem.org/mediawiki/2017/0/0d/TsinghuaA-GameMusic-bagger1.mp3"; sound_bagger1.load(url); sound_bagger1.setVolume(people_volume); sound_bagger1.play(); }


//游戏画面初始化 function gameToStart(){

   //背景层清空
   backLayer.die();
   backLayer.removeAllChild();
   
   //背景图片显示
   var bitmap = new LBitmap(new LBitmapData(imglist["back"]));
   backLayer.addChild(bitmap);
   
   var title = new LTextField();
   title.x = 170;
   title.y = 190;
   title.size = 40;
   title.color = "#000000";
   title.text = "Game for Solid Medium";
   title.weight = "bolder";
   title.textBaseline = "alphabetic";
   title.font = "Georgia";
   title.style = "italic";
   backLayer.addChild(title);
   
   var title = new LTextField();
   title.x = 75;
   title.y = 280;
   title.size = 22;
   title.color = "#000000";
   title.text = "Welcome to the world of bacteria. In this world, you have three types"
   title.textBaseline = "alphabetic";
   title.font = "Georgia";
   backLayer.addChild(title);
   
   var title = new LTextField();
   title.x = 75;
   title.y = 330;
   title.size = 22;
   title.color = "#000000";
   title.text = "of bacteria chess pieces, the farmer, the warrior and the beggar."
   title.textBaseline = "alphabetic";
   title.font = "Georgia";
   backLayer.addChild(title);
   
   var title = new LTextField();
   title.x = 75;
   title.y = 380;
   title.size = 22;
   title.color = "#000000";
   title.text = "The initialization needs strategy of deciding where to settle them."
   title.textBaseline = "alphabetic";
   title.font = "Georgia";
   backLayer.addChild(title);
   
   var title = new LTextField();
   title.x = 75;
   title.y = 430;
   title.size = 22;
   title.color = "#000000";
   title.text = "Now if you want to play with AI, please click the button 'SKIP'."
   title.textBaseline = "alphabetic";
   title.font = "Georgia";
   backLayer.addChild(title);
   
   
   var bingoButton;
   var btnUp, btnOver;
   
   btnUp = new LBitmap(new LBitmapData(imglist["skip-2"]));
   btnOver = new LBitmap(new LBitmapData(imglist["skip-2"]));
   bingoButton = new LButton(btnUp,btnOver);
   bingoButton.x = 800;
   bingoButton.y = 510;
   backLayer.addChild(bingoButton);
   bingoButton.addEventListener(LMouseEvent.MOUSE_UP,startsolid);

//back_music var url1 = "https://static.igem.org/mediawiki/2017/a/a4/TsinghuaA-GameMusic-music_back.mp3"; sound_init.load(url1); sound_init.setVolume( back_volume); var url2 = "https://static.igem.org/mediawiki/2017/c/cb/TsinghuaA-GameMusic-music_fight.mp3"; sound_fight.load(url2); sound_fight.setVolume( back_volume); //sound.addEventListener(LEvent.COMPLETE,loadOver);

}


function onframe_music(){ if(!sound_init.playing && start == 0 ) { sound_fight.stop();

   play_sound_init();
   }

if(!sound_fight.playing && start == 1 ) {

   sound_init.stop();

play_sound_fight();

   }


   if( (sound_init.getCurrentTime() >>>0) == 55  && start == 0)

{ play_sound_init(); }

   if( (sound_fight.getCurrentTime() >>>0) == 55 && start == 1 )

{ play_sound_fight(); }

}

function startsolid(){


   //背景层清空
   backLayer.die();
   backLayer.removeAllChild();
   //背景初始化
   backlayer = new LSprite();
   addChild( backlayer );

back_pic = new LBitmap(new LBitmapData(imglist["back"])); back_pic.x = 0; back_pic.y = 0; back_pic.scaleY = 1.2; backlayer.addChild( back_pic );

//music backLayer.addEventListener(LEvent.ENTER_FRAME, onframe_music);

   //初始化培养基
   peiyangji = new LSprite();

peiyangji.addEventListener(LMouseEvent.MOUSE_UP, csh );

   //peiyangji.graphics.drawRect(0,'#9F35FF',[0,0,radius*scale,radius*scale],true,'#000000');
   peiyangji.graphics.drawArc(15,'#ffffff',[radius*scale/2+1,radius*scale/2+1,radius*scale/2-10,0,360*Math.PI/180],false,'#000000');
   peiyangji.graphics.drawArc(5,'#000000',[radius*scale/2+1,radius*scale/2+1,radius*scale/2-6,0,360*Math.PI/180],false,'#000000');


peiyangji.x = start_x;

   peiyangji.y = start_y;
   backlayer.addChild( peiyangji );

backlayer.graphics.drawRect(10,'#000000',[0,0,LGlobal.width,LGlobal.height],true,'#000000');

   //初始化矩阵
   var i,j;
   for( i = -2 ; i < radius + 3 ; i++ ){
       var child1 = [];
       var child2 = [];
       var child3 = [];
       var child4 = [];
       for(j = -2 ; j < radius + 3 ; j++ ){
           var x = i - midradius;
           var y = j - midradius;
           if(( x*x + y*y ) <= (midradius-3)*(midradius-3))
           {
               child1[j] = 0;
               child2[j] = 0;
               child3[j] = 0;
               child4[j] = 0;

}

           else {
               child1[j] = 4; //超出圆的部分cells定义为4
               child2[j] = 0;
               child3[j] = 0;
               child4[j] = 0;
           }
       }
       cells[i] = child1;
       ahlA[i] = child2;
       ahlB[i] = child3;
       nutrition[i] = child4;
   }
   
   
   
   
   loadinglayer = new LoadingSample3();
   //进度条读取层显示
   backlayer.addChild(loadinglayer);
   //利用LLoadManage类,读取所有图片,并显示进度条进程
   LLoadManage.load(
                    imgDatapage,
                    function(progress){
                    loadinglayer.setProgress(progress);
                    },
                    gameinit
                    );
   

}


function gameinit(result){

   imglist = result;
   LBitmapDatalist = [
                      new LBitmapData(imglist["xijun1"]),
                      new LBitmapData(imglist["xijun2"]),
                      new LBitmapData(imglist["xijun3"]),
                      new LBitmapData(imglist["xijun4"]),
                      new LBitmapData(imglist["xijun5"]),
                      new LBitmapData(imglist["xijun6"]),
                      new LBitmapData(imglist["xijun7"]),
                      new LBitmapData(imglist["xijun8"]),

new LBitmapData(imglist["xijun9"]),

                      new LBitmapData(imglist["ks-2"]),
                      new LBitmapData(imglist["restart-2"]),
                      new LBitmapData(imglist["skip-2"]),
                      new LBitmapData(imglist["clock"]),
                      new LBitmapData(imglist["stop"]),
                      new LBitmapData(imglist["setting"]),
                      new LBitmapData(imglist["ks"]),
                      new LBitmapData(imglist["restart"]),
                      new LBitmapData(imglist["restart"]),
                      new LBitmapData(imglist["Awin"]),
                      new LBitmapData(imglist["Bwin"]),
                      new LBitmapData(imglist["pingju"]),
                      new LBitmapData(imglist["renji"]),
                      new LBitmapData(imglist["renren"]),
                      new LBitmapData(imglist["tuihui"]),

new LBitmapData(imglist["back"]), new LBitmapData(imglist["WB"]), new LBitmapData(imglist["WR"]), new LBitmapData(imglist["BB"]), new LBitmapData(imglist["RB"]), new LBitmapData(imglist["BF"]), new LBitmapData(imglist["RF"]), new LBitmapData(imglist["repeat"]), new LBitmapData(imglist["back_2"]), new LBitmapData(imglist["question"]), new LBitmapData(imglist["finger"]), new LBitmapData(imglist["option"]), new LBitmapData(imglist["help"]), new LBitmapData(imglist["option_slide"]), new LBitmapData(imglist["option_apply"]), new LBitmapData(imglist["option_cancel"])

                      ];
   backlayer.removeChild(loadinglayer);
   loadinglayer = null;
   
   getmap();
   
   //刷新屏幕
   drawnow();
   
   //初始化操作
   chushihua();
   
   

};


//循环播放 function onframe(){ if(start) {

   hexin();
   days++;
   drawnow();

count_num(); new_text();

   endgame();
   }

};

function new_text(){

   selfTextAll.text = "天数: " + Math.round(days/20);

A_count.text = "total :"+A; B_count.text = "total :"+B; A1_count.text = " "+A1; A2_count.text = " "+A2; A3_count.text = " "+A3; B1_count.text = " "+B1; B2_count.text = " "+B2; B3_count.text = " "+B3; }

function drawnow(){

   var i,j;
   for(i=0;i<radius;i++){
       for(j=0;j<radius;j++){
           if( cells[i][j]==0 && nutrition[i][j] == 0 ){maplist[i][j]["bitmap"].bitmapData = LBitmapDatalist[8]; }
           else if( cells[i][j] == 1){maplist[i][j]["bitmap"].bitmapData = LBitmapDatalist[0] ; }
           else if( cells[i][j] == 2){maplist[i][j]["bitmap"].bitmapData = LBitmapDatalist[1] ; }
           else if( cells[i][j] == 3){maplist[i][j]["bitmap"].bitmapData = LBitmapDatalist[2] ; }
           else if( cells[i][j] == -1){maplist[i][j]["bitmap"].bitmapData = LBitmapDatalist[4] ; }
           else if( cells[i][j] == -2){maplist[i][j]["bitmap"].bitmapData = LBitmapDatalist[5] ; }
           else if( cells[i][j] == -3){maplist[i][j]["bitmap"].bitmapData = LBitmapDatalist[3] ; }

else if( cells[i][j] == 0 && nutrition[i][j] != 0 ){maplist[i][j]["bitmap"].bitmapData = LBitmapDatalist[6] ; }

           else if( cells[i][j] == 4 ){ continue;}
       }
   }

};


//初始化画布 function getmap(){

   var i,j,xijun,nArr;
   for( i = 0 ; i <radius; i++ ){
       nArr = [];
       for(j = 0; j < radius; j++)
       {
           xijun = new LBitmap(new LBitmapData(imglist["xijun1"]));
           xijun.scaleX = scale;
           xijun.scaleY = scale;
           xijun.x = i*scale;
           xijun.y = j*scale;
           if( cells[i][j] != 4 ){
               peiyangji.addChild( xijun );
               nArr[j] = {"value":0,"bitmap":xijun};
           }
           else{
               nArr[j] = {"value":0};
           }
       }
       maplist[i] = nArr;
   }

}


function endgame(){ //游戏结束

   //peiyangji.removeEventListener(LEvent.ENTER_FRAME, onframe);
   var i,j; //A,B记录总数
   if(  (days == 1200) || (days > 20 && ( A== 0 || B==0)) || ten_flag == 20 ) //第60天
   {
       peiyangji.removeEventListener(LEvent.ENTER_FRAME, onframe);
       
       if( ( A - B ) > A/2 )
       {
           Aw = getwin("Awin");
           Aw.x = midradius;
           Aw.y = midradius;

Aw.scaleX = 0.2; Aw.scaleY = 0.2;

           Aw.addEventListener(LMouseEvent.MOUSE_UP,win);
           peiyangji.addChild(Aw);
           
       }
       
       else if( ( B - A) > B/2 )
       {
           Bw = getwin("Bwin");
           Bw.x = midradius;
           Bw.y = midradius;

Bw.scaleX = 0.2; Bw.scaleY = 0.2;

           Bw.addEventListener(LMouseEvent.MOUSE_UP,win);
           peiyangji.addChild(Bw);
       }
       
       else
       {
           pj = getwin("pingju");
           pj.x = midradius;
           pj.y = midradius;

pj.scaleX = 0.2; pj.scaleY = 0.2;

           pj.addEventListener(LMouseEvent.MOUSE_UP,win);
           peiyangji.addChild(pj);
           
       }
   }

}

function count_num(){ var A1_temp = 0; var A2_temp = 0; var A3_temp = 0; var B1_temp = 0; var B2_temp = 0; var B3_temp = 0; var A_temp = 0; var B_temp = 0;

   for( i = 0 ; i < radius ; i++ )
       for( j = 0 ; j < radius ; j++ ){
           if( cells[i][j] == 1 )

A1_temp++;

           else if( cells[i][j] == 2 )

A2_temp++;

           else if( cells[i][j] == 3 )

A3_temp++;

           else if( cells[i][j] == -1 )

B1_temp++;

           else if( cells[i][j] == -2 )

B2_temp++;

           else if( cells[i][j] == -3)

B3_temp++; else ;

       } 

A_temp = A1_temp + A2_temp + A3_temp; B_temp = B1_temp + B2_temp + B3_temp;

last_A = A; last_B = B; A = A_temp; B = B_temp; A1 = A1_temp; A2 = A2_temp; A3 = A3_temp; B1 = B1_temp; B2 = B2_temp; B3 = B3_temp;

if( (days > 400) && (last_A == A) && (last_B == B) ) {ten_flag++;} }

var ten_flag = 0; //5次出现数量不变直接进入评比


function getwin(value){ //win的按钮

   var btnUp = new LBitmap(new LBitmapData(imglist[value]));
   btnUp.scaleX = 1;
   btnUp.scaleY = 1;
   var btnOver = new LBitmap(new LBitmapData(imglist[value]));
   btnOver.scaleX = 1;
   btnOver.scaleY = 1;
   btnOver.x = 1;
   btnOver.y = 1;
   var btn = new LButton(btnUp,btnOver);
   btn.name = value;
   return btn;

}

function win(event,object){ //点击win的事件,关闭按钮,重新开始游戏

   if( object.name == "Awin")
   {   
       peiyangji.removeChild( Aw );
       restart1();
   }
   if(object.name == "Bwin")
   {   
       peiyangji.removeChild( Bw );
       restart1();
   }
   if(object.name == "pingju")
   {   
       peiyangji.removeChild( pj );
       restart1();
   }
   

}

function restart1(){ //点击重新启动游戏 init = 1; start = 0;

   //游戏暂停
   peiyangji.removeEventListener(LEvent.ENTER_FRAME, onframe);

fingerlayer.removeEventListener( LEvent.ENTER_FRAME,onframe1); backlayer.removeChild(fingerlayer); backlayer.removeChild(inittime);

for( key in peiyangji.childList ) { if(peiyangji.childList[key].name == "Awin") peiyangji.removeChild( Aw ); else if(peiyangji.childList[key].name == "Bwin") peiyangji.removeChild( Bw ); else if(peiyangji.childList[key].name == "pingju") peiyangji.removeChild( pj ); }

   //初始化天数

nowchoose = 0;//A=0 B=1 lastchoose = 1; choose_num = choose_num1; ten_flag = 0;

   days = 0;

A =0; A1 =0; A2 =0; A3 =0; last_A = 0; last_B = 0; B =0; B1 =0; B2 =0; B3 =0;

   new_text();
   //初始化矩阵
   var i,j;
   for( i = 0 ; i < radius ; i++ ){
       for(j = 0 ; j < radius ; j++ ){
           if( cells[i][j] != 4 ) //非培养基外
           {
               cells[i][j] = 0;
               ahlA[i][j] = 0;
               ahlB[i][j] = 0;
               nutrition[i][j] = 0;
           }
           else{
               ahlA[i][j] = 0;
               ahlB[i][j] = 0;
               nutrition[i][j] = 0;
           }
       }
       
   }
   drawnow();


backlayer.addChild(inittime); fingerlayer.addEventListener( LEvent.ENTER_FRAME,onframe1); backlayer.addChild(fingerlayer); }