-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnake.js
118 lines (100 loc) · 2.79 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
function Snake(){
var x1 = "x1";
var y1 = "y1"
this.x = 0;
this.y = 0;
this.xSpeed = speed;
this.ySpeed = 0;
this.total = 0;
this.tail = [];
this.movement = function(xDir, yDir){
this.xSpeed = xDir;
this.ySpeed = yDir;
}
this.eatFood = function(food){
if(dist(this.x, this.y, food.x, food.y) < 2){
food.pickLocation();
this.total++;
var theScore = document.getElementById("scores");
theScore.innerHTML = "Score: " + this.total;
}
}
this.newGame = function(){
this.x = 0;
this.y = 0;
this.xSpeed = speed;
this.ySpeed = 0;
this.total = 0;
this.tail = [];
oppKey = 0;
lose = false;
backColorR += backChangeR;
backColorG += backChangeG;
while(Math.abs(backColorR - 19) < 30 && Math.abs(255 - backColorG) < 30){
backColorR += backChangeR;
backColorG += backChangeG;
if(backColorR >= 255 - backChangeR || backColorR <= 0){
backChangeR = -backChangeR;
}
if(backColorG >= 255 - backChangeG || backColorG <= 0){
backChangeG = -backChangeG;
}
}
if(backColorR >= 255 - backChangeR || backColorR <= 0){
backChangeR = -backChangeR;
}
if(backColorG >= 255 - backChangeG || backColorG <= 0){
backChangeG = -backChangeG;
}
}
this.update = function(){
for(var c = 0; c < this.tail.length - 1; c++){
this.tail[c] = this.tail[c+1];
}
if(this.total > 0){
this.tail[this.total-1] = createVector(this.x, this.y);
}
this.x += this.xSpeed * blockSize;
this.y += this.ySpeed * blockSize;
this.x = constrain(this.x, -1, (cnv.width-blockSize+1));
this.y = constrain(this.y, -1, cnv.height-blockSize+1);
}
this.show = function(){
fill(0, 100, 255);
noStroke();
rect(this.x, this.y, blockSize, blockSize);
for(var c = 0; c < this.tail.length; c++){
fill(19, 255, 255);
rect(this.tail[c].x, this.tail[c].y, blockSize, blockSize);
}
}
this.deathScreen = function(){
lose = true;
fill('#EAE8EE');
rect(0, 0, cnv.width, cnv.height);
textSize(20);
textAlign(CENTER);
fill('#5E46FF');
text("You lose! Press ENTER for a new game.", cnv.width/2 - 100, cnv.height/2 -50, 200, 100)
if(this.total > highScore){
highScore = this.total;
var theScore = document.getElementById("highscore");
theScore.innerHTML = "Highscore: " + this.total;
}
}
this.death = function(){
if(this.x > cnv.width - blockSize || this.x < 0){
this.deathScreen();
}
if(this.y > cnv.height - blockSize || this.y < 0){
this.deathScreen();
}
for(var c = 0; c < this.tail.length - 1; c++){
if(this.tail[c].x == this.x){
if(this.tail[c].y == this.y){
this.deathScreen();
}
}
}
}
}