-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnake_game.js
More file actions
123 lines (115 loc) · 2.82 KB
/
snake_game.js
File metadata and controls
123 lines (115 loc) · 2.82 KB
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
let bg;
let scl=20;
let fR = 8;
let food;
let highscore = 0;
function setup() {
// replaced hard coded values with relative values
// so that canvas fill fill whole window without overflowing
createCanvas(windowWidth, windowHeight);
scl = width/20;
bg = loadImage("Background.png");
buttons = loadImage("buttons.png");
s = new Snake();
frameRate(fR);
pickLocation();
}
// to make canvas fill the window when window is resized
//function windowResized() {
// resizeCanvas(windowHeight, windowHeight);
//}
function highScore() {
if(highscore<s.tail.length)
{
highscore=s.tail.length;
}
return highscore;
}
function pickLocation() {
let rows = random(floor(width/scl));
let cols = random(floor(width/scl));
food = createVector(floor(rows), floor(cols));
food.mult(scl);
}
function mousePressed(){
if(mouseX>=((width/2)-50) && mouseX<=((width/2)+50))
{
if(mouseY>=(height-300) && mouseY<=(height-200))
{
s.dir(0,-1);
s.head = [50, 50, 0, 0];
if (navigator.vibrate) {
window.navigator.vibrate(35);
}
}
else if (mouseY>=(height-100) && mouseY<=(height-0))
{
s.dir(0, 1);
s.head = [0, 0, 50, 50];
if (navigator.vibrate) {
window.navigator.vibrate(35);
}
}
else if (mouseY>=(height-200) && mouseY<=(height-100)) // pauses the game
{
s.dir(0, 0);
s.head = [50, 50, 50, 50];
if (navigator.vibrate) {
window.navigator.vibrate(50);
}
}
}
else if(mouseY>=(height-200) && mouseY<=(height-100))
{
if(mouseX>=((width/2)-150) && mouseX<=((width/2)-50))
{
s.head = [50, 0, 0, 50];
s.dir(-1,0);
if (navigator.vibrate) {
window.navigator.vibrate(35);
}
}
else if(mouseX>=((width/2)+50) && mouseX<=((width/2)+150))
{
s.head = [0, 50, 50, 0];
s.dir(1,0);
if (navigator.vibrate) {
window.navigator.vibrate(35);
}
}
}
}
function pad(n, width, z) {
z = z || '0';
n = n + '';
return n.length >= width ? n : new Array(width - n.length + 1).join(z) + n;
}
function draw() {
background (0, 0, 0);
image(bg, 0, 0, windowWidth, windowWidth);
fill(255)
// rect((width/2)-152, height-332, 304, 304);
image(buttons, (width/2)-150, height-330, 300, 300);
if(s.eat(food)){
pickLocation();
if (navigator.vibrate) {
window.navigator.vibrate(40);
}
}
s.death();
s.update();
s.show();
stroke(255)
line(0, width, width, width);
line(0, width+45, width, width+45);
// shows the score
textSize(28);
text("Score: "+pad(s.tail.length, 3), 10, width+32);
text("High Score: "+pad(highScore(), 3), 200, width+32);
fill(0, 102, 153);
// food display properties
stroke(0, 0, 0);
strokeWeight(1.5);
fill(244, 5, 40, 220);
rect(food.x, food.y, scl, scl, 90, 90, 90, 90);
}