-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdodge.html
199 lines (185 loc) · 6.4 KB
/
dodge.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Dodge Game</title>
<style>
body {
font-family: Arial, sans-serif;
text-align: center;
background: linear-gradient(135deg, #1e3c72, #2a5298);
color: white;
margin: 0;
padding: 0;
}
canvas {
border: 4px solid white;
display: block;
margin: 20px auto;
width: 90vw;
max-width: 400px;
height: auto;
background: #0f1017;
}
.btn {
background: #ff9800;
color: white;
padding: 10px 20px;
border: none;
cursor: pointer;
font-size: 16px;
margin: 5px;
border-radius: 5px;
}
.touch-controls, .mode-controls {
display: flex;
justify-content: center;
margin-top: 10px;
}
.mode-btn {
background: #ff5722;
color: white;
padding: 8px 15px;
border: none;
margin: 5px;
border-radius: 5px;
cursor: pointer;
}
.mode-btn.active {
background: #4CAF50;
}
.touch-btn {
width: 60px;
height: 60px;
margin: 5px;
background: #ff9800;
color: white;
font-size: 24px;
border: none;
border-radius: 10px;
cursor: pointer;
}
#endScreen {
display: none;
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background: rgba(0, 0, 0, 0.8);
padding: 20px;
border-radius: 10px;
}
</style>
</head>
<body>
<h1>Dodge Game</h1>
<p>Move left and right to avoid falling objects!</p>
<div class="mode-controls">
<button class="mode-btn" id="slow" onclick="setMode('slow')">Slow</button>
<button class="mode-btn" id="medium" onclick="setMode('medium')">Medium</button>
<button class="mode-btn" id="fast" onclick="setMode('fast')">Fast</button>
</div>
<canvas id="gameCanvas" width="400" height="600"></canvas>
<div id="score">Score: 0</div>
<div class="touch-controls">
<button class="touch-btn" onclick="movePlayer(-20)">◀</button>
<button class="touch-btn" onclick="movePlayer(20)">▶</button>
</div>
<div id="endScreen">
<h2>Game Over</h2>
<p id="finalScore"></p>
<button class="btn" onclick="restartGame()">Restart</button>
</div>
<script>
const canvas = document.getElementById("gameCanvas");
const ctx = canvas.getContext("2d");
let player = { x: 180, y: 550, width: 40, height: 40 };
let obstacles = [];
let score = 0;
let gameRunning = true;
let speed = 0;
let spawnRate = 0.00;
function getRandomColor() {
const colors = ["red", "green", "purple", "#f72702", "yellow", "#f70258", "#12b84c"]; // Avoid blue and background color
return colors[Math.floor(Math.random() * colors.length)];
}
function movePlayer(distance) {
if (gameRunning) {
player.x = Math.max(0, Math.min(canvas.width - player.width, player.x + distance));
}
}
function setMode(mode) {
document.querySelectorAll('.mode-btn').forEach(btn => btn.classList.remove('active'));
document.getElementById(mode).classList.add('active');
if (mode === 'slow') {
speed = 2;
spawnRate = 0.02;
} else if (mode === 'medium') {
speed = 3;
spawnRate = 0.03;
} else if (mode === 'fast') {
speed = 4;
spawnRate = 0.04;
}
restartGame();
}
function gameLoop() {
if (!gameRunning) return;
ctx.clearRect(0, 0, canvas.width, canvas.height);
drawPlayer();
drawObstacles();
updateObstacles();
checkCollision();
score++;
document.getElementById("score").innerText = `Score: ${score}`;
requestAnimationFrame(gameLoop);
}
function drawPlayer() {
ctx.fillStyle = "blue";
ctx.fillRect(player.x, player.y, player.width, player.height);
}
function drawObstacles() {
obstacles.forEach(obstacle => {
ctx.fillStyle = obstacle.color;
ctx.fillRect(obstacle.x, obstacle.y, obstacle.width, obstacle.height);
});
}
function updateObstacles() {
obstacles.forEach(obstacle => {
obstacle.y += speed;
});
obstacles = obstacles.filter(obstacle => obstacle.y < canvas.height);
if (Math.random() < spawnRate) {
obstacles.push({ x: Math.random() * (canvas.width - 40), y: 0, width: 40, height: 40, color: getRandomColor() });
}
}
function checkCollision() {
for (let obstacle of obstacles) {
if (player.x < obstacle.x + obstacle.width && player.x + player.width > obstacle.x && player.y < obstacle.y + obstacle.height && player.y + player.height > obstacle.y) {
endGame();
return;
}
}
}
function endGame() {
gameRunning = false;
document.getElementById("finalScore").innerText = `Final Score: ${score}`;
document.getElementById("endScreen").style.display = "block";
}
function restartGame() {
score = 0;
obstacles = [];
gameRunning = true;
document.getElementById("endScreen").style.display = "none";
gameLoop();
}
document.addEventListener("keydown", (e) => {
if (e.key === "ArrowLeft") movePlayer(-20);
else if (e.key === "ArrowRight") movePlayer(20);
else if (e.key === " " || e.key === "Enter") restartGame();
});
setMode('medium');
</script>
</body>
</html>