Skip to content

Commit cb032c9

Browse files
author
Chandagal
committed
Enhancement:provided different levels for Ant_Smasher game #5221
1 parent ac4ce20 commit cb032c9

4 files changed

Lines changed: 169 additions & 21 deletions

File tree

‎Games/Ant_Smasher/index.html‎

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,63 @@
33
<head>
44
<meta charset="UTF-8">
55
<title>Ant Smasher Game</title>
6-
<link href='https://fonts.googleapis.com/css?family=Amatic+SC:400,700' rel='stylesheet' type='text/css'>
6+
7+
<link
8+
href="https://fonts.googleapis.com/css?family=Amatic+SC:400,700"
9+
rel="stylesheet"
10+
type="text/css"
11+
>
712
<link rel="stylesheet" href="style.css">
813
</head>
14+
915
<body>
1016

11-
<h1>Ant Smasher<span class="score">0</span></h1>
12-
<button onClick="startGame()" style="float:right;">Start</button>
17+
<h1>
18+
Ant Smasher
19+
<span class="score">0</span>
20+
</h1>
21+
22+
<!-- Start button -->
23+
<button onclick="startGame()" style="float:right;">
24+
Start
25+
</button>
26+
27+
<!-- Optional level buttons -->
28+
<div style="text-align:center; margin-bottom:10px;">
29+
<button onclick="startGame('easy')">Easy</button>
30+
<button onclick="startGame('medium')">Medium</button>
31+
<button onclick="startGame('hard')">Hard</button>
32+
</div>
33+
34+
<!-- 👇 Default level class added -->
35+
<div class="game easy">
1336

14-
<div class="game">
1537
<div class="hole hole1">
1638
<div class="mole"></div>
1739
</div>
40+
1841
<div class="hole hole2">
1942
<div class="mole"></div>
2043
</div>
44+
2145
<div class="hole hole3">
2246
<div class="mole"></div>
2347
</div>
48+
2449
<div class="hole hole4">
2550
<div class="mole"></div>
2651
</div>
52+
2753
<div class="hole hole5">
2854
<div class="mole"></div>
2955
</div>
56+
3057
<div class="hole hole6">
3158
<div class="mole"></div>
3259
</div>
60+
3361
</div>
3462

35-
<script src="script.js"></script>
63+
<script src="script.js"></script>
3664
</body>
3765
</html>

‎Games/Ant_Smasher/script.js‎

Lines changed: 76 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,114 @@
1+
// =====================
2+
// DOM ELEMENTS
3+
// =====================
14
const holes = document.querySelectorAll(".hole");
2-
const scoreBoard = document.querySelector(".score");
35
const moles = document.querySelectorAll(".mole");
4-
let lastHole;
6+
const scoreBoard = document.querySelector(".score");
7+
const game = document.querySelector(".game");
8+
9+
// =====================
10+
// GAME STATE
11+
// =====================
12+
let lastHole = null;
513
let timeUp = false;
614
let score = 0;
15+
let currentLevel = "easy";
716

8-
function randomTime(min, max) {
17+
// =====================
18+
// LEVEL CONFIGURATION
19+
// =====================
20+
const levels = {
21+
easy: { min: 1000, max: 2000 },
22+
medium: { min: 600, max: 1200 },
23+
hard: { min: 300, max: 800 }
24+
};
25+
26+
// =====================
27+
// UTILITY FUNCTIONS
28+
// =====================
29+
function randomTime() {
30+
const { min, max } = levels[currentLevel];
931
return Math.round(Math.random() * (max - min) + min);
1032
}
1133

1234
function randomHole(holes) {
1335
const idx = Math.floor(Math.random() * holes.length);
1436
const hole = holes[idx];
37+
1538
if (hole === lastHole) {
1639
return randomHole(holes);
1740
}
41+
1842
lastHole = hole;
1943
return hole;
2044
}
2145

46+
// =====================
47+
// CORE GAME LOOP
48+
// =====================
2249
function peep() {
23-
const time = randomTime(500, 2000);
50+
const time = randomTime();
2451
const hole = randomHole(holes);
52+
2553
hole.classList.add("up");
54+
2655
setTimeout(() => {
2756
hole.classList.remove("up");
2857
if (!timeUp) peep();
2958
}, time);
3059
}
3160

32-
function startGame() {
33-
scoreBoard.textContent = 0;
34-
timeUp = false;
61+
// =====================
62+
// LEVEL HANDLING
63+
// =====================
64+
function applyLevelClass() {
65+
game.classList.remove("easy", "medium", "hard");
66+
game.classList.add(currentLevel);
67+
}
68+
69+
function setLevel(level) {
70+
if (!levels[level]) return;
71+
currentLevel = level;
72+
applyLevelClass();
73+
}
74+
75+
function autoLevelUp() {
76+
if (score >= 10 && currentLevel !== "hard") {
77+
setLevel("hard");
78+
} else if (score >= 5 && currentLevel === "easy") {
79+
setLevel("medium");
80+
}
81+
}
82+
83+
// =====================
84+
// GAME CONTROLS
85+
// =====================
86+
function startGame(level = "easy") {
3587
score = 0;
88+
timeUp = false;
89+
lastHole = null;
90+
91+
scoreBoard.textContent = 0;
92+
setLevel(level);
93+
3694
peep();
37-
setTimeout(() => (timeUp = true), 10000);
95+
setTimeout(() => (timeUp = true), 10000); // 10 seconds
3896
}
3997

98+
// =====================
99+
// EVENT HANDLERS
100+
// =====================
40101
function bonk(e) {
41-
if (!e.isTrusted) return; // cheater!
102+
if (!e.isTrusted) return; // anti-cheat
103+
42104
score++;
43105
this.parentNode.classList.remove("up");
44106
scoreBoard.textContent = score;
107+
108+
autoLevelUp();
45109
}
46110

111+
// =====================
112+
// EVENT LISTENERS
113+
// =====================
47114
moles.forEach((mole) => mole.addEventListener("click", bonk));

‎Games/Ant_Smasher/style.css‎

Lines changed: 60 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,39 @@ body {
1616
font-family: "Amatic SC", cursive;
1717
}
1818

19+
/* =====================
20+
HEADER & SCORE
21+
===================== */
1922
h1 {
2023
text-align: center;
2124
font-size: 10rem;
2225
line-height: 1;
23-
margin-bottom: 0;
26+
margin-bottom: 1rem;
2427
}
2528

2629
.score {
27-
background: rgba(255, 255, 255, 0.2);
30+
background: rgba(255, 255, 255, 0.25);
2831
padding: 0 3rem;
29-
line-height: 1;
32+
line-height: 1.2;
3033
border-radius: 1rem;
34+
display: inline-block;
3135
}
3236

37+
/* =====================
38+
GAME BOARD
39+
===================== */
3340
.game {
3441
width: 600px;
3542
height: 400px;
3643
display: flex;
3744
flex-wrap: wrap;
3845
margin: 0 auto;
46+
position: relative;
3947
}
4048

49+
/* =====================
50+
HOLES
51+
===================== */
4152
.hole {
4253
flex: 1 0 33.33%;
4354
overflow: hidden;
@@ -46,27 +57,69 @@ h1 {
4657

4758
.hole:after {
4859
display: block;
49-
background: url(../../assets/images/Ant_Smasher_dirt.svg) bottom center
50-
no-repeat;
60+
background: url("../../assets/images/Ant_Smasher_dirt.svg")
61+
bottom center no-repeat;
5162
background-size: contain;
5263
content: "";
5364
width: 100%;
5465
height: 70px;
5566
position: absolute;
5667
z-index: 2;
5768
bottom: -30px;
69+
pointer-events: none;
5870
}
5971

72+
/* =====================
73+
MOLE
74+
===================== */
6075
.mole {
61-
background: url("../../assets/images/Ant_Smasher.png") bottom center no-repeat;
76+
background: url("../../assets/images/Ant_Smasher.png")
77+
bottom center no-repeat;
6278
background-size: 60%;
6379
position: absolute;
6480
top: 100%;
6581
width: 100%;
6682
height: 100%;
67-
transition: all 0.4s;
83+
cursor: pointer;
84+
will-change: transform, top;
85+
}
86+
87+
/* =====================
88+
LEVEL SPEED CONTROL
89+
===================== */
90+
91+
/* Easy: relaxed */
92+
.game.easy .mole {
93+
transition: top 0.45s ease-out;
94+
}
95+
96+
/* Medium: alert */
97+
.game.medium .mole {
98+
transition: top 0.3s ease-out;
6899
}
69100

101+
/* Hard: frantic */
102+
.game.hard .mole {
103+
transition: top 0.18s linear;
104+
}
105+
106+
/* =====================
107+
MOLE POP-UP STATE
108+
===================== */
70109
.hole.up .mole {
71110
top: 0;
72111
}
112+
113+
/* =====================
114+
OPTIONAL VISUAL FEEDBACK
115+
===================== */
116+
117+
/* Subtle danger tint on harder levels */
118+
.game.medium {
119+
box-shadow: 0 0 15px rgba(255, 165, 0, 0.4);
120+
}
121+
122+
.game.hard {
123+
box-shadow: 0 0 25px rgba(255, 60, 60, 0.6);
124+
}
125+
s

‎assets/images/Ant_Smasher_Demo.png‎

28.1 KB
Loading

0 commit comments

Comments
 (0)