Skip to content

Instantly share code, notes, and snippets.

@luizbills
Last active September 21, 2025 01:20
Show Gist options
  • Select an option

  • Save luizbills/80a99709c86ea1fc0e644a877ffda693 to your computer and use it in GitHub Desktop.

Select an option

Save luizbills/80a99709c86ea1fc0e644a877ffda693 to your computer and use it in GitHub Desktop.
Simple platformer with Litecanvas + Utils
litecanvas({
width: 320,
autoscale: false
})
const J = 340 // jump force
const G = 550 // gravity
const TS = 32 // tile size
const p = {
pos: vec(32,0),
v: vec(),
a: vec(),
c: 5,
grounded: false
}
const tiles = tilemap(`
#.........
#.........
#........#
#........#
#...#....#
###.#....#
#...#.#..#
#..##.####
#...#....#
##########
`)
// Art Code: 8x8/# 5 5 5 5 5 5 # 5 5 5 5 5 5 5 5 5 5 0 5 5 0 5 5 5 5 0 5 5 0 5 5 5 5 5 5 5 5 5 5 5 0 5 5 5 5 0 5 5 5 0 0 0 0 5 5 # 5 5 5 5 5 5 #
const player = paint(8, 8, () => spr(0, 0, 8, 8,
`
.555555.
55555555
55055055
55055055
55555555
50555505
55000055
.555555.
`
), {
scale: 4
})
function init() {
console.clear()
volume(0.1)
console.log(tiles)
}
function update(dt) {
p.v.x = 250*(iskeydown('d')-iskeydown('a'))
if (p.grounded && iskeypressed('space')) {
p.v.y = -J
p.grounded = false
sfx([,,392,,.03,.12,1,3.6,,69,,,,,,,,.95,.1])
}
if (p.v.y > 0) {
p.a.y = G * 1.5
} else {
p.a.y = G
}
p.v.y += p.a.y * dt
p.pos.y += p.v.y * dt
p.grounded = false
for (let tile of tiles) {
if (
colrect(
p.pos.x, p.pos.y+1, TS, TS,
...tile
)
) {
const {dir, x, y} = resolverect(
p.pos.x, p.pos.y, TS, TS,
...tile
)
p.pos.y = y
p.v.y = 0
if ( dir === 'bottom') {
if (dir === 'bottom') {
p.grounded = true
}
}
}
}
p.v.x += p.a.x * dt
p.pos.x += p.v.x *dt
for (let tile of tiles) {
if (
colrect(
p.pos.x, p.pos.y+1, TS, TS,
...tile
)
) {
const {dir, x, y} = resolverect(
p.pos.x, p.pos.y, TS, TS,
...tile
)
p.pos.x = x
}
}
}
function draw() {
cls(0)
image(p.pos.x, p.pos.y, player)
for (let tile of tiles) {
rectfill(...tile, 10)
}
}
function tilemap(map) {
map = map.trim().split('\n').map((s) => s.trim())
const tiles = []
for (let y = 0; y < map.length; y++) {
for (let x = 0; x < map[y].length; x++) {
const tile = map[y][x]
if ('#' === tile) {
tiles.push([x*TS, y*TS, TS, TS])
}
}
}
return tiles
}
@luizbills
Copy link
Author

luizbills commented Sep 17, 2025

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment