/
snake.html
executable file
·158 lines (154 loc) · 4.13 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
<html>
<body>
<style>
body {
background-color: black;
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
}
button {
margin-bottom: 30px; padding: 20px; background-color: turquoise;
}
* {
margin: 0;
padding: 0;
}
canvas {
background-color: black;
border: 1px solid white;
}
</style>
<button id="begin">
BEGIN
</button>
<canvas id="game" height=500 width=500 />
<script>
const startButton = document.getElementById("begin")
const canvas = document.getElementById("game")
const c = canvas.getContext("2d")
startButton.addEventListener("click", startGame)
const DOWN = 'DOWN', UP = 'UP', LEFT = 'LEFT', RIGHT = 'RIGHT'
function startGame() {
const state = {
direction: DOWN,
previousDirection: DOWN,
apple: [100, 100],
score: 0,
sections: [
[250, 250],
[250, 240],
[250, 230],
[250, 220],
[250, 210],
[250, 200],
[250, 190],
]
}
c.lineWidth = 10
window.addEventListener("keydown", changeDirection)
const tickId = window.setInterval(tick, 60)
function clear() {
c.clearRect(0,0,500,500)
}
function drawSnake() {
c.strokeStyle = 'green'
const [ head, ...rest ] = state.sections
c.beginPath()
c.moveTo(...head)
for (section of rest) {
c.lineTo(...section)
}
c.stroke()
c.closePath()
}
function drawApple() {
c.strokeStyle = 'red'
c.beginPath()
c.moveTo(state.apple[0], state.apple[1] - 5)
c.lineTo(state.apple[0], state.apple[1] + 5)
c.stroke()
c.closePath()
}
function gameOver() {
window.clearInterval(tickId)
clear()
alert("Game Over! You scored: " + state.score)
}
function randomBoundedCoordinate() {
return 10 * Math.floor(Math.random() * 49) + 10
}
function newRandomCoords() {
let newX, newY
do {
newX = randomBoundedCoordinate()
newY = randomBoundedCoordinate()
} while (
state.sections.find(function(section) {
return section[0] === newX && section[1] === newY
}) !== undefined)
return [newX, newY]
}
function move(dx, dy) {
function ranIntoSelf() {
return state.sections.
find(function(section) {
return section[0] === newHeadX && section[1] === newHeadY
}) !== undefined
}
function ranIntoBorder() {
return newHeadX > 500 || newHeadX < 0 || newHeadY > 500 || newHeadY < 0
}
function ate() {
return newHeadX === state.apple[0] && newHeadY === state.apple[1]
}
const oldHead = state.sections[0]
const middle = state.sections.slice(1)
const newHeadX = oldHead[0] + dx
const newHeadY = oldHead[1] + dy
if (ranIntoSelf() || ranIntoBorder()) {
return gameOver()
} else if (ate()) {
// make new apple
state.apple = [ ...newRandomCoords() ]
state.score += 1
} else {
middle.splice(middle.length - 1, 1)
}
const newHead = [newHeadX, newHeadY]
state.sections = [newHead, oldHead, ...middle]
}
function tick() {
state.previousDirection = state.direction
if (state.direction === DOWN) {
move(0, 10)
} else if (state.direction === UP) {
move(0, -10)
} else if (state.direction === LEFT) {
move(-10, 0)
} else if (state.direction === RIGHT) {
move(10, 0)
} else {
debugger
alert("INVALID DIRECTION!!!")
}
clear()
drawApple()
drawSnake()
}
function changeDirection(event) {
if (event.keyCode === 40 && state.previousDirection !== UP) {
state.direction = DOWN
} else if (event.keyCode === 38 && state.previousDirection !== DOWN) {
state.direction = UP
} else if (event.keyCode === 37 && state.previousDirection !== RIGHT) {
state.direction = LEFT
} else if (event.keyCode === 39 && state.previousDirection !== LEFT) {
state.direction = RIGHT
}
}
}
</script>
</body>
</html>