Arduboy Course - Part II: Movement
date
Jun 4, 2024
type
KnowledgeBase
year
slug
arduboy-basics-2
status
Published
tags
Arduino
C++
Microcontroller
summary
Let’s make things move and work our way towards something we could call a “game”!
Previous Chapter: Part I: BASICS
Ok, with all the basics out of of the way, let’s make something!
Movement
Let’s make a circle we can move via the up/down/left/right buttons!
- Create variables for the
xandyposition of our circle
- Check for button input and increase/decrease
xandyaccordingly
- Check that the position can’t go off-screen
- Draw a circle at the current
xandyposition!
Here’s the result:

Challenges
- Can you change it so the circle moves faster?
- Can you make it so the circle’s radius can be changed with the
AandBbuttons?
Movement via Momentum + Gravity
The previous example changed the position directly. But now we want to change the program so that movement is based on momentum!
- When we press a button we no longer change the
xandyposition directly
- Instead we change
xMomentumandyMomentum
- Then we use
xMomentumgandyMomentumto change thexandyposition
- If the user doesn’t press
LEFT_BUTTONorRIGHT_BUTTON, thenxMomentumshould decrease towards0.0
- If the user presses
A_BUTTONwe add an upwards force toyMomentumto make them jump
- And we add
gravityas a constant downward force that acts onyMomentumand pushes the player towards the ground

Challenges
- Can you make it so the player can only jump when in contact with the floor?
🎓 Making it Frame-Rate-Independent
Arduboy tries its best to keep running at a stable frame-rate. So unless you’re in danger of dipping below the set framerate, you can ignore all this for simplicity’s sake. But be aware that in other game engines you usually run at variable frame-rates and very much have to account for that!
The previous example will behave different at different frame-rates. You can try this out by changing the setup function to this:
A low frame-rate like
20 brings it to a crawl, a high frame-rate like 120 puts it on steroids. But ideally we’d like it to behave the same no matter the frame-rate ends up being!To achieve this we need to
- Calculate the
deltaTime- the actual time that passed since the previous frame
- Set all our speed values in pixels per second and then multiply them by the
deltaTimewe calculated

➡️ Continue in Part III: Sprites