-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
executable file
·130 lines (116 loc) · 3.13 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
const canvas = document.querySelector('canvas')
const score = document.querySelector('#score')
const btnTop = document.querySelector("#btnTop")
const btnLeft = document.querySelector("#btnLeft")
const btnBottom = document.querySelector("#btnBottom")
const btnRight = document.querySelector("#btnRight")
const ctx = canvas.getContext('2d')
const CANVAS_WIDTH = canvas.width = 600
const CANVAS_HEIGHT = canvas.height = 600
const eachBox = 20
const interval = 175
const snakeColor = '#0048EA'
const appleColor = 'red'
let defaultScore = 0
let snake = [{ x: 200, y: 200}]
let apple = { x: 4, y: 4 }
let direction = 'right'
function draw() {
ctx.clearRect(0, 0, CANVAS_WIDTH, CANVAS_HEIGHT)
ctx.fillStyle = snakeColor
snake.forEach(part => {
ctx.fillRect(part.x, part.y, eachBox, eachBox)
})
ctx.fillStyle = appleColor
ctx.fillRect(apple.x, apple.y, eachBox, eachBox)
}
function snakeMove() {
const head = { x: snake[0].x, y: snake[0].y }
switch (direction) {
case 'up':
head.y -= eachBox
break
case 'down':
head.y += eachBox
break
case 'left':
head.x -= eachBox
break
case 'right':
head.x += eachBox
break
}
snake.unshift(head)
if (head.x === apple.x && head.y === apple.y) {
createApple()
defaultScore += 1
score.innerHTML = defaultScore
} else {
snake.pop()
}
}
function createApple() {
apple.x = Math.floor(Math.random() * (canvas.width / eachBox)) * eachBox;
apple.y = Math.floor(Math.random() * (canvas.height / eachBox)) * eachBox;
snake.forEach(part => {
if (apple.x === part.x && apple.y === part.y) {
createApple()
}
})
}
function isColliding() {
const head = snake[0]
if (head.x <= (0 - (eachBox * 2)) || head.x >= (CANVAS_WIDTH + (eachBox * 2))) {
gameOver()
} else if (head.y <= (0 - (eachBox * 2)) || head.y >= (CANVAS_HEIGHT + (eachBox * 2))) {
gameOver()
}
snake.slice(1).forEach(snakePart => {
if (head.x === snakePart.x && head.y === snakePart.y) {
gameOver()
}
})
}
createApple()
function gameOver() {
clearInterval(gameLoop);
alert('Game Over!');
location.reload();
}
const gameLoop = setInterval(() => {
draw()
snakeMove()
isColliding()
}, interval)
document.addEventListener("keydown", (event) => {
let keyPressed = event.key
if ((keyPressed == 'w' || keyPressed === 'ArrowUp') && direction !== 'down') {
direction = 'up'
} else if ((keyPressed == 'a' || keyPressed === 'ArrowLeft') && direction !== 'right') {
direction = 'left'
} else if ((keyPressed == 's' || keyPressed === 'ArrowDown') && direction !== 'up') {
direction = 'down'
} else if ((keyPressed == 'd' || keyPressed === 'ArrowRight') && direction !== 'left') {
direction = 'right'
}
})
btnTop.addEventListener("click", function(){
if (direction !== 'down') {
direction = 'up'
}
})
btnLeft.addEventListener("click", function(){
if (direction !== 'left') {
direction = 'right'
}
})
btnBottom.addEventListener("click", function(){
if (direction !== 'up') {
direction = 'down'
}
})
btnRight.addEventListener("click", function(){
if (direction !== 'right') {
direction = 'left'
}
})