-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
195 lines (165 loc) · 5.09 KB
/
main.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
"use strict";
var EMPTY = 0,
SNAKE_HEAD = 1,
SNAKE_BODY = 2,
FOOD = 3,
WALL = 4;
var BLOCK_SIZE = 20;
var X = 50,
Y = 20;
var DT = 100;
var DIR_X = [0, 1, 0, -1];
var DIR_Y = [-1, 0, 1, 0];
var STYLES = {
0 : {class: "", id: ""},
1 : {class: "Block Snake", id: "SnakeHead"},
2 : {class: "Block Snake", id: ""},
3 : {class: "Block Food", id: ""}
}
var wrapper = null;
var grid = null;
// main
window.onload = function() {
// get wrapper
wrapper = document.getElementById("SnakeWrapper");
// initialize grid
(function(x, y) {
grid = [];
for (var i = 0; i < x; i++) {
grid.push([]);
for (var j = 0; j < y; j++) {
grid[i].push({type: 0, element:null});
}
}
})(X, Y);
// initialize objects
var head = {x:Math.floor(X/2), y:Math.floor(Y/2)};
var direction = 3,
new_direction = direction;
var body = [];
var snakeLength = 0;
snakeLength = 3;
addElm(head.x, head.y, SNAKE_HEAD);
addElm(6, 10, FOOD);
var keyboardHandler = function (e) {
var key = e.keyCode;
console.log(key)
switch (key) {
case 37: // Left
new_direction = 3;
break;
case 38: // Up
new_direction = 0;
break;
case 39: // Right
new_direction = 1;
break;
case 40: //Down
new_direction = 2;
break;
}
}
// Listener
window.addEventListener("keydown", keyboardHandler, false);
// main loop
var siid = setInterval(function () {
// change direction
if ((direction+2)%4 !== new_direction) {
direction = new_direction;
}
// move head
var new_x = head.x + DIR_X[direction];
var new_y = head.y + DIR_Y[direction];
if (validPosition(new_x, new_y)) {
var next_pos = getElm(new_x, new_y);
console.log("Type", next_pos.type);
if (next_pos.type === FOOD) {
snakeLength++;
// while not moving to valid position
console.log("Init eat");
while (moveElm(new_x, new_y,
Math.floor(Math.random()*X),
Math.floor(Math.random()*Y)) === null)
console.log("....");
console.log("Eat");
}
var state = moveElm(head.x, head.y, new_x, new_y);
console.log(state);
if (state === null) {
// #TODO
// end game
console.log("Invalid state");
console.log("head", head.x, head.y);
console.log("New", new_x, new_y);
console.log(body.length);
clearInterval(siid);
}
// move body
if (snakeLength > body.length) {
// extend body
addElm(head.x, head.y, SNAKE_BODY);
body.push({x:head.x, y:head.y});
} else {
// move tail to front
var last = body.shift();
moveElm(last.x, last.y, head.x, head.y);
body.push({x:head.x, y:head.y});
}
head.x = new_x;
head.y = new_y;
} else {
console.log("Invalid posiiton");
clearInterval(siid);
}
}, DT);
}
var validPosition = function (x, y) {
if (0 <= x && x < X && 0 <= y && y < Y) {
return true;
} else {
return false;
}
}
var getElm = function (x, y) {
return grid[x][y];
}
var addElm = function (x, y, elm_type) {
if (getElm(x, y).type || !validPosition(x, y))
return null;
var elm = document.createElement('div');
elm.setAttribute('class', STYLES[elm_type]['class']);
elm.setAttribute('id', STYLES[elm_type]['id']);
elm.setAttribute('style', 'left:'+BLOCK_SIZE*x+'px; '
+'top:' +BLOCK_SIZE*y+'px;');
grid[x][y].type = elm_type;
grid[x][y].element = wrapper.appendChild(elm);
return true;
}
var deleteElm = function (x, y) {
var element = getElm(x, y);
if (element.type && validPosition(x, y)) {
wrapper.removeChild(element.element);
grid[x][y] = {type: 0, element: null};
return true;
} else {
return null;
}
}
var moveElm = function (x, y, new_x, new_y) {
var element = getElm(x, y);
if (validPosition(x, y) && validPosition(new_x, new_y) &&
element.type !== 0 && getElm(new_x, new_y).type === 0) {
// remove old
wrapper.removeChild(element.element);
grid[x][y] = {type: 0, element: null};
// add new
var elm = element.element;
elm.setAttribute('style', 'left:'+BLOCK_SIZE*new_x+'px; '
+'top:'+BLOCK_SIZE*new_y+'px;');
grid[new_x][new_y].type = element.type;
grid[new_x][new_y].element = wrapper.appendChild(elm);
return true;
} else {
return null;
}
}