-
Notifications
You must be signed in to change notification settings - Fork 1
/
dinogame.js
165 lines (133 loc) · 4.06 KB
/
dinogame.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
//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;
let dino = {
x : dinoX,
y : dinoY,
width : dinoWidth,
height : dinoHeight
}
//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 = .4;
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 = "./dinogameImgs/dino.png";
dinoImg.onload = function() {
context.drawImage(dinoImg, dino.x, dino.y, dino.width, dino.height);
}
cactus1Img = new Image();
cactus1Img.src = "./dinogameImgs/cactus1.png";
cactus2Img = new Image();
cactus2Img.src = "./dinogameImgs/cactus2.png";
cactus3Img = new Image();
cactus3Img.src = "./dinogameImgs/cactus3.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 = "./dinogameImgs/dino-dead.png";
dinoImg.onload = function() {
context.drawImage(dinoImg, dino.x, dino.y, dino.width, dino.height);
}
}
}
//score
context.fillStyle="black";
context.font="20px courier";
score++;
context.fillText(score, 5, 20);
}
function moveDino(e) {
if (gameOver) {
return;
}
if ((e.code == "Space" || e.code == "ArrowUp") && dino.y == dinoY) {
//jump
velocityY = -10;
}
}
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
}