-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.html
233 lines (225 loc) · 7.57 KB
/
index.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
<!DOCTYPE html>
<html>
<head>
<title>Snake Game</title>
<link
rel="icon"
type="image/ico"
href="favicon.ico"
sizes="16x16"
/>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<marquee direction="right" scrollamount="25"
><h1><strong>🐍Snake Game🐍</strong></h1></marquee
>
<canvas id="canvas" width="1050" height="450"></canvas>
<script>
var game = {
tickNumber: 0,
timer: null,
score: 0,
highscore: 0,
board: [
"###################################",
"# # #",
"# #",
"# # #",
"# # #",
"# # ## #",
"# ## #",
"# # #",
"# #",
"# # # #",
"# # #",
"# # #",
"# # #",
"###################################"
],
fruit: [],
//fruit: [
// {x: 9, y: 5}
//],
tick: function() {
window.clearTimeout(game.timer);
game.tickNumber++;
if (game.tickNumber % 50 == 0) {
game.addRandomFruit();
}
var result = snake.move();
if (result == "gameover") {
alert("Game over! Your score is: " + game.score + "!");
if (game.score > game.highscore) {
game.highscore = game.score;
alert(
"New high score! Your local high score is: " +
game.highscore +
"!"
);
}
return;
}
graphics.drawGame();
game.timer = window.setTimeout("game.tick()", 200); //this is how it loops
},
addRandomFruit: function() {
var randomY = Math.floor(Math.random() * game.board.length) + 0;
var randomX =
Math.floor(Math.random() * game.board[randomY].length) + 0;
var randomLocation = { x: randomX, y: randomY };
if (game.isEmpty(randomLocation) && !game.isFruit(randomLocation)) {
game.fruit.push(randomLocation);
}
},
isEmpty: function(location) {
return game.board[location.y][location.x] == " ";
},
isWall: function(location) {
return game.board[location.y][location.x] == "#";
},
isFruit: function(location) {
for (
var fruitNumber = 0;
fruitNumber < game.fruit.length;
fruitNumber++
) {
var fruit = game.fruit[fruitNumber];
if (location.x == fruit.x && location.y == fruit.y) {
game.fruit.splice(fruitNumber, 1);
return true;
}
}
return false;
},
isSnake: function(location) {
for (var snakePart = 0; snakePart < snake.parts.length; snakePart++) {
var part = snake.parts[snakePart];
if (location.x == part.x && location.y == part.y) {
return true;
}
}
return false;
}
};
var snake = {
parts: [{ x: 4, y: 2 }, { x: 3, y: 2 }, { x: 2, y: 2 }],
facing: "E",
nextLocation: function() {
var snakeHead = snake.parts[0];
var targetX = snakeHead.x;
var targetY = snakeHead.y;
targetY = snake.facing == "N" ? targetY - 1 : targetY;
targetY = snake.facing == "S" ? targetY + 1 : targetY;
targetX = snake.facing == "W" ? targetX - 1 : targetX;
targetX = snake.facing == "E" ? targetX + 1 : targetX;
return { x: targetX, y: targetY };
},
move: function() {
var location = snake.nextLocation();
if (game.isWall(location) || game.isSnake(location)) {
return "gameover";
}
if (game.isEmpty(location)) {
snake.parts.unshift(location);
snake.parts.pop();
}
if (game.isFruit(location)) {
snake.parts.unshift(location);
game.score++;
game.addRandomFruit();
}
}
};
var graphics = {
canvas: document.getElementById("canvas"),
squareSize: 30,
drawBoard: function(ctx) {
var currentYoffset = 0;
game.board.forEach(function checkLine(line) {
line = line.split("");
var currentXoffset = 0;
line.forEach(function checkCharacter(character) {
if (character == "#") {
ctx.fillStyle = "black";
ctx.fillRect(
currentXoffset,
currentYoffset,
graphics.squareSize,
graphics.squareSize
);
}
currentXoffset += graphics.squareSize;
});
currentYoffset += graphics.squareSize;
});
},
draw: function(ctx, source, color) {
source.forEach(function(part) {
var partXlocation = part.x * graphics.squareSize;
var partYlocation = part.y * graphics.squareSize;
ctx.fillStyle = color;
ctx.fillRect(
partXlocation,
partYlocation,
graphics.squareSize,
graphics.squareSize
);
});
},
drawGame: function() {
var ctx = graphics.canvas.getContext("2d");
ctx.clearRect(0, 0, graphics.canvas.width, graphics.canvas.height);
graphics.drawBoard(ctx);
graphics.draw(ctx, game.fruit, "coral");
graphics.draw(ctx, snake.parts, "blue");
}
};
var gameControl = {
processInput: function(keyPressed) {
var key = keyPressed.key.toLowerCase();
var targetDirection = snake.facing;
if (key == "w" && snake.facing != "S") targetDirection = "N";
if (key == "a" && snake.facing != "E") targetDirection = "W";
if (key == "s" && snake.facing != "N") targetDirection = "S";
if (key == "d" && snake.facing != "W") targetDirection = "E";
snake.facing = targetDirection;
game.tick();
},
startGame: function() {
window.addEventListener("keypress", gameControl.processInput, false);
game.tick();
}
};
function playStart() {
game.tickNumber = 0;
game.timer = null;
game.score = 0;
snake.parts.length = 0;
snake.parts = [{ x: 4, y: 2 }, { x: 3, y: 2 }, { x: 2, y: 2 }];
snake.facing = "E";
game.fruit = [];
game.addRandomFruit();
gameControl.startGame();
}
text("Score:" + game.highscore + "!");
</script>
<form>
<input type="button" value="Play" onclick="playStart()" />
</form>
<p>
Press play to start!<br />
The goal of this game is to eat as many fruits (the red blocks) to make
your snake grow long!<br />
But don't run into the walls or into yourself or you will loose. Keep
playing until the game ends, your score will be displayed at the end in a
popup.<br />
Press play to play again!<br />
<u>Controls:</u><br />
W: Move Up<br />
A: Move Left<br />
S: Move Down<br />
D: Move Right<br />
</p>
</body>
</html>