-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
212 lines (174 loc) · 5.13 KB
/
script.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
//board
let board;
let boardWidth = 750;
let boardHeight = 250;
let context;
//dino
let dinoWidth = 88;
let dinoHeight = 94;
let dinoX = 50;
let dinoY = boardHeight - dinoHeight;
let dinoImg;
//Game-over
let overWidth = 386;
let overHeight = 40;
let overX = 182;
let overY = 105;
let overImg;
let reWidth = 76;
let reHeigth = 68;
let reX = 337;
let reY = 150;
let resetImg;
let dino = {
x : dinoX,
y : dinoY,
width : dinoWidth,
height : dinoHeight
}
let over = {
x : overX,
y : overY,
width : overWidth,
height : overHeight
}
let reset = {
x : reX,
y : reY,
width : reWidth,
height : reHeight
}
//cactus
let cactusArray = [];
let cactus1Width = 34;
let cactus2Width = 69;
let cactus3Width = 102;
let cactusHeight = 70;
let cactusX = 700;
let cactusY = boardHeight - cactusHeight;
let cactus1Img;
let cactus2Img;
let cactus3Img;
//physics
let velocityX = -8; //cactus moving left speed
let velocityY = 0;
let gravity = .6;
let gameOver = false;
let score = 0;
window.onload = function() {
board = document.getElementById("board");
board.height = boardHeight;
board.width = boardWidth;
context = board.getContext("2d"); //used for drawing on the board
//draw initial dinosaur
// context.fillStyle="green";
// context.fillRect(dino.x, dino.y, dino.width, dino.height);
dinoImg = new Image();
dinoImg.src = "./img/dino.png";
dinoImg.onload = function() {
context.drawImage(dinoImg, dino.x, dino.y, dino.width, dino.height);
}
cactus1Img = new Image();
cactus1Img.src = "./img/cactus1.png";
cactus2Img = new Image();
cactus2Img.src = "./img/cactus2.png";
cactus3Img = new Image();
cactus3Img.src = "./img/cactus3.png";
overImg = new Image();
overImg.src = "./img/game-over.png";
resetImg = new Image();
resetImg.src = "./img/reset.png";
requestAnimationFrame(update);
setInterval(placeCactus, 1000); //1000 milliseconds = 1 second
document.addEventListener("keydown", moveDino);
}
function update() {
requestAnimationFrame(update);
if (gameOver) {
return;
}
context.clearRect(0, 0, board.width, board.height);
//dino
velocityY += gravity;
dino.y = Math.min(dino.y + velocityY, dinoY); //apply gravity to current dino.y, making sure it doesn't exceed the ground
context.drawImage(dinoImg, dino.x, dino.y, dino.width, dino.height);
//cactus
for (let i = 0; i < cactusArray.length; i++) {
let cactus = cactusArray[i];
cactus.x += velocityX;
context.drawImage(cactus.img, cactus.x, cactus.y, cactus.width, cactus.height);
if (detectCollision(dino, cactus)) {
gameOver = true;
dinoImg.src = "./img/dino-dead.png";
resetImg.src = "./img/reset.png";
overImg.src = "./img/game-over.png";
dinoImg.onload = function() {
context.drawImage(dinoImg, dino.x, dino.y, dino.width, dino.height);
}
overImg.onload = function() {
context.drawImage(overImg,over.x,over.y,over.width,over.height);
}
}
}
//score
context.fillStyle="black";
context.font="20px courier";
score++;
context.fillText(score,700, 40);
if (score <= 500) {
velocityX = -8;
}
else if (score>500 && score <=1000) {
velocityX = -10;
}
else {
velocityX = -15;
}
}
function moveDino(e) {
if (gameOver) {
return;
}
if ((e.code == "Space" || e.code == "ArrowUp") && dino.y == dinoY) {
//jump
velocityY = -14;
}
}
function placeCactus() {
if (gameOver) {
return;
}
//place cactus
let cactus = {
img : null,
x : cactusX,
y : cactusY,
width : null,
height: cactusHeight
}
let placeCactusChance = Math.random(); //0 - 0.9999...
if (placeCactusChance > .90) { //10% you get cactus3
cactus.img = cactus3Img;
cactus.width = cactus3Width;
cactusArray.push(cactus);
}
else if (placeCactusChance > .70) { //30% you get cactus2
cactus.img = cactus2Img;
cactus.width = cactus2Width;
cactusArray.push(cactus);
}
else if (placeCactusChance > .50) { //50% you get cactus1
cactus.img = cactus1Img;
cactus.width = cactus1Width;
cactusArray.push(cactus);
}
if (cactusArray.length > 5) {
cactusArray.shift(); //remove the first element from the array so that the array doesn't constantly grow
}
}
function detectCollision(a, b) {
return a.x < b.x + b.width && //a's top left corner doesn't reach b's top right corner
a.x + a.width > b.x && //a's top right corner passes b's top left corner
a.y < b.y + b.height && //a's top left corner doesn't reach b's bottom left corner
a.y + a.height > b.y; //a's bottom left corner passes b's top left corner
}