-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.html
198 lines (165 loc) · 5.28 KB
/
game.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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Snake!</title>
<style>
* { padding: 0; margin: 0; }
canvas { background: #eee; display: block; margin: 0 auto; }
</style>
</head>
<body>
<canvas id="myCanvas" width="480" height="320"></canvas>
<script>
// Initiate the canvas object
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
// Configure initial values for the snake
var headX = 50;
var headY = 50;
var snakeSpeed = 3;
var angle = Math.PI;
var segCount = 1;
var segmentSize = 10;
var expansionRate = 10;
var score = 0;
ctx.font = "30px Arial";
var loss = false;
// Define the segment class used to build the snake
class Segment {
constructor(x, y, lastPosX = 0, lastPosY = 0){
this.x = x;
this.y = y;
}
lastPosX = 0;
lastPosY = 0;
}
// Main game loop
function playGame(){
// Update the board and check for loss conditions
move();
// Check if the loss conditions were met
rules();
// Clear the board to remove last frame
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Draw next frame
drawSnake();
// Display the end screen and stop the game once the loss conditions are met
if(loss){
ctx.fillText("Game Over! Score: " + score, 100, canvas.height/2);
// Clear the interval to stop the game loop from running
clearInterval(interval);
}
}
function move(){
head.x += Math.sin(angle) * snakeSpeed;
head.y -= Math.cos(angle) * snakeSpeed;
}
function rules(){
if(head.x < 0 || head.y < 0 || head.x > canvas.width || head.y > canvas.height){
loss = true;
snakeSpeed = 0;
}
for(i = 20; i < segments.length; i++){
if (head.x > segments[i].x - segmentSize && head.x < segments[i].x + segmentSize){
if (head.y > segments[i].y - segmentSize && head.y < segments[i].y + segmentSize){
console.log('intersection');
loss = true;
}
}
}
if (head.x > food.x - 2 * segmentSize && head.x < food.x + 2 * segmentSize){
if (head.y > food.y - 2 * segmentSize && head.y < food.y + 2 * segmentSize){
console.log("hit!");
food.x = Math.floor((Math.random() * canvas.width));
food.y = Math.floor((Math.random() * canvas.height));
score ++;
snakeSpeed = snakeSpeed + 0.25;
for(i = 0; i < expansionRate; i++){
expandSnake();
}
}
}
}
function expandSnake(){
segments[segments.length] = new Segment(segments[segments.length - 1].x, segments[segments.length - 1].y);
segCount++;
}
function drawSegment(seg, colour = "#0095DD") {
ctx.beginPath();
ctx.arc(seg.x, seg.y, segmentSize, 0, Math.PI*2);
//ctx.stroke();
ctx.fillStyle = colour;
ctx.fill();
ctx.closePath();
}
function drawSnake(){
drawSegment(food, "#ff0000");
for(let i = segCount; i > 0; i--){
drawSegment(segments[i-1]);
}
head.lastPosX = head.x;
head.lastPosY = head.y;
for(let i = 0; i < segCount; i++){
segments[i].lastPosX = segments[i].x;
segments[i].lastPosY = segments[i].y;
}
segments[0].x = head.lastPosX;
segments[0].y = head.lastPosY;
for(let i = 1; i < segCount; i++){
segments[i].x = segments[i-1].lastPosX;
segments[i].y = segments[i-1].lastPosY;
}
drawSegment(head, "#000000");
}
function init(){
clearInterval(interval);
loss = false;
headX = 50;
headY = 50;
snakeSpeed = 3;
angle = Math.PI;
segCount = 1;
score = 0;
head = new Segment(headX, headY);
food = new Segment(Math.floor((Math.random() * canvas.width-50)+50), Math.floor((Math.random() * canvas.height-50)+50));
segments = [];
for(let i = 0; i < segCount; i++){
segments[i] = new Segment(headX, headY);
}
}
init();
var interval = setInterval(playGame, 25);
document.addEventListener('keydown', function(event) {
switch (event.key) {
case "ArrowDown":
if(angle != Math.PI * 2){
angle = Math.PI / 1;
}
break;
case "ArrowUp":
if(angle != Math.PI / 1){
angle = Math.PI * 2;
}
break;
case "ArrowLeft":
if(angle != Math.PI / 2){
angle = Math.PI * 1.5;
}
break;
case "ArrowRight":
if(angle != Math.PI * 1.5){
angle = Math.PI / 2;
}
break;
case "r":
init();
interval = setInterval(playGame, 25);
break;
default:
return;
}
});
</script>
</body>
</html>