-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.js
236 lines (200 loc) · 4.89 KB
/
game.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
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
;(function(){
class Random{
static get(inicio, final){
return Math.floor(Math.random() * final) + inicio
}
}
class Food{
constructor(x,y){
this.x = x
this.y = y
this.width = 10
this.height = 10
}
draw(){
ctx.fillRect(this.x,this.y,this.width,this.height)
}
static generate(){
return new Food(Random.get(0,500), Random.get(0,300))
}
}
class Square{
constructor(x,y){
this.x = x
this.y = y
this.width = 10
this.height = 10
this.back = null // last square
}
draw(){
ctx.fillRect(this.x,this.y,this.width,this.height)
if(this.hasBack()){
this.back.draw()
}
}
add(){
if(this.hasBack()) return this.back.add()
this.back = new Square(this.x,this.y)
}
hasBack(){
return this.back !== null
}
copy(){
if(this.hasBack()){
this.back.copy()
this.back.x = this.x
this.back.y = this.y
}
}
right(){
this.copy()
this.x += 10
}
left(){
this.copy()
this.x -= 10
}
up(){
this.copy()
this.y -= 10
}
down(){
this.copy()
this.y += 10
}
hit(head, second = false){
if(this == head && !this.hasBack()) return false
if(this == head) return this.back.hit(head, true)
if(second && !this.hasBack()) return false
if(second) return this.back.hit(head)
if(this.hasBack()){
return squareHit(this,head) || this.back.hit(head)
}
return squareHit(this,head)
}
hitBorder(){
console.log(`x: ${this.x}`)
console.log(`y: ${this.y}`)
return this.x > 490 || this.x < 0 || this.y > 290 || this.y < 0
}
}
class Snake{
constructor(){
this.head = new Square(100,0)
this.draw()
this.direction = 'right'
this.head.add()
}
draw(){
this.head.draw()
}
// Avoid snake come back by the same way
right(){
if(this.direction === 'left') return
this.direction = 'right'
}
left(){
if(this.direction === 'right') return
this.direction = 'left'
}
up(){
if(this.direction === 'down') return
this.direction = 'up'
}
down(){
if(this.direction === 'up') return
this.direction = 'down'
}
move(){
if(this.direction === 'up') return this.head.up()
if(this.direction === 'down') return this.head.down()
if(this.direction === 'left') return this.head.left()
if(this.direction === 'right') return this.head.right()
}
eat(){
//TODO: accumulate score
this.head.add()
}
dead(){
// Chocar conmigo mismo
// Chocar con los bordes
return this.head.hit(this.head) || this.head.hitBorder()
}
}
const canvas = document.getElementById('canvas')
const ctx = canvas.getContext('2d')
const score = 0
const snake = new Snake()
let foods = []
window.addEventListener('keydown', function(ev){
if(ev.keyCode > 36 && ev.keyCode < 41) ev.preventDefault()
if(ev.keyCode === 40) return snake.down();
if(ev.keyCode === 39) return snake.right();
if(ev.keyCode === 38) return snake.up();
if(ev.keyCode === 37) return snake.left();
return false
})
const animation = setInterval(function(){
snake.move()
// Borra el cuadrado de acuerdo a la coordenada x, y
// n cantidad de pixeles hacia la derecha y hacia abajo
ctx.clearRect(0,0,canvas.width, canvas.height)
snake.draw()
drawFood()
if(snake.dead()){
window.clearInterval(animation)
}
}, 1000 / 10)
setInterval(function(){
const food = Food.generate()
foods.push(food)
setTimeout(function(){
// Elimina la comida
removeFromFoods(food)
}, 10000)
}, 4000)
function drawFood(){
for(const index in foods){
const food = foods[index]
if(typeof food !== 'undefined'){
food.draw()
if(hit(food,snake.head)){
snake.eat()
removeFromFoods(food)
}
}
}
}
function removeFromFoods(food){
foods = foods.filter(function(f){
return food !== f
})
}
function squareHit(square_1, square_2){
return square_1.x == square_2.x && square_1.y == square_2.y
}
function hit(a,b){
//Check if a collisions with b
var hit = false;
// Colisiones horizontales
if(b.x + b.width >= a.x && b.x < a.x + a.width){
// Colisiones verticales
if (b.y + b.height >= a.y && b.y < a.y + a.height){
hit = true
}
}
// Colisiones de a con b
if(b.x <= a.x && b.x + b.width >= a.x + a.width){
if(b.y <= a.y && b.y + b.height >= a.y + a.height){
hit = true;
}
}
// Colisiones de b con a
if(a.x <= b.x && a.x + a.width >= b.x + b.width){
if(a.y <= b.y && a.y + a.height >= b.y + b.height){
hit = true;
}
}
return hit;
}
})();