forked from OpenBMB/ChatDev
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
159 lines (159 loc) · 4.47 KB
/
script.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
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
// Game Constants
const CANVAS_WIDTH = 800;
const CANVAS_HEIGHT = 600;
const CONTAINER_WIDTH = 100;
const CONTAINER_HEIGHT = 20;
const COIN_RADIUS = 10;
const COIN_SPEED = 3;
const COIN_VALUES = [1, 10, 100];
const GAME_DURATION = 15; // in seconds
// Game Variables
let canvas, ctx;
let containerX, containerY;
let coins = [];
let score = 0;
let gameStarted = false;
let gameTimer;
let gameOverScreen;
let finalScoreElement;
let restartButton;
let countdownElement;
let countdownTimer;
// Initialize the game
function init() {
canvas = document.getElementById("gameCanvas");
ctx = canvas.getContext("2d");
canvas.width = CANVAS_WIDTH;
canvas.height = CANVAS_HEIGHT;
containerX = CANVAS_WIDTH / 2 - CONTAINER_WIDTH / 2;
containerY = CANVAS_HEIGHT - CONTAINER_HEIGHT;
gameOverScreen = document.getElementById("gameOverScreen");
finalScoreElement = document.getElementById("finalScore");
restartButton = document.getElementById("restartButton");
countdownElement = document.createElement("div");
countdownElement.id = "countdown";
document.body.appendChild(countdownElement);
document.addEventListener("keydown", handleKeyDown);
document.addEventListener("keyup", handleKeyUp);
restartButton.addEventListener("click", restartGame);
startGame();
}
// Start the game
function startGame() {
gameStarted = true;
score = 0;
coins = [];
countdownTimer = GAME_DURATION;
gameTimer = setInterval(updateGame, 1000 / 60); // 60 FPS
countdownElement.innerText = countdownTimer;
setTimeout(endGame, GAME_DURATION * 1000);
}
// End the game
function endGame() {
gameStarted = false;
clearInterval(gameTimer);
canvas.style.display = "none";
gameOverScreen.style.display = "block";
finalScoreElement.innerText = "Final Score: " + score;
}
// Restart the game
function restartGame() {
canvas.style.display = "block";
gameOverScreen.style.display = "none";
init();
}
// Update the game state
function updateGame() {
clearCanvas();
updateContainer();
updateCoins();
renderContainer();
renderCoins();
renderScore();
updateCountdown();
}
// Clear the canvas
function clearCanvas() {
ctx.clearRect(0, 0, CANVAS_WIDTH, CANVAS_HEIGHT);
}
// Update the container position based on user input
function updateContainer() {
if (leftKeyPressed && containerX > 0) {
containerX -= 5;
}
if (rightKeyPressed && containerX + CONTAINER_WIDTH < CANVAS_WIDTH) {
containerX += 5;
}
}
// Update the coin positions and check for collisions
function updateCoins() {
for (let i = coins.length - 1; i >= 0; i--) {
const coin = coins[i];
coin.y += COIN_SPEED;
if (coin.y + COIN_RADIUS > containerY && coin.x > containerX && coin.x < containerX + CONTAINER_WIDTH) {
coins.splice(i, 1);
score += coin.value;
}
if (coin.y + COIN_RADIUS > CANVAS_HEIGHT) {
coins.splice(i, 1);
}
}
if (Math.random() < 0.02) {
const coin = {
x: Math.random() * (CANVAS_WIDTH - COIN_RADIUS * 2) + COIN_RADIUS,
y: -COIN_RADIUS,
value: COIN_VALUES[Math.floor(Math.random() * COIN_VALUES.length)]
};
coins.push(coin);
}
}
// Render the container
function renderContainer() {
ctx.fillStyle = "blue";
ctx.fillRect(containerX, containerY, CONTAINER_WIDTH, CONTAINER_HEIGHT);
}
// Render the coins
function renderCoins() {
ctx.fillStyle = "gold";
for (const coin of coins) {
ctx.beginPath();
ctx.arc(coin.x, coin.y, COIN_RADIUS, 0, 2 * Math.PI);
ctx.fill();
}
}
// Render the score
function renderScore() {
ctx.fillStyle = "black";
ctx.font = "20px Arial";
ctx.fillText("Score: " + score, 10, 30);
}
// Update the countdown timer
function updateCountdown() {
countdownTimer -= 1 / 60;
if (countdownTimer <= 0) {
countdownTimer = 0;
}
countdownElement.innerText = Math.ceil(countdownTimer);
}
// Handle keydown events
let leftKeyPressed = false;
let rightKeyPressed = false;
function handleKeyDown(event) {
if (event.key === "ArrowLeft") {
leftKeyPressed = true;
}
if (event.key === "ArrowRight") {
rightKeyPressed = true;
}
}
// Handle keyup events
function handleKeyUp(event) {
if (event.key === "ArrowLeft") {
leftKeyPressed = false;
}
if (event.key === "ArrowRight") {
rightKeyPressed = false;
}
}
// Start the game when the page is loaded
window.onload = init;