-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
280 lines (244 loc) · 9.65 KB
/
index.html
File metadata and controls
280 lines (244 loc) · 9.65 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
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
<!DOCTYPE html>
<html lang="el">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Παιχνίδι Υγείας</title>
<style>
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
position: relative;
flex-direction: column;
}
canvas {
background-color: #8fc0a9;
border: 2px solid #333;
}
#restartButton, #gameSummary {
display: none;
padding: 10px 20px;
color: #fff;
border: none;
border-radius: 5px;
}
#restartButton {
margin-top: -265px;
padding: 10px 20px;
font-size: 16px;
text-align: center;
background-color: #333;
}
#gameSummary {
margin-top: 10px;
font-size: 16px;
background-color: #fff;
color: #333;
border: 1px solid #333;
}
</style>
</head>
<body>
<canvas id="gameCanvas" width="600" height="300"></canvas>
<button id="restartButton" onclick="resetGame()">Επανεκκίνηση</button>
<div id="gameSummary"></div>
<!-- Sound files -->
<audio id="backgroundSound" src="background.mp3" loop></audio>
<audio id="gameFinishedSound" src="game_finished.mp3"></audio>
<audio id="gameOverSound" src="game_over.mp3"></audio>
<audio id="jumpSound" src="jump.mp3"></audio>
<audio id="goodFoodSound" src="good_food.mp3"></audio>
<audio id="badFoodSound" src="bad_food.mp3"></audio>
<script>
// Game setup
const canvas = document.getElementById("gameCanvas");
const ctx = canvas.getContext("2d");
// Sound elements
const backgroundSound = document.getElementById("backgroundSound");
const gameFinishedSound = document.getElementById("gameFinishedSound");
const gameOverSound = document.getElementById("gameOverSound");
const jumpSound = document.getElementById("jumpSound");
const goodFoodSound = document.getElementById("goodFoodSound");
const badFoodSound = document.getElementById("badFoodSound");
// Start background music
backgroundSound.play();
let health = 10;
let gameOver = false; // Flag to stop the game when reaching the prize
let goodFoodsEaten = 0;
let badFoodsEaten = 0;
const restartButton = document.getElementById("restartButton");
const gameSummary = document.getElementById("gameSummary");
// Player setup (using a human emoji)
const player = {
x: 50,
y: canvas.height - 50,
width: 30, // Approximate size of emoji
height: 30,
emoji: "🧑", // Human emoji
speed: 3,
isJumping: false,
jumpSpeed: 0,
gravity: 0.5
};
// Emojis for foods
const goodFoods = ["🥦", "🐟", "🥔", "🫒", "🧅", "🍠", "🍅", "🌰", "🍲", "🍆", "🍗", "🌿"];
const badFoods = ["🍔", "🥩", "🍬", "🍖", "🥐", "🥤", "🍰", "🍩"];
// Track
let trackSpeed = 2;
const trackSpeedIncrement = 0.001; // Small increment to gradually increase speed
const trackLength = 6000; // Move for approx 1 minute at 60fps
let trackProgress = 0;
// Prize
const prize = {
x: trackLength - 100,
y: canvas.height - 100,
emoji: "🏆", // Prize emoji
size: 50 // Large size for the prize
};
// Keyboard controls
const keys = {};
document.addEventListener("keydown", (e) => {
keys[e.key] = true;
if (e.code === "Space" && !player.isJumping) {
player.isJumping = true;
player.jumpSpeed = -10;
jumpSound.play(); // Play jump sound
}
});
document.addEventListener("keyup", (e) => {
keys[e.key] = false;
});
// Create food items randomly
function createFood() {
const isGoodFood = Math.random() > 0.5; // 50% chance for good or bad food
const emoji = isGoodFood
? goodFoods[Math.floor(Math.random() * goodFoods.length)]
: badFoods[Math.floor(Math.random() * badFoods.length)];
foods.push({
x: canvas.width + Math.random() * 300,
y: Math.random() * (canvas.height - 30), // Random y position within canvas
emoji: emoji,
effect: isGoodFood ? 1 : -2
});
}
// Food items array
const foods = [];
// Game update
function updateGame() {
if (gameOver) return; // Stop the game if it's over
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Update player movement based on key presses
if (keys["ArrowUp"] || keys["w"]) player.y = Math.max(0, player.y - player.speed);
if (keys["ArrowDown"] || keys["s"]) player.y = Math.min(canvas.height - player.height, player.y + player.speed);
// Apply jump physics
if (player.isJumping) {
player.y += player.jumpSpeed;
player.jumpSpeed += player.gravity;
if (player.y >= canvas.height - player.height) {
player.y = canvas.height - player.height;
player.isJumping = false;
}
}
// Move track
trackProgress += trackSpeed;
if (trackProgress >= trackLength) {
// Check if player reaches the prize
if (player.x + player.width >= prize.x - trackProgress + canvas.width && player.y + player.height >= prize.y) {
endGame();
return;
}
}
// Gradually increase the track speed over time
trackSpeed += trackSpeedIncrement;
// Generate food
if (Math.random() < 0.03) createFood();
// Update foods
foods.forEach((food, index) => {
food.x -= trackSpeed;
// Collision detection
if (food.x < player.x + player.width &&
food.x + 30 > player.x && // Approximate width of emoji
food.y < player.y + player.height &&
food.y + 30 > player.y) { // Approximate height of emoji
health += food.effect;
if (food.effect > 0) {
goodFoodsEaten++;
goodFoodSound.play();
}
else {
badFoodsEaten++;
badFoodSound.play();
}
foods.splice(index, 1);
}
// Remove food if it goes off screen
if (food.x < -30) { // Approximate width of emoji
foods.splice(index, 1);
}
});
// Draw prize as a large emoji
if (trackProgress >= trackLength - canvas.width) {
ctx.font = `${prize.size}px Arial`;
ctx.fillText(prize.emoji, prize.x - trackProgress + canvas.width, prize.y + prize.size);
}
// Draw player as an emoji
ctx.font = "30px Arial";
ctx.fillText(player.emoji, player.x, player.y + player.height);
// Draw foods as larger emojis
foods.forEach((food) => {
ctx.font = "30px Arial";
ctx.fillText(food.emoji, food.x, food.y + 30); // Draw emoji with larger size
});
// Draw health inside canvas
ctx.font = "20px Arial";
ctx.fillStyle = "#333";
ctx.fillText(`Υγεία: ${health}`, 10, 30);
// Game over condition
if (health <= 0) {
gameOverSound.play(); // Play game over sound
alert("Game Over! Try again.");
resetGame();
} else {
// runs the game
requestAnimationFrame(updateGame);
}
}
// End game when reaching the prize
function endGame() {
gameOver = true;
backgroundSound.pause(); // Stop background music
gameFinishedSound.play(); // Play game finished sound
displaySummary();
restartButton.style.display = "block"; // Show restart button
}
// Display game summary
function displaySummary() {
gameSummary.innerHTML = `
<p>Καλές τροφές: ${goodFoodsEaten}</p>
<p>Κακές τροφές: ${badFoodsEaten}</p>
<p>Υγεία: ${health}</p>
`;
gameSummary.style.display = "block"; // Show summary
}
// Reset game
function resetGame() {
health = 10;
goodFoodsEaten = 0;
badFoodsEaten = 0;
foods.length = 0;
trackProgress = 0;
trackSpeed = 2; // Reset track speed to initial value
gameOver = false; // Reset game over flag
restartButton.style.display = "none"; // Hide restart button
gameSummary.style.display = "none"; // Hide summary
backgroundSound.play(); // Restart background music
updateGame();
}
// Start game
updateGame();
</script>
</body>
</html>