-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsketch.js
185 lines (156 loc) · 5.53 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
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
const SCALE = 15;
var Snake;
var Food;
var SpecialFood;
var isFirstSpecialFood = true;
var start = false;
var score = 0;
var highestScore = 0;
var timer = 5;
function setup() {
background(0);
createCanvas(450, 450).parent('sketch-div'); // put under sketch div in html
frameRate(10);
Snake = new Snake();
SpecialFood = createVector(-100, -100);
Food = makeValidFood(generateFood(), SpecialFood);
document.getElementById("pause").style.display = "none";
}
function draw() {
background(0);
//draw food
printFood(Food);
// food detection has to come first
// so that it works with my grow function
if (Snake.isEat(Food)) {
Snake.grow();
Food = makeValidFood(generateFood(), SpecialFood);
// add 100 points for eating food
score += 100;
document.getElementsByClassName("score")[0].innerText = "Score:" + " " + score;
}
// unlock special food once score reaches 1500
if (score > 2000) {
if (isFirstSpecialFood) {
isFirstSpecialFood = false;
SpecialFood = makeValidFood(generateFood(), Food);
}
document.getElementsByTagName("canvas")[0].style.border = "solid 10px rgb(122, 173, 44)";
document.getElementById("sketch-div").style.padding = "0px";
document.getElementsByClassName("info")[0].innerHTML = "2nd Stage";
// draw special food
printSpecialFood(SpecialFood);
// countdown (do not countdown when paused or game is over)
if (frameCount % 10 == 0 && timer > 0 && start == true) {
timer--;
}
// reset timer and regenerate special food position
if (frameCount % 10 == 0 && timer == 0) {
timer = 5; // reset timer
SpecialFood = makeValidFood(generateFood(), Food); // reset position
}
// when snake eats the speical food
if (Snake.isEat(SpecialFood)) {
Snake.grow();
timer = 5; // reset timer
SpecialFood = makeValidFood(generateFood(), Food); // reset position
// add 300 points for eating special food
score += 300;
document.getElementsByClassName("score")[0].innerText = "Score:" + " " + score;
}
}
// snake moves one step forward automatically when game has started
if (start == true) {
Snake.travel();
// add 1 point for each step
score++;
document.getElementsByClassName("score")[0].innerText = "Score:" + " " + score;
}
// when you lose the game
if (isGameOver()) {
document.getElementsByClassName("info")[0].innerHTML = "GAME OVER";
start = false;
document.getElementById("play_arrow").style.display = "inline-block";
document.getElementById("play_arrow").disabled = true;
document.getElementById("pause").style.display = "none";
noLoop();
}
//draw snake
Snake.print();
// check for highest score
if (score > highestScore) {
highestScore = score;
document.getElementsByClassName("highestScore")[0].innerText = "Highest Score:" + " " + highestScore;
}
}
/************************ FUNCTIONS ************************/
// function for button "Start Game"
function startGame() {
start = true;
document.getElementById("play_arrow").style.display = "none";
document.getElementById("pause").style.display = "inline-block";
loop();
}
// function for button "Pause Game"
function pauseGame() {
start = false;
document.getElementById("play_arrow").style.display = "inline-block";
document.getElementById("pause").style.display = "none";
noLoop();
}
// function for button "Restart"
function newGame() {
start = false;
document.getElementById("play_arrow").style.display = "inline-block";
document.getElementById("play_arrow").disabled = false;
document.getElementById("pause").style.display = "none";
Snake.reset();
Food = makeValidFood(generateFood());
SpecialFood = makeValidFood(generateFood());
// reset timer
timer = 5;
// reset score
score = 0;
document.getElementsByClassName("score")[0].innerText = "Score:" + " " + score;
// reset canvas border
document.getElementsByTagName("canvas")[0].style.border = "none";
document.getElementById("sketch-div").style.padding = "10px";
// reset stage
document.getElementsByClassName("info")[0].innerHTML = "1st Stage";
loop();
}
// control the direction of the snake
// NOTE: it will ignore the opposite direction keys
// for example, if the snake is moving to the right, and you pressed
// the left arrow key, it will be ignored
function keyPressed() {
if (start == true) {
if (keyCode === UP_ARROW) {
if (Snake.xSpeed != 0 && Snake.ySpeed != 1) {
Snake.changeDir(0, -1);
}
} else if (keyCode === DOWN_ARROW) {
if (Snake.xSpeed != 0 && Snake.ySpeed != -1) {
Snake.changeDir(0, 1);
}
} else if (keyCode === RIGHT_ARROW) {
if (Snake.xSpeed != -1 && Snake.ySpeed != 0) {
Snake.changeDir(1, 0);
}
} else if (keyCode === LEFT_ARROW) {
if (Snake.xSpeed != 1 && Snake.ySpeed != 0) {
Snake.changeDir(-1, 0);
}
}
}
}
// check for game over
function isGameOver() {
// when it eats itself
for (i = 0; i < Snake.body.length; i++) {
if (Snake.head.equals(Snake.body[i])) {
return true;
}
}
return false;
}