-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscripts.js
224 lines (199 loc) · 5.31 KB
/
scripts.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
213
214
215
216
217
218
219
220
221
222
223
224
const canvas = document.getElementById("canvas");
const ctx = canvas.getContext("2d");
//game control variables.
let controlPlayer = false;
let hasGameStarted = false;
// just to prevent game from starting while game reloads
let hasGameOver = false;
let startDate = null;
function milliSecondsToMinutes(millis){
let minutes = math.floor(millis / 6000);
let seconds =((millis % 60000)/1000).toFixed(0);
return minutes +":"+ (seconds < 10 ? "0" :"") + seconds;
}
function gameOver() {
if(!hasGameOver){
let endDate = new Date();
let timeDiff = endDate - startDate;
if(timeDiff < 6000){
alert(`You survived ${timeDiff / 1000} seconds!`);
}else{
let minutesSurvived = milliSecondsToMinutes(timeDiff)
alert(`You survived ${minutesSurvived} minutes. wow`);
}
hasGameOver = true;
location.reload();
}
}
//canvas black boarder.
function drawBoarder() {
ctx.fillStyle ='orange';
ctx.fillRect(0,0,canvas.width,canvas.height);
ctx.clearRect(50,50,500,500);
}
let playerRect = {
x:275,
y:275,
width:50,
height: 50
}
//game Rectangle.
let rectangles = [{
x:75,
y:75,
dx:5,
dy:4,
width:75,
height:75,
color: 'green',
},
{
x: 400,
y: 75,
dx: -5,
dy: 5.5,
width: 80,
height: 60,
color: 'green',
},
{
x:75,
y:445,
dx:5,
dy: -5,
width: 40,
height:80,
color:'green'
},
{
x:420,
y:450,
dx:-5,
dy:-5,
width:130,
height:25,
color:'green'
}]
// render rectangles to canvas.
function drawRect() {
//Draw player rect first.
ctx.fillStyle = "red";
ctx.fillRect(playerRect.x,playerRect.y,playerRect.width,playerRect.height);
rectangles.forEach(rect => {
ctx.fillStyle = rect.color;
ctx.fillRect(rect.x,rect.y,rect.width,rect.height);
})
}
//detect whether player makes contact with boarder
function playerCollisionDetection() {
if(
playerRect.x + playerRect.width > 550 ||
playerRect.x < 50 ||
playerRect.y + playerRect.height > 550 ||
playerRect.y < 50
){
gameOver();
}
}
//returns true if 2 rectangles collide
function isRectangleCollision(rect1, rect2){
return !(
rect1.x > rect2.x +rect2.width ||
rect1.x + rect1.width< rect2.x ||
rect1.y > rect2.y + rect2.height ||
rect1.y + rect1.height < rect2.y
);
}
//detect whether player makes contact with rectangle
function rectangleCollisionDetection(){
rectangles.forEach(rect =>{
if(isRectangleCollision(playerRect, rect)){
gameOver();
}
})
}
//get the green rectangles to start moving
function moveRectangle(){
rectangles.forEach(rect=>{
rect.x += rect.dx;
rect.y += rect.dy;
})
}
//detect whether green rectangle hit canvas
function borderRectangleCollisionDetection(){
rectangles.forEach(rect =>{
if(rect.x + rect.width > canvas.width || rect.x <0){
rect.dx *= -1;
}
if(rect.y + rect.height > canvas.height || rect.y < 0){
rect.dy *= -1;
}
})
}
//speed up game
let numberOfSpeed = 0;
function configureRectSpeed(){
const speedUpGame = setInterval(() => {
numberOfSpeed++;
rectangles.forEach(rect => {
rect.dx >= 0 ? rect.dx +=1 : rect.dx -= 1;
rect.dy >= o ? rect.dy +=1 : rect.dy -= 1;
});
if(numberOfSpeed === 4){
clearInterval(speedUpGame);
}
},10000)
}
function update() {
ctx.clearRect(0,0,canvas.width,canvas.height);
drawBoarder();
drawRect();
if(hasGameStarted){
//get rectangle to start moving
moveRectangle();
}
//detect whether player makes contact with boarder
playerCollisionDetection();
//detect whether player makes contact with rectangle
rectangleCollisionDetection();
//detect whether green rectangle hit canvas
borderRectangleCollisionDetection();
requestAnimationFrame(update);
}
// call update on initial document load.
update();
// returns true if mouse is in rectangle
function isCursorInRect(x,y,rect){
return(x>rect.x && x<rect.x+rect.width && y>rect.y && y<rect.y+rect.height);
}
canvas.addEventListener("mousedown", e=>{
//get x and y coordinated in relation to canvas
const pos = {
x: e.clientX - canvas.offsetLeft,
y: e.clientY - canvas.offsetTop
}
//see if they clicked on red square in particular
if(isCursorInRect(pos.x,pos.y,playerRect)){
//start timer
if(!hasGameStarted){
startDate = new Date();
configureRectSpeed();
}
hasGameStarted = true;
controlPlayer = true;
}
})
canvas.addEventListener("mousemove", e => {
if(controlPlayer && !hasGameOver){
//get x and y coordinated in relation to canvas
const pos = {
x: e.clientX - canvas.offsetLeft,
y: e.clientY - canvas.offsetTop
};
playerRect.x = pos.x - 25;
playerRect.y = pos.y - 25;
}
})
canvas.addEventListener("mouseup", ()=>{
controlPlayer = false;
})