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.