-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnake.html
214 lines (198 loc) · 8.28 KB
/
snake.html
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
<html>
<meta charset="UTF-8">
<head>
<title>Snake the game</title>
<style type="text/css">
#snake{position:absolute;left:0;top:0;height:20px;}
#wall{position:absolute;left:0;top:0;height:20px;}
</style>
<script>
var boardSize = 20;
var pieceSize = 20;
var stop = false; // debug flag
var maxFood = 1;
function SnakeBody() { // same class for snake head & snake body.
this.x = -1;
this.y = -1;
};
SnakeBody.prototype.setPos = function(newX, newY) {
this.x = newX;
this.y = newY;
};
SnakeBody.prototype.move = function(dir) {
switch (dir) {
case 'l':
this.x -= 1;
break;
case 'r':
this.x += 1;
break;
case 'u':
this.y -= 1;
break;
case 'd':
this.y += 1;
break;
}
};
function Board() {
this.board = new Array(boardSize);
for (var i = 0; i < boardSize; i++) {
this.board[i] = new Array(boardSize);
for (var j = 0; j < boardSize; j++) {
this.board[i][j] = ' ';
}
}
for (var i = 0; i < boardSize; i++) {
this.board[0][i] = 'w';
this.board[boardSize - 1][i] = 'w';
this.board[i][0] = 'w';
this.board[i][boardSize - 1] = 'w'
}
//this.board[15][10] = 'f';
this.foodCnt = 0;
this.snakeDir = 'r';
this.snake = new Array();
this.snakeLength = 1;
this.snake[0] = new SnakeBody(); // 0 is always the head
this.snake[0].setPos((boardSize/2)>>0, (boardSize/2)>>0); // start from the center
//this.snake[0].setPos(0,0);
this.addSnakeToBoard();
this.drawAll();
};
Board.prototype.addSnakeToBoard = function() {
// first clear the board;
for (var i = 0; i < boardSize; i++) {
for (var j = 0; j < boardSize; j++) {
if (this.board[i][j] == 's') {
this.board[i][j] = ' ';
}
}
}
for (var i = this.snakeLength - 1; i >= 0; i--) {
var xCord = this.snake[i].x;
var yCord = this.snake[i].y;
var cellStt = this.board[xCord][yCord];
if (xCord < 0 || yCord < 0) {
continue;
}
if (cellStt == ' ') {
this.board[xCord][yCord] = 's';
}else{
this.board[xCord][yCord] = ' ';
// clear out the cell because if it colide and die, colide() will not return
// otherwise it colide with food and food is consumed.
this.colide(cellStt);
return;
}
}
if (this.foodCnt < maxFood) {
var foodX, foodY, foodCell;
do {
foodX = Math.floor(Math.random()*boardSize);
foodY = Math.floor(Math.random()*boardSize);
foodCell = this.board[foodX][foodY];
} while (foodCell != ' ');
this.board[foodX][foodY] = 'f';
this.foodCnt++;
}
};
Board.prototype.snakeMove = function() {
var oldX = this.snake[0].x;
var oldY = this.snake[0].y;
this.snake[0].move(this.snakeDir);
for (var i = 1; i < this.snakeLength; i++){
var tempX = this.snake[i].x;
var tempY = this.snake[i].y;
this.snake[i].setPos(oldX, oldY);
oldX = tempX;
oldY = tempY;
}
};
Board.prototype.step = function() {
if (stop == false) {
this.snakeMove();
this.addSnakeToBoard();
this.drawAll();
}
};
Board.prototype.colide = function(arg) {
switch(arg) {
case 's':
case 'w':
//window.location = "gameover.html";
stop = true;
break;
case 'f':
this.snake[this.snakeLength] = new SnakeBody();
this.snakeLength++;
//this.addSnakeToBoard();
this.foodCnt--;
break;
}
}
Board.prototype.drawAll = function() {
clearGameArea();
for (var i = 0; i < boardSize; i++) {
for (var j = 0; j < boardSize; j++) {
if (this.board[i][j] == 'w'){
var line = '<div style="position: absolute; left:'+(i*pieceSize)+'px; top:'+(j*pieceSize)+'px;">';
line += '<img src="wall.jpg" /></div>';
document.getElementById('game-area').innerHTML += line;
}
if (this.board[i][j] == 's'){
var line = '<div style="position: absolute; left:'+(i*pieceSize)+'px; top:'+(j*pieceSize)+'px;">';
line += '<img src="snake.jpg" /></div>';
document.getElementById('game-area').innerHTML += line;
}
if (this.board[i][j] == 'f'){
var line = '<div style="position: absolute; left:'+(i*pieceSize)+'px; top:'+(j*pieceSize)+'px;">';
line += '<img src="food.jpg" /></div>';
document.getElementById('game-area').innerHTML += line;
}
}
}
};
function clearGameArea() {
document.getElementById('game-area').innerHTML = '';
};
function dbPrint(arg) {
document.getElementById('debug').innerHTML += '<p>' + arg + '</p>';
}
</script>
</head>
<body>
<div id="game-area" tabindex="0">
</div>
<script type="text/javascript">
var gameBoard = new Board();
setInterval('gameBoard.step()', 10);
window.addEventListener('keydown', function(event) {
switch (event.keyCode) {
case 37: // Left
if (gameBoard.snakeDir != 'r'){
gameBoard.snakeDir = 'l';
}
break;
case 38: // Up
if (gameBoard.snakeDir != 'd'){
gameBoard.snakeDir = 'u';
}
break;
case 39: // Right
if (gameBoard.snakeDir != 'l'){
gameBoard.snakeDir = 'r';
}
break;
case 40: // Down
if(gameBoard.snakeDir != 'u'){
gameBoard.snakeDir = 'd';
}
break;
}
}, false);
</script>
<div id="debug">
</div>
</body>
</html>