/
sketch.js
148 lines (122 loc) · 3.39 KB
/
sketch.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
var s;
var scl = 15;
var food;
var score = 0;
var col = color(25, 23, 200, 50);
var playing = false;
var direction = 'right';
var fr;
var canvas;
function setup() {
canvas = createCanvas(windowWidth * 0.3, windowHeight * 0.4);
canvas.parent('container-game');
background(0, 0, 0);
s = new Snake ();
newFood();
noLoop();
frameRate(12);
}
function playGame () {
if (playing) {
console.log('You are already playing!');
} else {
playing = true;
loop();
}
}
function resetGame () {
location.reload();
}
function newFood () {
var cols = floor(width / scl);
var rows = floor(height / scl);
food = createVector(floor(random(cols-scl)), floor(random(rows-scl)));
food.mult(scl);
}
function windowResized () {
resizeCanvas(windowWidth * 0.5, windowHeight * 0.5);
background(0, 0, 0);
}
function draw () {
background(0,0,0);
if (s.eat(food)) {
newFood();
score = score + 1;
document.getElementById('score').innerHTML = score;
}
s.death()
s.update();
s.show();
fill(255, 0, 100);
rect(food.x, food.y, scl, scl);
}
function keyPressed () {
if (keyCode === UP_ARROW && direction != 'down') {
direction = 'up'
s.slither(0, -1);
} else if (keyCode === DOWN_ARROW && direction != 'up') {
direction = 'down';
s.slither(0, 1);
} else if (keyCode === LEFT_ARROW && direction != 'right') {
direction = 'left';
s.slither(-1, 0);
} else if (keyCode === RIGHT_ARROW && direction != 'left') {
direction = 'right';
s.slither(1, 0);
}
}
function Snake () {
this.x = scl;
this.y = scl;
this.xspeed = 1;
this.yspeed = 0;
this.total = 0;
this.tail = [];
this.slither = function(x, y) {
this.xspeed = x;
this.yspeed = y;
}
this.update = function () {
for (var i = 0; i < this.tail.length - 1; i++) {
this.tail[i] = this.tail[i+1];
}
if (this.total >= 1) {
this.tail[this.total - 1] = createVector(this.x, this.y);
}
this.x = this.x + this.xspeed*scl;
this.y = this.y + this.yspeed*scl;
this.x = constrain(this.x, 0, width-scl);
this.y = constrain(this.y, 0, height-scl);
}
this.show = function () {
fill(255)
for (var i = 0; i < this.tail.length; i++) {
rect(this.tail[i].x, this.tail[i].y, scl, scl);
}
fill(255)
rect(this.x, this.y, scl, scl);
}
this.eat = function (obj) {
var d = dist(this.x, this.y, obj.x, obj.y);
if (d < 1) {
this.total++;
return true;
} else {
return false;
}
}
this.death = function () {
if(this.x >= width || this.y >= height || this.x < 0 || this.y < 0){
document.getElementById('score').style.color = "red";
document.getElementById('gameOver').style.color = "red";
noLoop();
}
for(var i=0;i<this.tail.length;i++){
if(this.tail[i].x == this.x && this.tail[i].y == this.y){
document.getElementById('score').style.color = "red";
document.getElementById('gameOver').style.color = "red";
noLoop();
}
}
}
}