-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnake2.js
65 lines (56 loc) · 1.57 KB
/
snake2.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
function Snake2(){
this.show = function(){
fill(19,70,221);
//draw the snake2 tail
for(var i=0;i<this.tail.length;i++){
rect(this.tail[i].x, this.tail[i].y, pixel_size, pixel_size);
}
//draw the snake2 head
rect(this.pos.x, this.pos.y, pixel_size, pixel_size)
}
this.update = function(){
//move snake2's position into tail and pop off the end
if(movement2.length){
if(snake2.speed.x != movement2[0][0]*-1 && snake2.speed.y != movement2[0][1]*-1){
snake2.dir(movement2[0][0], movement2[0][1]);
}
movement2.splice(0, 1);
}
this.tail.unshift(createVector(this.pos.x, this.pos.y));
this.tail.pop();
//move the snake2
this.pos.x += this.speed.x * pixel_size;
this.pos.y += this.speed.y * pixel_size;
}
this.dir = function(x, y){
this.speed.x = x;
this.speed.y = y;
}
this.checkDeath = function(){
if(this.pos.x >= width || this.pos.y >= height || this.pos.x < 0 || this.pos.y < 0){
gameState = 'end2';
}
for(var i=0;i<this.tail.length;i++){
if(this.tail[i].x == this.pos.x && this.tail[i].y == this.pos.y){
gameState = 'end2';
}
}
for(var j=0;j<snake1.tail.length;j++)
{
if(snake1.tail[j].x == this.pos.x && snake1.tail[j].y == this.pos.y)
{
gameState = 'end2';
}
}
}
this.eat = function(pos){
return this.pos.x == pos.x && this.pos.y == pos.y;
}
this.reset = function(){
shots = [];
this.tail = [];
this.pos = createVector(0, 0);
this.speed = createVector(1, 0);
}
this.reset();
}