Class 12 - Handling scenes
Play homework
Topics
- Finite State Machines
- Handling states
Description
This week we’ll learn how to structure and handle creating a complex games with various scenes a player can encounter.
Class notes
Finite State Machine Concept
A finite-state machine (FSM) or finite-state automaton (FSA, plural: automata), finite automaton, or simply a state machine, is a mathematical model of computation. It is an abstract machine that can be in exactly one of a finite number of states at any given time. The FSM can change from one state to another in response to some inputs; the change from one state to another is called a transition.[1] An FSM is defined by a list of its states, its initial state, and the inputs that trigger each transition. Finite-state machines are of two types—deterministic finite-state machines and non-deterministic finite-state machines.[2] A deterministic finite-state machine can be constructed equivalent to any non-deterministic one.
The behavior of state machines can be observed in many devices in modern society that perform a predetermined sequence of actions depending on a sequence of events with which they are presented. Simple examples are: vending machines, which dispense products when the proper combination of coins is deposited; elevators, whose sequence of stops is determined by the floors requested by riders; traffic lights, which change sequence when cars are waiting; combination locks, which require the input of a sequence of numbers in the proper order.
The finite-state machine has less computational power than some other models of computation such as the Turing machine.[3] The computational power distinction means there are computational tasks that a Turing machine can do but an FSM cannot. This is because an FSM’s memory is limited by the number of states it has. A finite-state machine has the same computational power as a Turing machine that is restricted such that its head may only perform “read” operations, and always has to move from left to right. FSMs are studied in the more general field of automata theory.
An example of a simple mechanism that can be modeled by a state machine is a turnstile. A turnstile, used to control access to subways and amusement park rides, is a gate with three rotating arms at waist height, one across the entryway. Initially the arms are locked, blocking the entry, preventing patrons from passing through. Depositing a coin or token in a slot on the turnstile unlocks the arms, allowing a single customer to push through. After the customer passes through, the arms are locked again until another coin is inserted.
Considered as a state machine, the turnstile has two possible states: Locked and Unlocked. There are two possible inputs that affect its state: putting a coin in the slot (coin) and pushing the arm (push). In the locked state, pushing on the arm has no effect; no matter how many times the input push is given, it stays in the locked state. Putting a coin in – that is, giving the machine a coin input – shifts the state from Locked to Unlocked. In the unlocked state, putting additional coins in has no effect; that is, giving additional coin inputs does not change the state. However, a customer pushing through the arms, giving a push input, shifts the state back to Locked.
The turnstile state machine can also be represented by a directed graph called a state diagram (above). Each state is represented by a node (circle). Edges (arrows) show the transitions from one state to another. Each arrow is labeled with the input that triggers that transition. An input that doesn’t cause a change of state (such as a coin input in the Unlocked state) is represented by a circular arrow returning to the original state. The arrow into the Locked node from the black dot indicates it is the initial state.
Title Screen or Menu
Initializing
The function “_init()” is what happens when the game is started up (or in this case, the State).
function _init()
titleinit() -- does title things.
end
function titleinit()
mode = 0
--whatever you wish to do when the title starts up.
end
function gameinit()
mode = 1
--what to do at the start of the play session.
end
We’ll call on gameinit() later.
Updating
The function “_update()” is where gameplay elements are changed (like a timer).
function _update()
if (mode == 0) then --Title Screen mode
titleupdate()
else
gameupdate()
end
end
function titleupdate()
--Example "Press Z to start"
--if btn(4,0) then
-- gameinit()
--end
end
function gameupdate()
--gameplay elements (or whatever used to be in _update) goes here.
end
The example given can be used by simply getting rid of the dashes (or rather uncommenting it).
Drawing
The function “_draw()” is called to draw things to the screen. (There’s some repetition in this one.)
function _draw()
if (mode == 0) then --Tit
titledraw()
else
gamedraw()
end
end
function titledraw()
--Example "Title"
--print("Super Cool Game")
end
function gamedraw()
--the screen drawing mechanisms(or whatever used to be in _draw) goes here.
end
Notes
- If you want to add more to “_draw” or “_update”, simply follow this guideline:
else if (mode == 2) then
--optiondraw() or optionupdate()
just be sure to put that before the else statement!
Code homework
For homework this week you will begin working on your final class project
- Start in your notebook with sketching.
- What is the genre(s)?
- What is the goal?
- What are the game mechanics? (movement, controls, special functions, lose state, attacks, etc)
- Sketch sprites and the character
- Draw a flowchart of your basic game loop
- If you have thoughts on the title, write it down
- Note down any questions
Credits
- Finite State Machine by Wikipedians CC BY SA
- Cutscenes and Coroutines (c) Dan Sanderson 2015 CC BY SA
- finite state machine diagram by Chetvorno cc0
- Turnstile image by tournelli 2023 cc by sa
- Interactive Portraits by Zoyander Street