-
Notifications
You must be signed in to change notification settings - Fork 0
/
snake.html
217 lines (210 loc) · 7.15 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
215
216
217
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<style>
body {
display: flex;
justify-content: center;
align-items: center;
}
</style>
</head>
<body>
<canvas id="playground"></canvas>
<script>
const canvas = document.getElementById("playground")
const context = canvas.getContext("2d")
const width = canvas.width
const height = canvas.height
const blockSize = 10
const widthInBlocks = width / blockSize
const heightInBlocks = height / blockSize
let score = 0
const drawBorder = function () {
context.fillStyle = "Gray"
context.fillRect(0, 0, width, blockSize)
context.fillRect(width - blockSize, 0, blockSize, height)
context.fillRect(0, height - blockSize, width, blockSize)
context.fillRect(0, 0, blockSize, height)
}
const drawScore = function () {
context.font = "20px Arial"
context.fillStyle = "Black"
context.textAlign = "center"
context.textBaseline = "middle"
context.fillText("Score: " + score, width / 2, height / 2)
}
const clearCanvas = function () {
context.clearRect(0, 0, width, height)
}
const Block = function (col, row) {
this.col = col
this.row = row
}
Block.prototype.drawSquare = function (color) {
this.color = color
const x = this.col * blockSize
const y = this.row * blockSize
context.fillStyle = this.color
context.fillRect(x, y, blockSize, blockSize)
}
Block.prototype.drawCircle = function (color) {
this.color = color
const centerX = this.col * blockSize + blockSize / 2
const centerY = this.row * blockSize + blockSize / 2
context.beginPath()
context.arc(centerX, centerY, blockSize / 2, 0, Math.PI * 2, false)
context.closePath()
context.fillStyle = this.color
context.fill()
}
Block.prototype.equal = function (otherBlock) {
return this.col === otherBlock.col && this.row === otherBlock.row
}
const Snake = function (blocks) {
this.segments = blocks
this.direction = "right"
this.nextDirection = "right"
}
Snake.prototype.draw = function () {
for (let i = 0; i < this.segments.length; i++) {
this.segments[i].drawSquare("Green")
}
}
Snake.prototype.move = function () {
let head = this.segments[0]
let newHead
this.direction = this.nextDirection
if (this.direction === "right") {
newHead = new Block(head.col + 1, head.row)
} else if (this.direction === "down") {
newHead = new Block(head.col, head.row + 1)
} else if (this.direction === "left") {
newHead = new Block(head.col - 1, head.row)
} else if (this.direction === "up") {
newHead = new Block(head.col, head.row - 1)
}
if (this.checkCollision(newHead)) {
gameOver()
return
}
this.segments.unshift(newHead)
if (newHead.equal(apple.position)) {
score++
apple.move(this.segments)
animationTime -= 5
if (animationTime < 50) {
animationTime = 50
}
} else {
this.segments.pop()
}
}
Snake.prototype.checkCollision = function (head) {
let leftCollision = head.col === 0
let topCollision = head.row === 0
let rightCollision = head.col === widthInBlocks - 1
let bottomCollision = head.row === heightInBlocks - 1
let wallCollision = leftCollision || topCollision || rightCollision || bottomCollision
let selfCollision = false
for (let i = 0; i < this.segments.length; i++) {
if (head.equal(this.segments[i])) {
selfCollision = true
}
}
return wallCollision || selfCollision
}
// 算是一个拦截,如果新方向与原方向相反,则不改变方向。
Snake.prototype.setDirection = function (newDirection) {
if (this.direction === "up" && newDirection === "down") {
return
} else if (this.direction === "right" && newDirection === "left") {
return
} else if (this.direction === "down" && newDirection === "up") {
return
} else if (this.direction === "left" && newDirection === "right") {
return
}
this.nextDirection = newDirection // 否则把新方向赋值给nextDirection属性。
}
const Apple = function (position) {
this.position = position
}
Apple.prototype.draw = function () {
this.position.drawCircle("Red")
}
Apple.prototype.move = function (occupiedBlocks) {
const randomCol = Math.floor(Math.random() * (widthInBlocks - 2)) + 1
const randomRow = Math.floor(Math.random() * (heightInBlocks - 2)) + 1
this.position = new Block(randomCol, randomRow)
for (let i = 0; i < occupiedBlocks.length; i++) {
if (this.position.equal(occupiedBlocks[i])) {
this.move(occupiedBlocks)
return
}
}
}
const snake = new Snake([new Block(7, 5), new Block(6, 5), new Block(5, 5)])
const apple = new Apple(new Block(10, 10))
const gameOver = function () {
clearTimeout(timeoutId)
context.clearRect(0, 0, width, height)
context.font = "60px Courier"
context.fillStyle = "Black"
context.textAlign = "center"
context.textBaseline = "middle"
context.fillText("Game Over", width / 2, height / 2)
}
const gameLoop = function () {
context.clearRect(0, 0, width, height)
drawScore()
snake.move()
snake.draw()
apple.draw()
drawBorder()
timeoutId = setTimeout(gameLoop, animationTime)
}
let timeoutId = setTimeout(gameLoop, 100)
let animationTime = 100 //搞成变量是为了,加速——吃苹果可以跑更快
const directions = {
37: "left",
38: "up",
39: "right",
40: "down",
}
document.body.addEventListener(
"keydown",
function (event) {
const newDirection = directions[event.keyCode]
if (newDirection !== undefined) {
snake.setDirection(newDirection)
}
},
false
)
if (window.DeviceOrientationEvent) {
window.addEventListener(
"deviceorientation",
function (event) {
const gamma = event.gamma
const beta = event.beta
if (gamma > 5) {
snake.setDirection("right")
} else if (gamma < -5) {
snake.setDirection("left")
} else if (beta > 5) {
snake.setDirection("down")
} else if (beta < -5) {
snake.setDirection("up")
}
},
false
)
}
</script>
</body>
</html>