New demo - Menus and changing game state

Hey all - I added a new Lua Demo for the PlayDate. This one includes three game screens - a Title Screen, a Help Screen, and a Game Screen. This helps show how to write code for three different parts of a program and keep it relatively clean. You can get the source code here:

If you're wanting to use this code (and you can for anything, don't even have to give me credit), you'll mostly just update the "screen_title.lua", "screen_help.lua", and "screen_game.lua" files. You can pretty much keep "main.lua" the same and you only need to change "state_manager.lua" if you are adding additional states.

The Anatomy of a State

I included a TEMPLATE STATEthat you can use to build additional states. The Title and Help states are menu-based and has a rudimentary cursor that can scroll vertically. The Game state has a very rudimentary sample game written, including smooth directional movement, so you can expand your game within this state.

Each state must have the following functions to operate with the state manager properly:

  • Init - Each time the state begins again, this is what all the starter values of your variables should be.
  • Update - This gets called every cycle, handling collision detection and whatever other game updates should happen here.
  • Draw - This gets called every cycle, add your drawing functions here.
  • Cleanup - This gets called when the state is being left, you can free up memory in here.

At the bottom of the template there are also functions for handling each keypress update (button press and button release for up, down, left, right, A, and B). You can code within these functions to handle those events.

Game State

If you look at the "screen_game.lua" file you'll see the functions above as well as additional helper functions and the gameScreen table also contains additional variables, such as the images this state uses, a table to store the current button states, and a rudimentary player and poop object table.

Helper functions here include GetDistance and MovePlayer.

The basic game written lets you move a little smiley face guy around the screen. If you "collect" the poop then you will die and return to the title screen.

Title State and Help State

These two states are menus. The Title State has two buttons: Play and Help. There is a simple cursor, and it has a "yPositions" table of positions it can be at. These are the y positions of the two buttons. It also has a "selections" table which I'm storing strings of what state option 1 and option 2 go to.

The states have a "buttons" table which is a list of button items.

Pressing the B button will "select" one of the two buttons, based on wherever the cursor is currently at.


If you have any questions, feel free to post a message. If you have a request for another demo you'd like to see, also feel free to suggest something!



Files 248 kB
May 05, 2022

Get PlayDate Demos and Source Code

Download NowName your own price

Leave a comment

Log in with to leave a comment.