-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
102 lines (84 loc) · 2.41 KB
/
main.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
// import Modules
import Car, { laneDirectionLeft, laneDirectionRight } from "./modules/car.js";
import {
myCanvas,
ctx,
drawFrogImage,
moveFrog,
releasedKey,
xFrog,
yFrog,
resetFrog,
} from "./modules/frog.js";
import { drawMotorway } from "./modules/motorway.js";
import { blinking, stopBlinking } from "./modules/blinking.js";
import { instructionsUp, clearInstructions } from "./modules/instructions.js";
let playing,
running = false;
// animate canvas
function animate(
leftCar1,
carsRight1,
leftCar2,
carsRight2,
leftCar3,
carsRight3
) {
// clear frog and cars previous position
ctx.clearRect(0, 0, myCanvas.width, myCanvas.height);
// Draw motherWay
drawMotorway(14);
leftCar1.forEach((car) => car.drawLeft());
carsRight1.forEach((car) => car.drawRight());
leftCar2.forEach((car) => car.drawLeft());
carsRight2.forEach((car) => car.drawRight());
leftCar3.forEach((car) => car.drawLeft());
carsRight3.forEach((car) => car.drawRight());
drawFrogImage(xFrog, yFrog);
if (running === true) {
playing = window.requestAnimationFrame(() =>
animate(leftCar1, carsRight1, leftCar2, carsRight2, leftCar3, carsRight3)
);
}
}
export function stopAnimation() {
window.cancelAnimationFrame(playing);
}
export function stopGame() {
// stop animation
running = false;
// release keys from memory
window.addEventListener("keyup", releasedKey);
}
export function restartGame() {
running = true;
}
export function playGame() {
console.log("Pre-load game");
// reset frog position
resetFrog();
restartGame();
const leftCar1 = laneDirectionLeft(3, 14, 0.5),
carsRight1 = laneDirectionRight(2, 34, 1),
leftCar2 = laneDirectionLeft(1, 54, 4),
carsRight2 = laneDirectionRight(2, 74, 0.5),
leftCar3 = laneDirectionLeft(3, 94, 1),
carsRight3 = laneDirectionRight(3, 114, 1);
ctx.clearRect(0, 0, myCanvas.width, myCanvas.height);
animate(leftCar1, carsRight1, leftCar2, carsRight2, leftCar3, carsRight3);
}
// start game click
const gameText = document.querySelector(".game-text");
// get text blinking
blinking();
gameText.addEventListener("click", startGame);
window.addEventListener("keyup", startGame);
function startGame() {
window.removeEventListener("keyup", startGame);
// stop message instructions
stopBlinking();
// get instructions
instructionsUp();
// clear instructions after 4 seconds
setTimeout(clearInstructions, 4000);
}