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0,0,0,0,0,0,0,1,1,0,1,0,0,0,0,0], [0,0,0,0,0,1,0,1,1,0,1,1,1,1,1,1,1,1,0,1,1,0,1,0,0,0,0,0], [1,1,1,1,1,1,0,1,1,0,1,1,1,1,1,1,1,1,0,1,1,0,1,1,1,1,1,1], [1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,1], [1,0,1,1,1,1,0,1,1,1,1,1,0,1,1,0,1,1,1,1,1,0,1,1,1,1,0,1], [1,0,1,1,1,1,0,1,1,1,1,1,0,1,1,0,1,1,1,1,1,0,1,1,1,1,0,1], [1,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,1], [1,1,1,0,1,1,0,1,1,0,1,1,1,1,1,1,1,1,0,1,1,0,1,1,0,1,1,1], [1,1,1,0,1,1,0,1,1,0,1,1,1,1,1,1,1,1,0,1,1,0,1,1,0,1,1,1], [1,0,0,0,0,0,0,1,1,0,0,0,0,1,1,0,0,0,0,1,1,0,0,0,0,0,0,1], [1,0,1,1,1,1,1,1,1,1,1,1,0,1,1,0,1,1,1,1,1,1,1,1,1,1,0,1], [1,0,1,1,1,1,1,1,1,1,1,1,0,1,1,0,1,1,1,1,1,1,1,1,1,1,0,1], [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1], [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1] ], _COS = [1,0,-1,0], _SIN = [0,1,0,-1], _COLOR = ['#F00','#F60','#3C6','#69F'], _SIZE = 20; //地图大小 var game = new Game('canvas'); //启动页 (function(){ var stage = game.createStage(); stage.bind('keydown',function(e){ switch(e.keyCode){ case 13: case 32: game.nextStage(); break; } }); //logo stage.createItem({ x:game.width/2, y:game.height*.45, width:100, height:100, frames:10, draw:function(context){ context.fillStyle = '#FC3'; context.beginPath(); if(this.times%2){ context.arc(this.x,this.y,this.width/2,.20*Math.PI,1.80*Math.PI,false); }else{ context.arc(this.x,this.y,this.width/2,.01*Math.PI,1.99*Math.PI,false); } context.lineTo(this.x,this.y); context.closePath(); context.fill(); context.fillStyle = '#000'; context.beginPath(); context.arc(this.x+5,this.y-27,7,0,2*Math.PI,false); context.closePath(); context.fill(); } }); //游戏名 stage.createItem({ x:game.width/2, y:game.height*.6, draw:function(context){ context.font = 'bold 42px Helvetica'; context.textAlign = 'center'; context.textBaseline = 'middle'; context.fillStyle = '#FFF'; context.fillText('Pac-Man',this.x,this.y); } }); //版权信息 stage.createItem({ x:game.width-12, y:game.height-5, draw:function(context){ context.font = '14px Helvetica'; context.textAlign = 'right'; context.textBaseline = 'bottom'; context.fillStyle = '#AAA'; context.fillText('© passer-by.com',this.x,this.y); } }); })(); //游戏主程序 (function(){ var stage = game.createStage({ update:function(){ var stage = this; if(stage.status==1){ var player = stage.getItemsByType(1)[0]; var items = stage.getItemsByType(2); items.forEach(function(item){ var dx = item.x-player.x; var dy = item.y-player.y; if(dx*dx+dy*dy<750){ stage.status = 2; stage.timeout = 30; player.status = 3; } }); }else if(stage.status=3){ if(!stage.timeout){ stage.reset(); } } } }); //绘制地图 var map = stage.createMap({ x:50, y:10, data:_DATA, draw:function(context){ var y_length = this.data.length; var x_length = this.data[0].length; for(var j=0; jthis.coord.x){ this.orientation = 0; }else if(this.vector.xthis.coord.y){ this.orientation = 1; }else if(this.vector.y