Skip to content

Commit

Permalink
subway_surfers_infinite_runner (#2264)
Browse files Browse the repository at this point in the history
* subway_surfers_infinite_runner

* Update subway surfers.js

---------

Co-authored-by: Cheru Berhanu <[email protected]>
Co-authored-by: Cheru Berhanu <[email protected]>
  • Loading branch information
3 people authored Sep 4, 2024
1 parent c2fcab7 commit c3bd474
Showing 1 changed file with 157 additions and 0 deletions.
157 changes: 157 additions & 0 deletions games/subway surfers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
/*
First time? Check out the tutorial game:
https://sprig.hackclub.com/gallery/getting_started
@title: subway surfers
@author: AKKS1013
@tags: []
@addedOn: 2024-09-03
*/

const player = "p"
const obstacle_low = "l"
const obstacle_high = "h"

let obstacleInterval = setInterval(moveObstacles, 1000)
let spawn = false

setLegend(
[ player, bitmap`
................
................
.......000......
.......0.0......
......0..0......
......0...0.0...
....0003.30.0...
....0.0...000...
....0.05550.....
......0...0.....
.....0....0.....
.....0...0......
......000.......
......0.0.......
.....00.00......
................` ],
[ obstacle_low, bitmap`
................
................
................
................
................
................
3333333333333333
3333333333333333
3333333333333333
3333333333333333`],
[ obstacle_high, bitmap`
5555555555555555
5555555555555555
5555555555555555
5555555555555555
5555555555555555
5555555555555555
5..............5
5..............5
5..............5
5..............5`]
)

setSolids([ player, obstacle_low, obstacle_high ])

function spawnObstacle(obstacle, three) {
const rand = Math.random()
const randx = Math.floor(Math.random() * width())
const randn = Math.random()

while (obstacle === randx) {
randx = Math.floor(Math.random() * width())
}

if (rand <= 0.5) {
addSprite(randx, 0, obstacle_low)
obstacle = randx
} else {
addSprite(randx, 0, obstacle_high)
obstacle = randx
}
if (randn > 0.5 && !three) {
three = true
spawnObstacle(randx, three)
}
}

function moveObstacles() {
const obstacles = getAll(obstacle_low).concat(getAll(obstacle_high));

obstacles.forEach(obstacle => {
if (getTile(obstacle.x, obstacle.y + 1).length !== 0) {
addText("GAME OVER", {x: 5, y: 4, color: color`3`});
clearInterval(obstacleInterval);
} else {

obstacle.y += 1;

if (obstacle.y >= height() - 1) {
obstacle.remove();
}
}
});

if (spawn === true) {
spawnObstacle(-1, false)
spawn = false
} else {
spawn = true
}
}

let level = 0
const levels = [
map`
...
...
l.h
.p.`
]

setMap(levels[level])

setPushables({
[ obstacle_low ]: [ player ],
[ obstacle_high ]: [ player ]
})

onInput("w", () => {
getFirst(player).y -= 1
})

onInput("s", () => {
getFirst(player).y += 1
})

onInput("a", () => {
getFirst(player).x -= 1
})

onInput("d", () => {
getFirst(player).x += 1
})

onInput("i", () => {
if (getTile(getFirst(player).x, getFirst(player).y - 1)[0].type === obstacle_low) {
getFirst(player).y -= 2
}
})

onInput("k", () => {
if (getTile(getFirst(player).x, getFirst(player).y - 1)[0].type === obstacle_high) {
getFirst(player).y -= 2
}
})


afterInput(() => {

})

0 comments on commit c3bd474

Please sign in to comment.