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1,1,1,1,1,1],
- [1,1,1,1,1,1,0,1,1,0,0,0,0,0,0,0,0,0,0,1,1,0,1,1,1,1,1,1],
- [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,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]
- ],
- _GOODS = { //能量豆
- '1,3':1,
- '26,3':1,
- '1,23':1,
- '26,23':1
- },
- _COS = [1,0,-1,0],
- _SIN = [0,1,0,-1],
- _COLOR = ['#F00','#F93','#0CF','#F9C'],//红,橙,
- _LIFE = 3,
- _SCORE = 0; //得分
-
- var game = new Game('canvas');
- //启动页
- (function(){
- var stage = game.createStage();
- //logo
- stage.createItem({
- x:game.width/2,
- y:game.height*.45,
- width:100,
- height:100,
- frames:3,
- draw:function(context){
- var t = Math.abs(5-this.times%10);
- context.fillStyle = '#FFE600';
- context.beginPath();
- context.arc(this.x,this.y,this.width/2,t*.04*Math.PI,(2-t*.04)*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);
- }
- });
- //事件绑定
- stage.bind('keydown',function(e){
- switch(e.keyCode){
- case 13:
- case 32:
- game.nextStage();
- break;
- }
- });
- })();
- //游戏主程序
- (function(){
- var stage,map,beans,player,times;
- stage = game.createStage({
- update:function(){
- var stage = this;
- if(stage.status==1){ //场景正常运行
- items.forEach(function(item){
- if(map&&!map.get(item.coord.x,item.coord.y)&&!map.get(player.coord.x,player.coord.y)){
- var dx = item.x-player.x;
- var dy = item.y-player.y;