-
Notifications
You must be signed in to change notification settings - Fork 1
/
snake.js
178 lines (169 loc) · 4.2 KB
/
snake.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
(function(){
function Coordinate(x,y){
this.x=x;
this.y=y;
};
function Snake(options){
var snake=this;
this.maxX=options.maxX;
this.maxY=options.maxY;
this.orientation=options.initOrientation;
this.length=options.length;
this.queue=[];
window.onkeyup = function(e){
snake.onWindowKeyUp(e);
};
this.init(options);
setInterval(function(){
snake.crawl();
drawBoard(renderer,board);
},100);
}
Snake.prototype={
orientations:{
up:'u',
down:'d',
left:'l',
right:'r'
},
position:[],
orientation:'u',
maxX:0,
maxY:0,
init:function(options){
var i,x=options.startX,y=options.startY;
this.position=[new Coordinate(x,y)];
for(i=1;i<this.length;i++){
switch (this.orientation){
case this.orientations.down:
y=(y-1)%this.maxY;
if(y<0){y+=this.maxY};
break;
case this.orientations.up:
y=(y+1)%this.maxY;
break;
case this.orientations.left:
x=(x+1)%this.maxX;
break;
case this.orientations.right:
x=(x-1)%this.maxX;
if(x<0){x+=this.maxX};
break;
}
this.position.push(new Coordinate(x,y));
}
},
crawl:function(){
if(this.isDead()){
return;
}
this.processQueue();
var i,x=this.position[0].x,y=this.position[0].y;
this.position.pop();
switch (this.orientation){
case this.orientations.up:
y=(y-1)%this.maxY;
if(y<0){y+=this.maxY};
break;
case this.orientations.down:
y=(y+1)%this.maxY;
break;
case this.orientations.right:
x=(x+1)%this.maxX;
break;
case this.orientations.left:
x=(x-1)%this.maxX;
if(x<0){x+=this.maxX};
break;
}
this.position.unshift(new Coordinate(x,y));
},
processQueue:function(){
var success=false;
while(!success && this.queue.length>0){
success=this.setOrientation(this.queue.shift());
}
},
isDead:function(){
var i,
headX=this.position[0].x,
headY=this.position[0].y;
for(i=1;i<this.length;i++){
if(this.position[i].x==headX && this.position[i].y==headY){
return true;
}
}
return false;
},
setOrientation:function(orientation){
var lr=[this.orientations.left,this.orientations.right],du=[this.orientations.up,this.orientations.down];
if(lr.indexOf(orientation)>=0 && lr.indexOf(this.orientation)===-1){
this.orientation=orientation;
return true;
}else if(du.indexOf(orientation)>=0 && du.indexOf(this.orientation)===-1){
this.orientation=orientation;
return true;
}
return false;
},
onWindowKeyUp:function(e){
var key = e.keyCode ? e.keyCode : e.which;
switch(key){
case 37:
this.queue.push(this.orientations.left);
break;
case 38:
this.queue.push(this.orientations.up);
break;
case 39:
this.queue.push(this.orientations.right);
break;
case 40:
this.queue.push(this.orientations.down);
break;
}
}
}
function Arena(h,w){
this.height=h;
this.width=w;
}
function Board(arena,snake){
this.arena=arena;
this.snake=snake;
}
function drawBoard(renderer,board){
renderer.clear();
var cellHeight=renderer.getMaxY()/board.arena.height;
var cellWidth=renderer.getMaxX()/board.arena.width;
var i;
var drawOutline=false;
if(drawOutline){
var boardrect=renderer.drawRect(0,0,renderer.getMaxX(),renderer.getMaxY(),{color:'red',width:'5'});
for(i=0;i<=board.arena.height;i++){
var line=renderer.drawLine(0,cellHeight*i,renderer.getMaxX(),cellHeight*i,{color:'white'});
console.log(line);
}
for(i=0;i<=board.arena.width+1;i++){
var line=renderer.drawLine(cellWidth*i,0,cellWidth*i,renderer.getMaxY(),{color:'white'});
console.log(line);
}
}
var position=board.snake.position;
var padding=2;
for(i=position.length-1;i>=0;i--){
var coord=position[i];
var color='#55c';
if(i==0){
color='#5c5';
}else if(i==position.length-1){
color='#c55';
}
renderer.drawRect(coord.x*cellWidth+padding,coord.y*cellHeight+padding,cellWidth-2*padding,cellHeight-2*padding,{color:color,width:'3'});
}
}
var renderer=new nativeCanvasRenderer('container');
var arena=new Arena(20,20);
var snake=new Snake({startX:4,startY:4,length:10,initOrientation:'u',maxX:arena.width,maxY:arena.height});
var board=new Board(arena,snake);
})();