-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnake.js
133 lines (118 loc) · 3.65 KB
/
snake.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
131
132
133
class Snake{
constructor(x, y, size){
this.x = x
this.y = y
this.size = size
this.tail = [{x:this.x, y:this.y}]
this.rotateX = x
this.rotateY = y
}
move(){
var newRect;
if (this.rotateX == 1) {
newRect = {
x: this.tail[this.tail.length - 1].x + this.size,
y: this.tail[this.tail.length - 1].y
}
} else if (this.rotateX == -1) {
newRect = {
x: this.tail[this.tail.length - 1].x - this.size,
y: this.tail[this.tail.length - 1].y
}
} else if (this.rotateY == 1) {
newRect = {
x: this.tail[this.tail.length - 1].x,
y: this.tail[this.tail.length - 1].y + this.size
}
} else if (this.rotateY == -1) {
newRect = {
x: this.tail[this.tail.length - 1].x,
y: this.tail[this.tail.length - 1].y - this.size
}
}
this.tail.shift()
this.tail.push(newRect)
}
}
class Apple{
constructor(){
var isTouching;
while (true) {
isTouching = false;
this.x = Math.floor(Math.random() * canvas.width / snake.size) * snake.size
this.y = Math.floor(Math.random() * canvas.height / snake.size) * snake.size
for (var i = 0; i < snake.tail.length; i++) {
if (this.x == snake.tail[i].x && this.y == snake.tail[i].y) {
isTouching = true;
}
}
if (!isTouching) {
break;
}
this.color = "pink"
this.size = snake.size
console.log(this.x, this.y)
}
}
}
var canvas = document.getElementById("canvas")
var snake = new Snake(20,20,20);
var apple = new Apple();
var canvasContext = canvas.getContext('2d');
window.onload = ()=>{
gameLoop();
}
function gameLoop(){
setInterval(show, 1000/15)
}
function show(){
update();
draw();
}
function update(){
canvasContext.clearRect(0,0, canvas.width, canvas.height)
console.log("update")
snake.move();
eatApple()
}
function eatApple(){
if (snake.tail[snake.tail.length - 1].x == apple.x &&
snake.tail[snake.tail.length - 1].y == apple.y) {
snake.tail[snake.tail[i].length] = {x:apple.x, y: apple.y}
apple = new Apple();
}
}
function draw(){
createRect(0,0,canvas.width, canvas.height, "black")
createRect(0,0,canvas.width, canvas.height)
for (var i = 0; i < snake.tail.length; i++) {
createRect(snake.tail[i].x + 2.5, snake.tail[i].y + 2.5,
snake.size - 5, snake.size - 5, 'white')
}
canvasContext.font = "20px Arial"
canvasContext.fillStyle = "#00FF42"
canvasContext.fillText("Score: ", (snake.tail.length + 1),
canvas.width - 120, 18);
createRect(apple.x, apple.y, apple.size, apple.size, apple.color)
}
function createRect(x,y,width,height, color){
canvasContext.fillStyle = color
canvasContext.fillRect(x,y,width,height)
}
window.addEventListener("keydown", (event)=>{
setTimeout(()=>{
if (event.keyCode == 37 && snake.rotateX != 1) {
snake.rotateX = -1
snake.rotateY = 0;
} else if (event.keyCode == 38 && snake.rotateY != 1) {
snake.rotateX = 0
snake.rotateY = -1;
} else if (event.keyCode == 39 && snake.rotateX != -1) {
snake.rotateX = 1
snake.rotateY = 0;
} else if (event.keyCode == 40 && snake.rotateY != -1) {
snake.rotateX = 0
snake.rotateY = 1;
}
})
})