<- /posts

learning pico-8, part 2

Buggy user movement

In the previous post, we were able to correctly register user inputs and move the rectangle sprite accordingly. But we have a minor bug in our program. Whenever the user moves, we are not checking if the user should move. What do we mean by this? In the original game of Pong, users were confined to a small box, similar to what PICO-8 provides! Our program is allowing the user to move the rectangle freely, but we shouldn’t allow that.

Handling this is an easy fix. We should check if the sprite has reached an edge in the screen for us to allow movement. In our code we can include additional flags whenever the button is being pressed, like so:

FUNCTION _INIT()
  Y=50
END

FUNCTION _UPDATE()
  IF Y>0 BTN(2) THEN
    Y=Y-1
  END

  IF Y<127 BTN(3) THEN
    Y=Y+1
  END
END

FUNCTION _DRAW()
  SPR(0, 10, 10)
  SPR(1, 20, Y)
END

PICO-8 provides a 128x128 pixel grid for us to interact with (counting from 0 to 127). Now whenever we move our rectangle, it moves and stops at the edges, but now there is another visual bug. Part of the rectangle is allowed to go over the screen edges.

We will handle this by telling PICO-8 that the sprite has a specific height. Taking the height into consideration will allow us to properly handle the collisions between the rectangle and the screen edges.

PICO-8 will draw sprites starting from the top-left corner and we are only tracking that poiont whenever we render sprites. That means that the engine is not aware of the sprite’s real dimensions since it renders (at least by default), an 8x8 sprite with no real “dimensions”.

We will