Play game
1k Rally's itch.io pageCompressed Bytes used
1000
Source Code (OPTIONAL)
p={}
for y=63,191do -- go a bit beyong 127 so hills dont get cut off at the bottom
-- camera is 5m behind pixel drawn at bottom of screen and 3m above ground. 127 is the lowest pixel to draw and 62 is the highest — it ought really be 63 to match y but that seems not to work so ok
z=5*((127-y)/(y-63)+1)
p[y]={z=z,s=9/z}end
-- "theoretically" s should be 5/z but boosting it a little makes the angle more dramatic
th={"st","nd","rd","th","th","th","th","th","th","th"}
v=0
z=0
xv=0
px=24
tt=-120 -- game clock
dt=0 -- dead time after crash
nd=1200 -- end of track
-- initialise the engine sound effect. this will also play it but who cares. we just want this to set up the basic metadata for the effect so we don't have to poke it
?"\a0d"
sfx(-1,0)
function _draw()
if z>nd do
rectfill(0,62,127,70,0)
?"you came "..pp,42,64,10
return
end
tt+=1
if tt==-90or tt==-60or tt==-30do
?"\ac3c3c3"
end
if tt==0do
?"\ac4c4c4"
end
t=max(tt,1)
if dt>0then dt-=1elseif tt>=0do
z+=v*(1-abs(xv)/9) -- move the car forwards
if btn(4)do v+=0.013end -- acceleration
v*=0.99 -- friction
if btn(5)do v*=0.9end -- brakes
-- replace the single note in our engine sound effect with a pitch based on our current speed
poke2(12800,512+min(v*60,16)) -- this goes up to 63 but that's far too high for an engine
if abs(px)>48do
poke2(12800,641) -- noise
v=max(v,0.3)
end
if t>10do
sfx(0,0) -- play the sample regardless of whether we actually want to
if v<0.01do v=0
sfx(-1,0) -- and stop it if we don't, which weirdly compresses to fewer bytes than just letting it end on its own and then only playing it when we want it
end
end
if btn(0)do xv-=0.1if xv<-3do xv=-3end end
if btn(1)do xv+=0.1if xv>3do xv=3end end
px+=xv*v
end
cls(12) -- sky blue
c=p[111] -- car y position, normalise stuff to this
dz=z-c.z
ch=sin(z/161)*12 -- 12 is how high hills are, or you could make this a bit smaller so the car does go up and down a bit, that might be cool
cx=c.s*sin(z/100)*32+px
nc=9 --next car index, they stay in order so it's easy
ndd=true
for y,i in pairs(p)do
rz=i.z+dz
d=cos(rz/161)<0 -- show dark palette
x=64+i.s*(sin(rz/100)*32-cx)-xv*(111-y)
yi=y+i.s*sin(rz/161)*12-ch -- 12 is how high hills are
rectfill(0,yi,128,128,d and 3or 11) -- grass
rectfill(x-56*i.s,yi,x+56*i.s,128,rz%2>1and(d and 6or 7)or(d and 2or 8)) -- sideline
rectfill(x-48*i.s,yi,x+48*i.s,128,5) -- road
if ndd and rz>0and rz<=nd do
ndd=false
line(x-56*i.s,yi,x-56*i.s,yi-48*i.s,7)
line(x+56*i.s,yi,x+56*i.s,yi-48*i.s)
fillp(13260) -- checkerboard
rectfill(x-56*i.s,yi-48*i.s,x+56*i.s,yi-36*i.s,7)
fillp()
end
if rz%10<5do rectfill(x-4*i.s,yi,x+4*i.s,128,d and 6or 7)end
-- cx is the "real" x coordinate of the car before any perspective
-- c is the colour
function car(o,c)
rectfill(x+(o-12)*i.s,yi-16*i.s,x+(o+12)*i.s,yi-4*i.s,c) -- body
rectfill(x+(o-10)*i.s,yi-18*i.s,x+(o+10)*i.s,yi-4*i.s) -- top
rectfill(x+(o-8)*i.s,yi-17*i.s,x+(o+8)*i.s,yi-15*i.s,12) -- window
rectfill(x+(o-11)*i.s,yi-4*i.s,x+(o-8)*i.s,yi,0) -- left wheel
rectfill(x+(o+8)*i.s,yi-4*i.s,x+(o+11)*i.s,yi) -- right wheel
fillp(▒)
rectfill(x+(o-12)*i.s,yi-2*i.s,x+(o+12)*i.s,yi) -- shadow
fillp()
end
while nc>0and rz>0and rz<(nc+1)/8*t do
j=cos(t/400)*24*(nc%2==1and 1or-1)
car(j,({2,3,4,10})[nc%4+1])
nc-=1
if y>108and y<114and j<px+22and j>px-22and t>10do
?"\ai6ccc" -- crash noise
v=0
dt=15
end
end
if y==111do
if dt%2==0do car(px,8)end
pp=10-nc..th[10-nc]
end
end
?pp,100,5,10
end
Link to Commented Source Code (OPTIONAL)
https://andrew-t.itch.io/1k-rally/devlog/1037969/annotated-source-code
Leave a comment
Log in with itch.io to leave a comment.
Comments
Amazing work! Looks great and feels great too.
Nice road graphics!