Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add files via upload #2784

Closed
wants to merge 3 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add files via upload
Vinesh-0710 authored Jan 11, 2025
commit c127e66e4ceafdc17d15f91eda25948bc14a5fe8
141 changes: 141 additions & 0 deletions Spring Bounce.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,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)