Menu, Game, Game Over โ a State Machine
๐ฏ A project with a title screen, a playing state and an end screen, properly separated.
๐ก The idea behind it
Almost every real program is a state machine: it is in exactly one state at a time, and events move it between them. Most messy Scratch projects are messy because nobody told the author this.
๐งฑ Blocks you will need
variable (state)broadcastwhen I receiveif โฆ thenstop this script
๐จ Build it
- Make a variable called state, holding 'menu', 'play' or 'over'.
- Every sprite's forever loop begins with: if state = 'play' โ and does nothing otherwise.
- Clicking Start sets state to 'play' and broadcasts startGame, which resets score, lives and positions.
- Losing sets state to 'over' and broadcasts gameOver, which shows the end screen.
- Nothing anywhere else needs to know about screens. Each part asks only what state the program is in.
๐ Make it harder
- Add a paused state that freezes everything without losing it.
- Add levels as a second variable, and reset positions between them.
๐ Bugs you will hit
- Game logic runs on the menu screen: a loop somewhere is missing its state check.
- Score carries over between games: your startGame handler is not resetting everything it should.
โฌ Need Scratch itself?
Free from MIT, in a browser or as an offline app that needs no connection at all.