-
Notifications
You must be signed in to change notification settings - Fork 0
/
snake.js
181 lines (153 loc) · 4.52 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
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
/* Final Project
* Author: Alistair Chambers
* Date: 12/15/21
* Filename: snake.js
*/
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 = 0
this.rotateY = 1
}
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(){
console.log("apple")
console.log(snake.size)
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
}
}
console.log(this.x , this.y)
this.size = snake.size
this.color = "red"
if(!isTouching){
break;
}
}
}
}
var canvas = document.getElementById("canvas")
var snake = new Snake(20,20,20);
var apple = new Apple();
var active = true;
var canvasContext = canvas.getContext('2d');
window.onload = ()=>{
gameLoop();
}
function gameLoop(){
setInterval(show, 100)
}
function show(){
update();
draw();
}
function update(){
if(active== true){
canvasContext.clearRect(0,0, 400, 400)
snake.move()
eatApple()
checkHitWall();
}
}
function checkHitWall(){
var headTail = snake.tail[snake.tail.length -1]
if(headTail.x == - snake.size) {
headTail.x = canvas.width - snake.size
} else if(headTail.x == canvas.widh) {
headTail.x = 0
} else if(headTail.y == - snake.size) {
headTail.y = canvas.height - snake.size
} else if(headTail.y == canvas.height) {
headTail.y = 0
}
}
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.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;
}
}, 1)
// pauses the game
if (event.shiftKey){
if (active==true){
active=false;
}else{
active = true;
}
}
//resets the game on spacebar
if (event.key == " " || event.key == "Spacebar"){
snake = new Snake(20,20,20);
apple = new Apple();
}
})