Skip to content

Instantly share code, notes, and snippets.

@andymasteroffish
Created September 5, 2021 22:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andymasteroffish/1e29e35ca1d80fdb9e2b3e43a0e11004 to your computer and use it in GitHub Desktop.
Save andymasteroffish/1e29e35ca1d80fdb9e2b3e43a0e11004 to your computer and use it in GitHub Desktop.
--[[
1k jump
by andy wallace
@andy_makes
andymakes.com
you can play the game here: https://andymakes.itch.io/1k-jump
that page has the original 1013 byte code as well
this is a breakdown of the code where i add whitespace and a lot of comments
with one or two small exceptions, this code is identical to the 1024 byte version
i made this game as an entry for pico-1k jam
hosted by paul nicholas (@liquidream)
https://itch.io/jam/pico-1k
thank for setting it up!
if you dig this, you might like my #tweetcarts, which are just 280 chars
https://twitter.com/search?q=%40andy_makes%20%23tweetcart&src=typed_query
]]
--setting color palette
--7 is the "active" color for the game. all live elements will use it
--13, 136, 141 are the background colors that spread while the game is played
--the title and game over also use 5 for gret, but i got lucky in that i had exactly 4 earlier colors so i did not need to add it to my palette
pal({7,13,136,141},1)
--the number of resets since the game started
--this is used to kick the game into the title screen when it first launches
a=0
--shortening some function names
b=btn
r=rnd
--reset game values
::_r:: --this flag is used by a goto when the player presses x to reset
g=1 --game/game over. 1 means playing, 0 means game over
t=0 --time, goes up by 1 each tick
n=0 --time for next platform spawn
s=0 --score
w=128 --width of the screen
--player values
x=64
y=60
v=-1 --y velocity
--array for the platforms
o={}
--increment the number of attempts
a+=1
--play a beep. this is weird! it's a print function that plays audio via an escape char!
--https://www.lexaloffle.com/dl/docs/pico-8_manual.html#audio_
?"\ab"
--clear the screen
cls()
--game loop
--this flag is used by a goto to return to this point and create frames
::_::
--"clearing" the screen by setting pixels to black
--this affects 1500 randomly selected pixels each frame
for i=0,1500 do
--grab a random x and y position
d = r(w)
e = r(w)
--see what color it is
c = pget(d,e)
--if it is the base color, set it to 0 (black)
if(c==1) pset(d, e, 0)
--if it is one of the background colors, expand it to a nearby pixel
if(c>1) pset(d+r(2)-1, e+r(2)-1, c)
end
--main physics/game loop
--the runs m times per frame, allowing the game to get faster over time
--the game speeds up every 7 points
--the maximum is 12 ticks per frame (i've never gotten this far)
--if g is 0 (game over), the physics loop will not run at all
m = min(12, 2+s/7) * g
for j=1,m do
--move player x position from button input
--based on https://gist.github.com/kometbomb/7ab11b8383d3ac94cbfe1be5fb859785#example-how-to-move-left-and-right-in-minimal-chars
--max(.3,-v*1.2) modulates the speed based on the y velocity
--the player moves faster when going up
if(b()>0 and b()<3) x+=(b()*2-3) * max(.3,-v*1.2)
--update y position of the player
v+=.01 --gravity
y+=v --add the velocity to the y position
--go through all platforms
--z represents the platform object for each iteration of the loop
for k,z in pairs(o)do
--move platform up
z.y-=.14 --this value is very small because it will happen m times per frame
c=1 --assume the platform will use the default color
--rectangular hit detection on the player
if abs(x-z.x)<9 and abs(y-z.y)<7 then
--increase the color to one of the background colors
c = 2+s%3
--set the player velocity go upward
v = -0.9
--increase the score
s+=1
--play a little sound
?"\as"..flr(m).."c.e.-g"
end
--draw the rectangle for the platform
--this happens multiple times each frame, but that's fine
rect(z.x-4,z.y-3,z.x+4,z.y+3,c)
--if the color is above 1, it got set when the player hit the platform and we can remove it from the list
if(c>1) del(o,z)
end
--t is the timer, n is the time for the next spawn
--if t is greater than n, it's time for a new platform
if t>n then
--add a new platform at a random x position and a y that is just past the bottom of the screen
add(o,{x=r(128),y=w+4})
--increase the next time to spawn a platform
--as the game goes on and m gets bigger, the random range increases
n+=120+r(m*15)
end
--increase the timer
t+=1
end
--end of the physics loop
--this next part deals with drawing the title/instructions and player
--the decoration escape string that multiple text draws use
--https://www.lexaloffle.com/dl/docs/pico-8_manual.html#rendering_mode_options__
f = "\#5\^t\^w"
--although g controls if game is over, i set this based on if the player's y is greater than 128 or lower than 0 (meaning they are off screen)
if y>w or y<0 then
--if g is still 1 and this isn't the first time we've reached this screen (a>1) play a little sound
if(g>0and a>1) print("\af#..f#..e7..e7")
--set g to 0 to make it game over
g=0
--a lot of this text uses the "?" version of the print command
--you can find some info about it here: https://demoman.net/?a=optimizing-for-tweetcarts
--draw the score using the formatting saved as f earlier
?f..s,57,48,1
--draw the instructions
?"\#5⬅️ & ➡️ to move",35,72
?"\#5❎ to start",43,82
--if the user presses x, go to ::_r:: to reset the game
if(b(5)) goto _r
--things we draw if the game is still going
else
--the player is a mess of trig to allow it to spin, so i shortened the names of cos() and sin() to save chars
c=cos
z=sin
--draw a dot at the top of the screen randomly. this is to show the dangerous border on the top of the screen
?".",r(128),0,1
--drawing a player that rotates as it moves was the last thing i did
--it's a little sloppy because i had some chars to spare and thought it would be fun
--this is a mess of bad variable names. pretty much just the next single letter name i had available
e = -x/30 --the current angle to rotate the player. based on their x position
j = x+c(e)*3 --x1 for the line that makes the player's arm
k = y+z(e)*3 --y1 for the line that makes the player's arm
u = x-c(e)*3 --x2 for the line that makes the player's arm
h = y-z(e)*3 --y2 for the line that makes the player's arm
--draw the line for the player arm
line(j,k,u,h)
--the "body" is a set of lines that run perpendicular to the arm line
--first, change the angle to add 90 degrees (pi/2 radians)
e += .25
--this loop draws lines from 25% of the arm line to 75%
for i=-1,1,.25 do
p = .5 + i/4 --get the percentage
--get the x and y position of the base point along the arm line
--more bad variable names
l = p*j + u*(1-p) --x
m = p*k + h*(1-p) --y
--by default, the vertical line for the body will extend 3 pixels in either direciton
d=3
--if this point is in the center, i don't have it extend down
if(abs(i)<.8) d=0
--draw a vertical line around the center point from the arm line
line( l+c(e)*3, m+z(e)*3, l-c(e)*d, m-z(e)*d )
end
--give the player a head in the form of a circle
circ( x+c(e)*4, y+z(e)*4, 2)
--draw the score in the top left of the screen
?s,1,8
end
--if the game just started, instead of playing i want to be on the title screen
--this is a slight mod on the game over screen
--if a is 1, that means the game just started and we should end the round
if a<2 then
--move the player off screen to kill them
y=-1
--write the game title over the score
?f.."\#41k jump",37,48
--add in some credits so we know who made this thing!
?"by @andy_makes",36,99
end
--draw everything to the screen
flip()
--return to the ::_:: and reapeat everything for next frame!
goto _
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment