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= ['#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(){
|
|
if(this.map){
|
|
var stage = this;
|
|
var items = this.getItemsByType(1,2);
|
|
var hash = {}; //当前对象位置分布
|
|
items.forEach(function(item){
|
|
var key = 'x'+item.coord.x+'y'+item.coord.y; //坐标的标识
|
|
if(hash[key]){
|
|
if(hash[key]!=item.type){ //如果NPC与玩家相遇
|
|
stage.status = 2;
|
|
}
|
|
}else{
|
|
hash[key] = item.type;
|
|
}
|
|
});
|
|
}
|
|
}
|
|
});
|
|
//绘制地图
|
|
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; j<this.y_length; j++){
|
|
for(var i=0; i<this.x_length; i++){
|
|
context.lineWidth = 2;
|
|
context.strokeStyle="#09C";
|
|
if(this.get(i,j)){
|
|
var code = 0;
|
|
if(this.get(i,j-1)&&!(this.get(i-1,j-1)&&this.get(i+1,j-1)&&this.get(i-1,j)&&this.get(i+1,j))){
|
|
if(j){
|
|
code += 1000;
|
|
}
|
|
}
|
|
if(this.get(i+1,j)&&!(this.get(i+1,j-1)&&this.get(i+1,j+1)&&this.get(i,j-1)&&this.get(i,j+1))){
|
|
if(i<x_length-1){
|
|
code += 100;
|
|
}
|
|
}
|
|
if(this.get(i,j+1)&&!(this.get(i-1,j+1)&&this.get(i+1,j+1)&&this.get(i-1,j)&&this.get(i+1,j))){
|
|
if(j<y_length-1){
|
|
code += 10;
|
|
}
|
|
}
|
|
if(this.get(i-1,j)&&!(this.get(i-1,j-1)&&this.get(i-1,j+1)&&this.get(i,j-1)&&this.get(i,j+1))){
|
|
if(i){
|
|
code += 1;
|
|
}
|
|
}
|
|
if(code){
|
|
var pos = this.coord2position(i,j);
|
|
switch(code){
|
|
case 1100:
|
|
context.beginPath();
|
|
context.arc(pos.x+this.size/2,pos.y-this.size/2,this.size/2,.5*Math.PI,1*Math.PI,false);
|
|
context.stroke();
|
|
context.closePath();
|
|
break;
|
|
case 110:
|
|
context.beginPath();
|
|
context.arc(pos.x+this.size/2,pos.y+this.size/2,this.size/2,Math.PI,1.5*Math.PI,false);
|
|
context.stroke();
|
|
context.closePath();
|
|
break;
|
|
case 11:
|
|
context.beginPath();
|
|
context.arc(pos.x-this.size/2,pos.y+this.size/2,this.size/2,1.5*Math.PI,2*Math.PI,false);
|
|
context.stroke();
|
|
context.closePath();
|
|
break;
|
|
case 1001:
|
|
context.beginPath();
|
|
context.arc(pos.x-this.size/2,pos.y-this.size/2,this.size/2,0,.5*Math.PI,false);
|
|
context.stroke();
|
|
context.closePath();
|
|
break;
|
|
default:
|
|
var arr = String.prototype.split.call(code,'');
|
|
if(+arr.pop()){
|
|
context.beginPath();
|
|
context.moveTo(pos.x,pos.y);
|
|
context.lineTo(pos.x-this.size/2,pos.y);
|
|
context.stroke();
|
|
context.closePath();
|
|
}
|
|
if(+arr.pop()){
|
|
context.beginPath();
|
|
context.moveTo(pos.x,pos.y);
|
|
context.lineTo(pos.x,pos.y+this.size/2);
|
|
context.stroke();
|
|
context.closePath();
|
|
}
|
|
if(+arr.pop()){
|
|
context.beginPath();
|
|
context.moveTo(pos.x,pos.y);
|
|
context.lineTo(pos.x+this.size/2,pos.y);
|
|
context.stroke();
|
|
context.closePath();
|
|
}
|
|
if(+arr.pop()){
|
|
context.beginPath();
|
|
context.moveTo(pos.x,pos.y);
|
|
context.lineTo(pos.x,pos.y-this.size/2);
|
|
context.stroke();
|
|
context.closePath();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
});
|
|
//主角
|
|
var pos = map.coord2position(14,23);
|
|
var player = stage.createItem({
|
|
x:pos.x-_SIZE/2,
|
|
y:pos.y,
|
|
width:30,
|
|
height:30,
|
|
type:1,
|
|
orientation:2,
|
|
speed:2,
|
|
frames:10,
|
|
update:function(){
|
|
var coord = this.coord;
|
|
if(!coord.offset){
|
|
if(typeof this.control.orientation!='undefined'){
|
|
if(!map.get(coord.x+_COS[this.control.orientation],coord.y+_SIN[this.control.orientation])){
|
|
this.orientation = this.control.orientation;
|
|
}
|
|
}
|
|
this.control = {};
|
|
var value = map.get(coord.x+_COS[this.orientation],coord.y+_SIN[this.orientation]);
|
|
if(value==0){
|
|
this.x += this.speed*_COS[this.orientation];
|
|
this.y += this.speed*_SIN[this.orientation];
|
|
}else if(value<0){
|
|
this.x -= map.size*(map.x_length-1)*_COS[this.orientation];
|
|
this.y -= map.size*(map.y_length-1)*_SIN[this.orientation];
|
|
}
|
|
}else{
|
|
this.x += this.speed*_COS[this.orientation];
|
|
this.y += this.speed*_SIN[this.orientation];
|
|
}
|
|
},
|
|
draw:function(context){
|
|
context.fillStyle = '#FC3';
|
|
context.beginPath();
|
|
if(this.times%2){
|
|
context.arc(this.x,this.y,this.width/2,(.5*this.orientation+.20)*Math.PI,(.5*this.orientation-.20)*Math.PI,false);
|
|
}else{
|
|
context.arc(this.x,this.y,this.width/2,(.5*this.orientation+.01)*Math.PI,(.5*this.orientation-.01)*Math.PI,false);
|
|
}
|
|
context.lineTo(this.x,this.y);
|
|
context.closePath();
|
|
context.fill();
|
|
}
|
|
});
|
|
//NPC
|
|
for(var i=0;i<4;i++){
|
|
var pos = map.coord2position(12+i,14);
|
|
stage.createItem({
|
|
x:pos.x,
|
|
y:pos.y,
|
|
width:30,
|
|
height:30,
|
|
color:_COLOR[i],
|
|
type:2,
|
|
frames:10,
|
|
speed:1,
|
|
update:function(){
|
|
if(!this.coord.offset){
|
|
var new_map = JSON.parse(JSON.stringify(map.data));
|
|
var items = stage.getItemsByType(2);
|
|
var index = this.index;
|
|
items.forEach(function(item){
|
|
if(item.index!=index){
|
|
new_map[item.coord.y][item.coord.x]=1;
|
|
}
|
|
});
|
|
this.path = map.finder({
|
|
map:new_map,
|
|
start:this.coord,
|
|
end:player.coord
|
|
});
|
|
if(this.path.length){
|
|
this.vector = this.path[0];
|
|
}
|
|
if(this.vector.change){ //是否转变方向
|
|
this.coord.x = this.vector.x;
|
|
this.coord.y = this.vector.y;
|
|
var pos = map.coord2position(this.coord.x,this.coord.y);
|
|
this.x = pos.x;
|
|
this.y = pos.y;
|
|
}
|
|
if(this.vector.x>this.coord.x){
|
|
this.orientation = 0;
|
|
}else if(this.vector.x<this.coord.x){
|
|
this.orientation = 2;
|
|
}else if(this.vector.y>this.coord.y){
|
|
this.orientation = 1;
|
|
}else if(this.vector.y<this.coord.y){
|
|
this.orientation = 3;
|
|
}
|
|
}
|
|
this.x += this.speed*_COS[this.orientation];
|
|
this.y += this.speed*_SIN[this.orientation];
|
|
},
|
|
draw:function(context){
|
|
context.fillStyle = this.color;
|
|
context.beginPath();
|
|
context.arc(this.x,this.y,this.width*.5,0,Math.PI,true);
|
|
switch(this.times%2){
|
|
case 0:
|
|
context.lineTo(this.x-this.width*.5,this.y+this.height*.4);
|
|
context.quadraticCurveTo(this.x-this.width*.4,this.y+this.height*.5,this.x-this.width*.2,this.y+this.height*.3);
|
|
context.quadraticCurveTo(this.x,this.y+this.height*.5,this.x+this.width*.2,this.y+this.height*.3);
|
|
context.quadraticCurveTo(this.x+this.width*.4,this.y+this.height*.5,this.x+this.width*.5,this.y+this.height*.4);
|
|
break;
|
|
case 1:
|
|
context.lineTo(this.x-this.width*.5,this.y+this.height*.3);
|
|
context.quadraticCurveTo(this.x-this.width*.25,this.y+this.height*.5,this.x,this.y+this.height*.3);
|
|
context.quadraticCurveTo(this.x+this.width*.25,this.y+this.height*.5,this.x+this.width*.5,this.y+this.height*.3);
|
|
break;
|
|
}
|
|
context.fill();
|
|
context.closePath();
|
|
context.fillStyle = '#FFF';
|
|
context.beginPath();
|
|
context.arc(this.x-this.width*.15,this.y-this.height*.21,this.width*.12,0,2*Math.PI,false);
|
|
context.arc(this.x+this.width*.15,this.y-this.height*.21,this.width*.12,0,2*Math.PI,false);
|
|
context.fill();
|
|
context.closePath();
|
|
context.fillStyle = '#000';
|
|
context.beginPath();
|
|
context.arc(this.x-this.width*(.15-.04*_COS[this.orientation]),this.y-this.height*(.21-.04*_SIN[this.orientation]),this.width*.07,0,2*Math.PI,false);
|
|
context.arc(this.x+this.width*(.15+.04*_COS[this.orientation]),this.y-this.height*(.21-.04*_SIN[this.orientation]),this.width*.07,0,2*Math.PI,false);
|
|
context.fill();
|
|
context.closePath();
|
|
}
|
|
});
|
|
}
|
|
//布景事件绑定
|
|
stage.bind('keydown',function(e){
|
|
switch(e.keyCode){
|
|
case 13: //回车
|
|
case 32: //空格
|
|
this.status = this.status==2?1:2;
|
|
break;
|
|
case 39: //右
|
|
player.control = {orientation:0};
|
|
break;
|
|
case 40: //下
|
|
player.control = {orientation:1};
|
|
break;
|
|
case 37: //左
|
|
player.control = {orientation:2};
|
|
break;
|
|
case 38: //上
|
|
player.control = {orientation:3};
|
|
break;
|
|
}
|
|
});
|
|
})();
|
|
game.init();
|
|
game.nextStage(); //*测试*游戏主布景,完成后需关闭
|
|
})(); |