1 of 16

Coding Mario

STEAM Workshop: Game Logic

2 of 16

snap.berkeley.edu

Import project from http://tinyurl.com/jm82fpc

Please save your work on your computer.

3 of 16

I’ve put in some sprites.

4 of 16

What We’re Making Today

5 of 16

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.

6 of 16

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

7 of 16

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?

8 of 16

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.

  • If we press up, we jump, which means we move up and then down.
  • If we press right, we just move to the right.

Where is (-200, -130) on the stage?

-200 200

150

-150

9 of 16

Moving

What goes up must come down

What does change y by 75 look like?

75

-75

10 of 16

Gameover is a state that our game is in.

When Mario touches a pipe.

What do you think this means?

  • I.e. if touching pipe? Is True, and touching pipe2 is False, what does this statement evaluate to?
    • True, because if any part is True, the entire thing is True

When do we have one?

11 of 16

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.

12 of 16

Gameover Sprite

We hide the gameover image, until gameover = true

Then it pops up!

  • With an opacity
  • Centered at (0, 0)
  • Slightly larger than its original size

13 of 16

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.

14 of 16

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.

15 of 16

What you Learned...

  • Model-View-Controller
  • Boolean
  • Computer States
  • Game Events
  • Basics of a graphic user interface (in this case, the game!)

16 of 16

Congrats!

You’ve made a game!

You did it!