-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
178 lines (160 loc) · 4.77 KB
/
Copy pathapp.js
File metadata and controls
178 lines (160 loc) · 4.77 KB
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
//defining game container and scoreboard elements
const gameArea = document.getElementById("game-area");
const scoreSpan = document.getElementById("score");
const highScoreSpan = document.getElementById("high-score");
const restartBtn = document.getElementById("restart-btn");
const pauseBtn = document.getElementById("pause-btn");
const smashSound = document.getElementById("smash-sound");
//defining score variables and keeping track of highscore with localStorge
let score = 0;
let highScore = parseInt(localStorage.getItem("antHighScore")) || 0;
let antInterval = null;
let antSpawnRate = 1200;
let isPaused = false;
let gameStarted = false;
let gameOver = false;
highScoreSpan.textContent = highScore;
//CORE APP FUNCTIONS
const startGame = () => {
const oldGameOver = document.getElementById("game-over");
if (oldGameOver) oldGameOver.remove();
score = 0;
scoreSpan.textContent = score;
gameArea.innerHTML = "";
if (antInterval) clearInterval(antInterval);
antInterval = setInterval(spawnAnt, antSpawnRate);
isPaused = false;
gameStarted = true;
gameOver = false;
restartBtn.textContent = "Quit";
pauseBtn.style.display = "inline-block";
pauseBtn.disabled = false;
pauseBtn.textContent = "Pause";
};
const showGameOver = () => {
//remove existing game over
const oldGameOver = document.getElementById("game-over");
if (oldGameOver) oldGameOver.remove();
//create new game over
const gameOverDiv = document.createElement("div");
gameOverDiv.id = "game-over";
gameOverDiv.textContent = "Game Over";
gameArea.appendChild(gameOverDiv);
};
const stopGame = () => {
clearInterval(antInterval);
antInterval = null;
gameArea.innerHTML = "";
score = 0;
scoreSpan.textContent = score;
isPaused = false;
gameStarted = false;
gameOver = false;
restartBtn.textContent = "Start";
pauseBtn.style.display = "none";
pauseBtn.disabled = true;
pauseBtn.textContent = "Pause";
};
const pauseGame = () => {
clearInterval(antInterval);
isPaused = true;
pauseBtn.textContent = "Resume";
document.querySelectorAll(".ant").forEach((ant) => {
ant.style.animationPlayState = "paused";
ant.style.pointerEvents = "none";
});
};
const resumeGame = () => {
antInterval = setInterval(spawnAnt, antSpawnRate);
isPaused = false;
pauseBtn.textContent = "Pause";
document.querySelectorAll(".ant").forEach((ant) => {
ant.style.animationPlayState = "running";
ant.style.pointerEvents = "auto";
});
};
//ANT FUNCTIONS
const spawnAnt = () => {
if (gameOver) return;
const ant = document.createElement("div");
ant.className = "ant";
const areaRect = gameArea.getBoundingClientRect();
const antWidth = 40;
const antHeight = 40;
//random horizontal position within game area
const x = Math.random() * (areaRect.width - antWidth);
//determine direction: bottom-to-top if score < 5, else randomize
let direction = "up";
if (score >= 5) {
direction = Math.random() < 0.5 ? "up" : "down";
}
//set ant travel time based on score
let antTravelTime = 10;
if (score > 10) {
antTravelTime = 4;
}
//set initial position and animation
if (direction === "up") {
ant.style.left = `${x}px`;
ant.style.top = `${areaRect.height - antHeight}px`;
ant.style.animation = `ant-move-up ${antTravelTime}s linear forwards`;
} else {
ant.style.left = `${x}px`;
ant.style.top = `0px`;
ant.style.animation = `ant-move-down ${antTravelTime}s linear forwards`;
}
//SMASHING FUNCTIONALITY
const smash = () => {
if (gameOver) return;
ant.classList.add("smashed");
smashSound.currentTime = 0;
smashSound.play();
score++;
scoreSpan.textContent = score;
if (score > highScore) {
highScore = score;
highScoreSpan.textContent = highScore;
localStorage.setItem("antHighScore", highScore);
}
ant.removeEventListener("click", smash);
setTimeout(() => {
ant.remove();
}, 400);
};
ant.addEventListener("click", smash);
//GAME OVER IF ANT REACHES THE OTHER SIDE
ant.addEventListener("animationend", () => {
if (gameArea.contains(ant)) {
if (!ant.classList.contains("smashed")) {
gameOver = true;
showGameOver();
clearInterval(antInterval);
antInterval = null;
gameStarted = false;
restartBtn.textContent = "Start";
pauseBtn.style.display = "none";
pauseBtn.disabled = true;
pauseBtn.textContent = "Pause";
}
ant.remove();
}
});
gameArea.appendChild(ant);
};
restartBtn.textContent = "Start";
pauseBtn.style.display = "none";
pauseBtn.disabled = true;
restartBtn.addEventListener("click", () => {
if (!gameStarted) {
startGame();
} else {
stopGame();
}
});
pauseBtn.addEventListener("click", () => {
if (isPaused) {
resumeGame();
} else {
pauseGame();
}
});