-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_game.js
More file actions
99 lines (88 loc) · 2.4 KB
/
main_game.js
File metadata and controls
99 lines (88 loc) · 2.4 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
import kaboom from "https://unpkg.com/kaboom/dist/kaboom.mjs";
kaboom({
background: [0, 0, 0],
});
const SPEED = 620;
let BSPEED = 2;
let SCORE = 0;
loadSprite("bad", "pg.png");
loadSprite("hero", "ultra.jpeg");
loadSprite("ball", "ball.jpeg");
scene("main", () => {
const player = add([
sprite("hero"),
pos(120, 80),
scale(0.5),
area(),
]);
// Create score text at top-right
let scoreText = add([
text(`Score: ${SCORE}`, 24),
pos(width() - 20, 20),
]);
// Align right by shifting left by the text width
scoreText.pos.x = width() - 20 - scoreText.width;
const updateScore = () => {
scoreText.text = `Score: ${SCORE}`;
scoreText.pos.x = width() - 20 - scoreText.width;
};
// Player movement
onKeyDown("left", () => player.move(-SPEED, 0));
onKeyDown("right", () => player.move(SPEED, 0));
onKeyDown("up", () => player.move(0, -SPEED));
onKeyDown("down", () => player.move(0, SPEED));
// Spawn bugs and balls every 4 seconds
loop(4, () => {
for (let i = 0; i < 4; i++) {
let x = rand(0, width());
let y = height();
const bad = add([
sprite("bad"),
pos(x, y),
scale(0.1),
area(),
"bad",
]);
bad.onUpdate(() => {
bad.moveTo(bad.pos.x, bad.pos.y - BSPEED);
if (bad.pos.y < 0) {
destroy(bad);
}
});
}
if(BSPEED<13){BSPEED+=1}
let x = rand(0, width());
let y = height();
const ball = add([
sprite("ball"),
pos(x, y),
scale(0.6),
area(),
"ball",
]);
ball.onUpdate(() => {
ball.moveTo(ball.pos.x, ball.pos.y - BSPEED);
if (ball.pos.y < 0) {
destroy(ball);
}
});
});
// Collisions
player.onCollide("bad", (bad) => {
destroy(player);
addKaboom(player.pos);
// Add game over or restart logic here if you want
scoreText = add([
text(`Game Over`),
color(0,0,225),
pos(5, 21),
]);
});
player.onCollide("ball", (ball) => {
destroy(ball);
SCORE += 1;
updateScore();
console.log("Score:", SCORE);
});
});
go("main");