forked from hackclub/sprig
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpring Bounce.js
141 lines (126 loc) · 3.02 KB
/
Spring Bounce.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
/*
@title: sprig_dodging
@author: sam liu and lucas
@tags: ['tutorial']
@addedOn: 2022-12-15
*/
// define the sprites in our game
const player = "p";
const obstacle = "o";
// assign bitmap art to each sprite
setLegend(
[obstacle, bitmap`
................
....33333333....
............3...
....66666666....
...6............
....33333333....
............3...
....66666666....
...6............
....33333333....
............3...
....66666666....
...6............
....33333333....
............3...
................`],
[player, bitmap`
................
.....0000000....
.....0.....0....
.....0.0.0.00...
.....0......0...
.....0.000..0...
.....0......0...
.....00000000...
.....00LLLL00...
.....00000000...
......00..00....
......00..00....
......00..00....
......00..00....
......00..00....
................`],
)
// Step 1 - Add player to map
setMap(map`
........
........
........
........
........
........
.....p..
........`)
// Create a variable that shows when the game is running
var gameRunning = true;
// Player movement controls
onInput("a", () => {
if (gameRunning) {
const playerSprite = getFirst(player);
if (playerSprite.x > 0) {
playerSprite.x -= 1; // Move player left
}
}
});
onInput("d", () => {
if (gameRunning) {
const playerSprite = getFirst(player);
if (playerSprite.x < 7) {
playerSprite.x += 1; // Move player right
}
}
});
// Function to spawn an obstacle at a random x position
function spawnObstacle() {
let x = Math.floor(Math.random() * 8); // Random x position (0-7)
let y = 0; // Start at the top
addSprite(x, y, obstacle);
}
// Function to move obstacles down
function moveObstacles() {
let obstacles = getAll(obstacle);
for (let i = 0; i < obstacles.length; i++) {
obstacles[i].y += 1;
}
}
// Function to despawn obstacles when they reach the bottom
function despawnObstacles() {
let obstacles = getAll(obstacle);
for (let i = 0; i < obstacles.length; i++) {
if (obstacles[i].y === 7) { // If the obstacle reaches the bottom row
obstacles[i].remove();
}
}
}
// Function to check if the player was hit by an obstacle
function checkHit() {
const playerSprite = getFirst(player);
const obstacles = getAll(obstacle);
for (let i = 0; i < obstacles.length; i++) {
if (obstacles[i].x === playerSprite.x && obstacles[i].y === playerSprite.y) {
return true; // Player is hit if the x and y positions match
}
}
return false; // No collision
}
// Game loop (runs every second)
var gameLoop = setInterval(() => {
if (gameRunning) {
spawnObstacle(); // Spawn a new obstacle periodically
moveObstacles(); // Move all obstacles down
despawnObstacles(); // Remove obstacles that reach the bottom
// Check if the player has been hit by any obstacle
if (checkHit()) {
clearInterval(gameLoop); // Stop the game loop
gameRunning = false;
addText("Game Over!", {
x: 5,
y: 6,
color: color`9` // Display game over text in red
});
}
}
}, 1000); // Run every 1000 ms (1 second)