加入吃豆及记分

This commit is contained in:
郑浩乐 2016-01-11 21:39:55 +08:00
parent 7bce202206
commit 7be106ad7e
2 changed files with 62 additions and 20 deletions

View File

@ -95,6 +95,12 @@ function Game(id,params){
} }
return -1; return -1;
}; };
//设置地图上某点的值
Map.prototype.set = function(x,y,value){
if(this.data[y]){
this.data[y][x] = value;
}
};
//地图坐标转画布坐标 //地图坐标转画布坐标
Map.prototype.coord2position = function(cx,cy){ Map.prototype.coord2position = function(cx,cy){
return { return {

View File

@ -1,6 +1,6 @@
//主程序,业务逻辑 //主程序,业务逻辑
(function(){ (function(){
_DATA = [ //地图数据 var _DATA = [ //地图数据
[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,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,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,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],
@ -11,15 +11,15 @@
[1,0,1,1,1,1,0,1,1,0,1,1,1,1,1,1,1,1,0,1,1,0,1,1,1,1,0,1], [1,0,1,1,1,1,0,1,1,0,1,1,1,1,1,1,1,1,0,1,1,0,1,1,1,1,0,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,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,1,1,1,1,1,0,1,1,1,1,1,0,1,1,0,1,1,1,1,1,0,1,1,1,1,1,1], [1,1,1,1,1,1,0,1,1,1,1,1,0,1,1,0,1,1,1,1,1,0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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,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,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],
@ -36,7 +36,7 @@
_COS = [1,0,-1,0], _COS = [1,0,-1,0],
_SIN = [0,1,0,-1], _SIN = [0,1,0,-1],
_COLOR = ['#F00','#F60','#3C6','#69F'], _COLOR = ['#F00','#F60','#3C6','#69F'],
_SIZE = 20; //地图大小 _SCORE = 0; //得分
var game = new Game('canvas'); var game = new Game('canvas');
//启动页 //启动页
@ -127,15 +127,12 @@
var map = stage.createMap({ var map = stage.createMap({
x:50, x:50,
y:10, y:10,
data:_DATA, data:JSON.parse(JSON.stringify(_DATA)),
draw:function(context){ 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 j=0; j<this.y_length; j++){
for(var i=0; i<this.x_length; i++){ for(var i=0; i<this.x_length; i++){
context.lineWidth = 2; var value = this.get(i,j);
context.strokeStyle="#09C"; if(value){
if(this.get(i,j)){
var code = 0; 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(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){ if(j){
@ -143,12 +140,12 @@
} }
} }
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(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){ if(i<this.x_length-1){
code += 100; 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(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){ if(j<this.y_length-1){
code += 10; code += 10;
} }
} }
@ -158,6 +155,8 @@
} }
} }
if(code){ if(code){
context.lineWidth = 2;
context.strokeStyle=value==2?"#FFF":"#09C";
var pos = this.coord2position(i,j); var pos = this.coord2position(i,j);
switch(code){ switch(code){
case 1100: case 1100:
@ -221,6 +220,39 @@
} }
} }
}); });
var goods = stage.createMap({
x:50,
y:10,
data:JSON.parse(JSON.stringify(_DATA)),
draw:function(context){
for(var j=0; j<this.y_length; j++){
for(var i=0; i<this.x_length; i++){
if(!this.get(i,j)){
var pos = this.coord2position(i,j);
context.fillStyle = "#FFF";
context.fillRect(pos.x-2,pos.y-2,4,4);
}
}
}
}
});
//得分
stage.createItem({
x:680,
y:84,
draw:function(context){
context.font = 'bold 28px Helvetica';
context.textAlign = 'left';
context.textBaseline = 'bottom';
context.fillStyle = '#C33';
context.fillText('SCORE',this.x,this.y);
context.font = '28px Helvetica';
context.textAlign = 'left';
context.textBaseline = 'top';
context.fillStyle = '#FFF';
context.fillText(_SCORE,this.x+12,this.y);
}
});
//主角 //主角
var player = stage.createItem({ var player = stage.createItem({
width:30, width:30,
@ -249,6 +281,10 @@
this.y -= map.size*(map.y_length-1)*_SIN[this.orientation]; this.y -= map.size*(map.y_length-1)*_SIN[this.orientation];
} }
}else{ }else{
if(!goods.get(this.coord.x,this.coord.y)){
_SCORE++;
goods.set(this.coord.x,this.coord.y,1);
}
this.x += this.speed*_COS[this.orientation]; this.x += this.speed*_COS[this.orientation];
this.y += this.speed*_SIN[this.orientation]; this.y += this.speed*_SIN[this.orientation];
} }
@ -289,7 +325,7 @@
update:function(){ update:function(){
if(!this.coord.offset){ if(!this.coord.offset){
if(!this.timeout){ if(!this.timeout){
var new_map = JSON.parse(JSON.stringify(map.data)); var new_map = JSON.parse(JSON.stringify(map.data).replace(/2/g,0));
var items = stage.getItemsByType(2); var items = stage.getItemsByType(2);
var index = this.index; var index = this.index;
items.forEach(function(item){ items.forEach(function(item){