Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// Infinite runner!
import "sounds"
clear
display(5).mode = displayMode.tile
td = display(5)
td.extent = [150, 10]
td.tileSet = file.loadImage("/sys/pics/SimplePlatformTiles.png")
td.tileSetTileSize = [64,64]
td.cellSize = 64
td.clear
td.scrollX = 0
kGroundTile = 0
kBlockTile = 40
kGroundLevel = 110
// create the ground
for x in range(0, 149)
td.setCell x, 0, kGroundTile
end for
// add some obstacles (tile 40)
for x in range(20, 120, 13)
td.setCell x, 1, kBlockTile
end for
for x in range(20, 120, 17)
td.setCell x, 1, kBlockTile
end for
// set up sprite display
sd = display(4)
sd.clear
// load animation frames
frame = {}
frame.run = []
frame.run.push file.loadImage("/sys/pics/KP/KP-run1.png")
frame.run.push file.loadImage("/sys/pics/KP/KP-run2.png")
frame.jump = file.loadImage("/sys/pics/KP/KP-jump.png")
frame.fall = file.loadImage("/sys/pics/KP/KP-fallen.png")
// load sounds
hitSound = file.loadSound("/sys/sounds/hit.wav")
// prepare our hero
hero = new Sprite
hero.image = frame.run[0]
sd.sprites.push hero
hero.x = 280
hero.y = kGroundLevel
hero.curFrame = 0
hero.nextFrameTime = 0 // time at which we should change frames
hero.vy = 0
hero.update = function
if key.available and key.get == " " then
// jump (IF we are currently grounded)
if self.image != frame.jump then
sounds.bounce.play 0.2
self.image = frame.jump
self.vy = 20
end if
end if
if self.image == frame.jump then
// continue jumping
self.y = self.y + self.vy
self.vy = self.vy - 2
if self.y <= kGroundLevel then
// stop jumping (i.e. land)
sounds.land.play
self.image = frame.run[0]
self.y = kGroundLevel
self.vy = 0
end if
else
// continue running
if time > self.nextFrameTime then
self.curFrame = (self.curFrame + 1) % frame.run.len
self.image = frame.run[self.curFrame]
self.nextFrameTime = time + 0.1
end if
end if
self.checkCollision
end function
hero.checkCollision = function
if self.y > 170 or self.vy < 0 then return // (jumping, no collision)
cellX = (self.x + td.scrollX) / td.cellSize
if td.cell(cellX, 1) == kBlockTile then
// We hit something!
hitSound.play
print "GAME OVER"
exit
end if
end function
// Main loop
while true
yield
// update the hero
hero.update
// scroll the screen
td.scrollX = td.scrollX + 10
if td.scrollX >= 135*64 then
td.scrollX = 0
end if
end while
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment