A SIMPLE
SCRATCH GAME
(Animal
Olympics)
M. Meyer
Brooklyn College
BACKGROUND
I. Introduction:
In our last lecture we talked about the
evolution of high-level programming languages and discussed popular programming
paradigms. We discussed in some detail three of the most popular paradigms:
Today you will create a simple game in Scratch
using the concepts behind these paradigms.
II. Ludology & Funativity:
As you create the game you should make a
determination on whether or not YOU think the game is fun. If you think
it IS fun, why? If you think it IS NOT fun, why? We talked about abstract
(psychological) things that make a game fun and concrete (physical) things that
make a game fun. As you create the game, ask yourself how the following
concepts are demonstrated (or NOT demonstrated):
Abstract (things that make a game fun):
- Spatial Relationships
- Pattern Recognition
- Social Interaction
Concrete (things that make a game fun):
- Goals (Numerous, Clear, Accomplishable)
- Rewards and Punishments.
- Choice (or the illusion of choice).
PLANNING THE GAME
1. Description of Game:
(Never start a game by sitting down and coding.
Every large programming project needs to be planned out, in as much detail as
possible BEFORE you start coding. The more time, effort and detail you put into
your planning stage, the faster the coding of the game will actually go.)
The game will be called Animal Olympics.
In the game a cat (the player) will attempt to win a variety of track and field
competitions, in order to be declared Olympic champion. The player will control
the cat using the keyboard.
In the first iteration of the game (what this
tutorial covers), we will create the 100 meter dash part of the game. In this
part of the game the player will run a race. To make the cat run the player
must alternately push the left and right arrow keys. If the cat reaches the
finish line first the cat wins.
2. Programming Outline
Plan, plan, plan, before you write a single
line of code! A programming outline will help you see problems, and help
prevent you from making mistakes
Object (Name,
Description) |
Properties (What
are the facts about this object? What
does the object look like? How many images will you need for it? Where does it
start? What are its states (alive, dead, etc.) |
Functions (What
does this object do? Can it
move? Can it change costumes? Can it interact with other objects? Can it
interact with the player?) |
Stage (the
stage itself, which will have a title and a gameplay screen). |
Stage will have two backgrounds. Stage will have two variables: canGo catWin both of these will be set to 0 at start. |
Stage will switch backgrounds, play a sound,
set canGo to 1 and broadcast a startgame message to start the actual
game. |
Cat (the
player) |
Will have two costumes so that it can appear
to run. Will have one variable: lastKeyWasleft which will be use to control player input. |
The cat will move and change costume when the
player alternates between pushing the left and right arrow. |
Bat (the
opponent AI) |
Will have two costumes so that it can appear
to fly. |
Will automatically change costume and move
left to try and beat the player. |
Finish Line (will
determine who wins, based on who touches it first). |
Has only one costume. |
Will detect whether the bat or the cat
touches it first, it will then set the canGo variable to 0, stopping the
game, and set the mouseWin variable to 1 if the cat was the first to touch
it. |
CREATING THE GAME
1. Creating the Objects:
By looking at the programming outline, you should be able to see that we need four objects. In SCRATCH these objects are called Sprites.
Racing Game: Use left and right arrows to make
the Cat run. Get to the finish line before the bat.
Click on the sounds tab, and then click on
Import.
Click on the Music Loops folder and
choose the GuitarChords1 sound file. Then click OK.
2. Sequence
The code in SCRATCH is put together using
blocks from the library and is assembled in the Scripts area for each Sprite.
The scripts that are created are read from top down once they are started. We
need a script for the stage, that will initialize all of the variables to 0 (by
convention, 0 means NO and 1 means YES), display the title screen, play a
sound, wait 10 seconds, and then switch to the playscreen. Click on the stage
icon in the sprites area, and then click on the Scripts tab in the center
window. Then drag and drop the code blocks from the library on the left of the
screen, until your script window looks like this (YOU DO NOT NEED TO ADD THE
COMMENT BOXES):
Once your script looks like the example above,
click on the green flag in the top right of the screen to test it.
2. Procedures (messages)
We need a way for different objects to communicate
with each other in the game. As an example, after the Stage sprite has switched
to its playscreen background we would like it to send out a signal to all of
the other sprites that the game can now start. Add the following code to your
script in the Scripts window of the Stage sprite.
Once your script looks like the example above,
click on the green flag in the top right of the screen to test it.
2. Selection and Repetition
Select the bat sprite. He needs to be put in his
starting place and hidden when the green flag is clicked. Then he needs to be
appear, and start flying left when the game actually starts. Make your code
look like this:
Once your script looks like the example above,
click on the green flag in the top right of the screen to test it.
2. User Interaction
Select the cat sprite. He needs to be put in
his starting place and hidden when the green flag is clicked. Then he needs to
appear and respond to the users input (pushing the left and right keys) when
the game actually starts. Make your code look like this:
Once your script looks like the example above,
click on the green flag in the top right of the screen to test it.
2. Collision Detection
The finish line needs to know who hits it, so
that it can stop the game and determine whether or not the cat won. Make your
code look like this:
Making
the Game Better:
What could be done to make this game better and
more fun to play? Trying adding some more things to this game:
- Add a screen at the end that tells whether or
not you won or lost.
- (Hint: Use the gameOver broadcast and the
catwin variable).
- Add a difficulty setting.
- (Hint: Create a variable to specify how long
the bat pauses between moves).
- Add another opponent.
- Make the new opponent start slow, but get
faster as the race progresses.
- Add a start button after the Title Screen.
- Add some more sounds (running, wings
flapping, etc.)
- Add a second race (a second level that will
start after a secondlevel broadcast is made).
- Give the cat the ability to jump.
- Add hurdles on the course that the cat has to
get over.