Skip to content

Instantly share code, notes, and snippets.

@daltonclaybrook
Created December 24, 2019 19:21
Show Gist options
  • Save daltonclaybrook/3daf0295ad2a83bcab708c6884f052e7 to your computer and use it in GitHub Desktop.
Save daltonclaybrook/3daf0295ad2a83bcab708c6884f052e7 to your computer and use it in GitHub Desktop.
pico-8 cartridge // http://www.pico-8.com
version 18
__lua__
-- this is called automatically
-- one time when the game starts
-- but we'll call it again later
-- to start the game over
function _init()
-- later, when the game is over
-- we'll call this functiom
-- again to set this value back
-- to false
game_over=false
-- how fast the player
-- accelerates downwards.
-- bigger means more gravity.
gravity=0.2
-- how fast the player travels
-- towards the obstacles, or
-- really, how fast the obstacles
-- travel towards the player
-- since the player's x position
-- never changes
speed=2
-- player jump speed. bigger
-- means higher jumps.
jump=2
-- this sets the initial values
-- for the player
make_player()
-- this creates a new pole
-- coming towards the player.
-- the first one gets created
-- here, but others will be
-- created as the pole falls
-- off the left side of the
-- screen
make_pole()
-- play the music track
music(0)
end
-- this is called automatically
-- 30 times per second. used to
-- update your game "state"
function _update()
if (not game_over) then
-- only update positions of
-- things if the game is not
-- over
move_player()
move_pole()
check_hit()
elseif (btnp(5)) then
-- this restarts the game
_init()
end
end
-- this is called automatically
-- 30 times per second, like the
-- _update function, but this is
-- where you actually draw things
function _draw()
-- clear the screen, otherwise
-- stuff from the last frame
-- would still be displayed.
-- the "1" argument is the color
-- blue
cls(1)
draw_pole()
draw_player()
if (game_over) then
print("game over", 46, 60, 7)
print("press ❎ to start over", 22, 68, 7)
end
end
-->8
function make_player()
-- this makes an empty "table"
-- to represent our player.
player={}
-- x position on screen
player.x=12
-- y position on screen.
-- 60 is right in the middle.
player.y=60
-- dy stands for "delta y"
-- "delta" means "change" so
-- this is the current change
-- in y. we set this to "-jump"
-- so the player is initially
-- jumping.
player.dy=-jump
-- index of the sprites in the
-- sprite sheet. change tabs
-- to see all the sprites.
player.flap=6
player.glide=7
end
-- called from _update()
function move_player()
-- since gravity is an
-- "acceleration" force, the "dy"
-- value increases every frame
-- so that the player falls
-- faster and faster
player.dy += gravity
if (btnp(2)) then
-- if the up button (2) is
-- pressed, cancel out the fall
-- speed by resetting the "dy"
-- to the "jump" force. using
-- a negative here because
-- the "y" axis is top to bottom
player.dy = -jump
-- play the "flap" sound fx
sfx(1)
end
-- add the change in y (dy) to
-- the player's current y
player.y += player.dy
end
-- called from _draw()
function draw_player()
-- if "dy" is negative, the
-- player is moving upwards
-- and the bird has "flapped".
-- so draw the "flap" sprite.
-- otherwise, draw the "glide"
-- sprite
if (player.dy < 0) then
spr(player.flap, player.x, player.y)
else
spr(player.glide, player.x, player.y)
end
end
-- called from _update()
function check_hit()
-- this checks if the player is
-- colliding with the top or
-- bottom of the screen
if (player.y < 0 or player.y+8 > 128) then
end_game()
-- this checks if the player is
-- colliding with the pole.
--
-- the outer "if" checks if the
-- player's x position is inside
-- the pole's x position and
-- width.
--
-- if that succeeds, we need to
-- check if the player is inside
-- the "hole"
elseif (player.x > pole.x-8 and player.x < pole.x+pole.width) then
if (player.y < pole.hole_y or player.y+8 > pole.hole_y+pole.hole_size) then
end_game()
end
end
end
function end_game()
game_over=true
-- play the dying sound effect.
-- tweak this in the sounds
-- tab.
sfx(0)
-- stop the music
music(-1)
end
-->8
-- create a new pole
function make_pole()
-- make an empty "table"
pole={}
-- pole's width in pixels
pole.width=16
-- the width of the opening
-- the player must pass through
-- in pixels
pole.hole_size=40
-- starting the pole off the
-- right edge of the screen
pole.x=128
-- set a random y position for
-- the hole.
-- "rnd" returns a number between
-- zero and the provided value.
pole.hole_y=rnd(128-pole.hole_size)
end
-- called from _update()
function move_pole()
-- moves the pole to the left
-- by the "speed" amount
pole.x -= speed
-- if pole has moved off the
-- left edge of the screen,
-- make a new pole
if (pole.x <= -pole.width) then
make_pole()
end
end
function draw_pole()
-- draw the top part of the pole
-- as a rectangle.
-- this function takes arguments
-- for: x,y,x,y,color
-- there are great docs on the
-- internet for all the kinds
-- of drawing calls you can
-- make.
rectfill(pole.x, 0, pole.x+pole.width, pole.hole_y, 3)
-- draw the bottom part of the
-- pole as a rectangle.
rectfill(pole.x, pole.hole_y+pole.hole_size, pole.x+pole.width, 128, 3)
end
__gfx__
00000000000000000110000100aaaa00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
000000000000440000a900090aaaaaa0000000000009a00000099a00000000000000000000000000000000000000000000000000000000000000000000000000
0070070000044740000aaaa9aa0aa0aa00000000009aaa00009aaaa000099a000000000000000000000000000000000000000000000000000000000000000000
0007700000444740990a0aa0aa0aa0aa0000000009aa0a8809aaa0a8009aaaa00000000000000000000000000000000000000000000000000000000000000000
00077000444444409908aaa9a8aaaa8a000000009aaaaa00aaaaaaa009aaa0a80000000000000000000000000000000000000000000000000000000000000000
0070070000444999090a9990aa0aa0aa00000000009aa0000009aa00aaaaaaa00000000000000000000000000000000000000000000000000000000000000000
000000000004400009a9a9a00a0000a0000000000009a00000009a000009aa000000000000000000000000000000000000000000000000000000000000000000
000000000000400000a9009000aaaa00000000000000a00000000900000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000044000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000447400000000000000000000000000009a00000000000000000000000000000000000000000000000000000000000000000000000000000000000
0000000000444740000000000000000000000000009aaa0000000000000000000000000000000000000000000000000000000000000000000000000000000000
000000004444444000000000000000000000000009aa0a8800000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000004449990000000000000000000000009aaaaa0000000000000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000009aa00000000000000000000000000000000000000000000000000000000000000000000000000000000000
__sfx__
000400002e751307513175132751307512d751287512675125751287512a75127751217511b751197511a7511c7511e7511b75117751117510e7510e751107510f7510b751067510075100701007010070100701
000200000e550105501255015550195501f550265501150012500135001550016500195001d500245002f50036500005000050000500005000050000500005000050000500005000050000500005000050000500
011a00001805018055240502405524050230501f0501c0501c0501c0501c0501f050210501f0501c0501c05024050240502405024050240502405024050240502405024050000000000000000000000000000000
010c000024045230451f0451c045240452304524045230451f0451c04518045150451c0451f0452304526045090450c0450e045100451304515045180451a0451d0451c045180451504511045100450c04509045
010c000024045230451f0451c045240452304524045230451f0451c045240452304524045230451f0451c04524045230451f0451c045240452304524045230451f0451c045240452304524045230451f0451c045
010c00001d0451c04518045150451d0451c0451d0451c04518045150451d0451c0451d0451c04518045150451d0451c04518045150451d0451c0451d0451c04518045150451d0451c0451d0451c0451804515045
010c00001104515045180451c045180451c0451f0452304524045230451f0451c0451f045210451f0451c0451d0451c04518045150451d0451c04518045150451f0451c0451a0451c0451d0451f0452104523045
011800100030200302183021c3021c3441c3421d3421c3421c3421c34215342183421834218342183421834500302003020000000000000000000000000000000000000000000000000000000000000000000000
01180010000000000000000000001c3441c3441d3441f3441f3421f342213421c3421c3421c3421a3421834500000000000000000000000000000000000000000000000000000000000000000000000000000000
011800000000200002000020000224342243451f3421f3451d3441d3441f3441c3441c3421c3421a3421834500002000020000200002000020000200002000020000200002000020000200002000020000200002
010c00000c7410c7420c7420c7420c7420c7420c7420c7420c7420c7420c7420c7420c7420c7420c7420c7420c7420c7420c7420c7420c7420c7420c7420c7420c7420c7420c7420c7420c7420c7420c7420c741
010c00000574105742057420574205742057420574205742057420574205742057420574205742057420574205742057420574205742057420574205742057420574205742057420574205742057420574205741
010c00000274102742027420274202742027420274202742027420274202742027420274202742027420274202742027420274202742027420274202742027420274202742027420274202742027420274202741
010c00000074100742007420074200742007420074200742007420074200742007420074200742007420074200742007420074200742007420074200742007420074200742007420074200742007420074200741
010c00000c0730c073000000c00323605000000c0730000023655000000c003000002360500000000000000000000000000c073000000c0730000000000000002365500000000000000000000000000000000000
010c00000c0730c073000000c00323605000000c07300000236550000000000000000000000000000000000000000000000c073000000c0730000000000000002365500000000000c073000000c0730c07300000
__music__
01 04470a4e
00 03480d4f
00 05470b0e
00 06490c0f
01 040a070e
00 030d080f
00 050b070e
02 060c090f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment