-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathscript.js
More file actions
212 lines (187 loc) · 5.68 KB
/
script.js
File metadata and controls
212 lines (187 loc) · 5.68 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
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
const resetButton = document.getElementById('resetButton');
const GRAVITY = 0.5;
let player;
let platforms = [];
let goal;
let keys = {
right: false,
left: false,
up: false
};
let gameActive = true;
let winMessage = "You Win!";
// Player Class
class Player {
constructor() {
this.position = { x: 100, y: 400 };
this.velocity = { x: 0, y: 0 };
this.width = 30;
this.height = 30;
this.onGround = false;
}
draw() {
ctx.fillStyle = '#4CAF50';
ctx.fillRect(this.position.x, this.position.y, this.width, this.height);
}
update() {
this.draw();
this.position.x += this.velocity.x;
this.position.y += this.velocity.y;
// Apply gravity if not at bottom of canvas
if (this.position.y + this.height + this.velocity.y < canvas.height) {
this.velocity.y += GRAVITY;
this.onGround = false;
} else {
this.velocity.y = 0;
this.position.y = canvas.height - this.height;
this.onGround = true;
}
// --- NEW ---
// Restart if player hits horizontal boundaries
if (this.position.x < 0 || this.position.x + this.width > canvas.width) {
init();
}
// Restart if player falls off bottom
if (this.position.y > canvas.height) {
init();
}
}
}
// Platform Class
class Platform {
constructor(x, y, width, height) {
this.position = { x, y };
this.width = width;
this.height = height;
}
draw() {
ctx.fillStyle = '#8B4513'; // Brown color for platforms
ctx.fillRect(this.position.x, this.position.y, this.width, this.height);
}
}
// Goal Class
class Goal {
constructor(x, y, width, height) {
this.position = { x, y };
this.width = width;
this.height = height;
}
draw() {
ctx.fillStyle = '#FFD700'; // Gold color for the goal
ctx.fillRect(this.position.x, this.position.y, this.width, this.height);
}
}
// Initialize game elements
function init() {
gameActive = true;
player = new Player();
platforms = [
new Platform(0, 550, 300, 50),
new Platform(400, 450, 200, 50),
new Platform(650, 350, 150, 50),
new Platform(400, 250, 100, 50)
];
goal = new Goal(425, 200, 50, 50); // Goal on the top platform
}
// Main animation loop
function animate() {
if (!gameActive) {
// Display win message
ctx.fillStyle = 'rgba(0, 0, 0, 0.7)';
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.font = '60px Arial';
ctx.fillStyle = '#FFD700';
ctx.textAlign = 'center';
ctx.fillText(winMessage, canvas.width / 2, canvas.height / 2);
return;
}
requestAnimationFrame(animate);
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Draw goal
goal.draw();
// Draw platforms
platforms.forEach(platform => {
platform.draw();
});
// Update player
player.update();
// Player movement (boundary checks moved to player.update)
if (keys.left) {
player.velocity.x = -5;
} else if (keys.right) {
player.velocity.x = 5;
} else {
player.velocity.x = 0;
}
// Platform collision detection
platforms.forEach(platform => {
// Check for collision on top of the platform
if (
player.position.y + player.height <= platform.position.y &&
player.position.y + player.height + player.velocity.y >= platform.position.y &&
player.position.x + player.width >= platform.position.x &&
player.position.x <= platform.position.x + platform.width
) {
player.velocity.y = 0;
player.position.y = platform.position.y - player.height;
player.onGround = true;
}
});
// Goal collision detection
if (
player.position.x < goal.position.x + goal.width &&
player.position.x + player.width > goal.position.x &&
player.position.y < goal.position.y + goal.height &&
player.position.y + player.height > goal.position.y
) {
gameActive = false; // Stop the game
}
}
// Event Listeners
function handleKeyDown({ keyCode }) {
switch (keyCode) {
case 37: // Left Arrow
case 65: // A
keys.left = true;
break;
case 39: // Right Arrow
case 68: // D
keys.right = true;
break;
case 32: // Spacebar
case 87: // W
if (player.onGround) {
player.velocity.y = -12; // Jump height
player.onGround = false;
}
break;
}
}
function handleKeyUp({ keyCode }) {
switch (keyCode) {
case 37: // Left Arrow
case 65: // A
keys.left = false;
break;
case 39: // Right Arrow
case 68: // D
keys.right = false;
break;
}
}
window.addEventListener('keydown', handleKeyDown);
window.addEventListener('keyup', handleKeyUp);
// --- UPDATED ---
// Reset button listener logic fixed
resetButton.addEventListener('click', () => {
const wasGameStopped = !gameActive;
init(); // Resets gameActive to true
if (wasGameStopped) {
animate(); // Manually restart the animation loop if it was stopped (on win screen)
}
});
// Start the game
init();
animate();