Coding Mario
STEAM Workshop: Game Logic
snap.berkeley.edu
I’ve put in some sprites.
What We’re Making Today
Controls
When we press space, we start the game.
We want to continuously check for updates, so we use this forever loop.
What we do in the forever loop will be different for every sprite.
Model-View-Controller Framework
Model - our script logic (when gameover do this…, move x steps, etc.)
View - our stage visual output
Controller - our controls - the spacebar, the up key, etc.
Commonly used in industry
When spacebar is pressed...
We want all of our sprites to do something.
What do we want Mario to be able to do?
What do we want the pipe to do?
When do we want the game over screen to be triggered?
Mario
We want Mario to be able to jump and move around.
Usually we use the direction keys for that.
So let’s have our sprite move when we hit up and right.
Where is (-200, -130) on the stage?
-200 200
150
-150
Moving
What goes up must come down
What does change y by 75 look like?
75
-75
Gameover is a state that our game is in.
When Mario touches a pipe.
What do you think this means?
When do we have one?
Game Over
How do we keep track of a gameover?
We can have a variable called gameover.
In computer science, this sort of variable is called a boolean.
Booleans are variables that are either true or false.
Gameover Sprite
We hide the gameover image, until gameover = true
Then it pops up!
What do we want the pipe to do?
How do we get it to fly out of the screen?
We move it in that direction until it reaches a point off of the stage.
Then we can reset it’s position and start all over again.
*Bonus: we can also use a randomizer function to randomize the speed at which it travels.
What I didn’t implement
How does the game finish if you make it to the other side?
That is something I’m going to leave up to you.
Maybe have Mario say “You’re done!”
Maybe import a screen that says Congrats! And hide/show it using the same logic as we had for the Game Over screen.
What you Learned...
Congrats!
You’ve made a game!
You did it!