How to make a PICO-8 game: Part 5

How to make a PICO-8 game: Part 5

Now that things can collide and the player can effectively die (see last time), the next job is to score some points. Doing this is surprisingly easy, and so this lesson is going to be very short.

Part 5: Score!

For the purposes of this game, simply staying alive is how you score points. Maybe we’ll add bonus points or something later, but for now, the player will get one point for every frame they stay alive.

We need to do three things: set an initial score, update that score, and draw the score on the screen. Hopefully you can see where this is going.

In _init(), we’ll initialise the score to zero:

score = 0

Then, in _update(), we’ll increment it (so it goes up by one point each time _update() is called. We’ll do that at the end of _update() in case we die that frame:

score += 1

And finally, we’ll draw this on the screen in – you guessed it – _draw(). We need to draw it after we clear the screen with cls (or we won’t see it), but it’s an artistic choice as to whether our sprites are “on top” of the score, or the score is “on top” of the sprites.

Of course, we could have a status area on the screen for this (where the gameplay area won’t encroach), but for now we’re keeping it simple. Let’s print it at 100,0 so it’s in the top right but has space to allow for multiple digits in case we’re that good, and in colour 7 (white):

print(score,100,0,7)

And that’s literally all there is to it.

I said it was going to be short! Let’s catch up with the code so far:

function _init()
 -- player start location
 x = 64
 y = 64
 -- enemy start location
 ex = 120
 ey = 120
 -- enemy "speed"
 d = 0.05
 -- score
 score = 0
end

function _update()
 -- check for collision
 if checkcol(x,y,ex,ey) then
  stop()
 end

 --[[ check button presses and screen edge
  btn(0) is left
  btn(1) is right
  btn(2) is up
  btn(3) is down
 ]]
 if btn(0) and x > 0 then
  x -= 1
 end
 if btn(1) and x < 120 then
  x += 1
 end
 if btn(2) and y > 0 then
  y -= 1
 end
 if btn(3) and y < 120 then
  y += 1
 end

 -- move enemy
 if x < ex then
  ex -= d
 end
 if x > ex then
  ex += d
 end
 if y < ey then
  ey -= d
 end
 if y > ey then
  ey += d
 end
 
 -- increment score
 score += 1
 
end

function _draw()
 cls(2) -- clear the screen to mauve
 print(score,100,0,7) -- print score
 spr(1,x,y) -- draw sprite 1 at x, y
 spr(2,ex,ey) -- draw sprite 2 (enemy) at ex, ey
end

function checkcol(ax,ay,bx,by)
 if bx+8>ax and bx<ax+8 and by+8>ay and by<ay+8 then
  return true
 else
  return false
 end
end

 

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.