Do you love playing video and computer games? Do you dream of someday creating your own? With this science fair project you can turn that "someday" wish into a "today" reality!
Objective
To program a simple video game that can be customized, as desired, and to determine the factors that affect the game's score.
Introduction
How would you like to make your very own video game? Sound impossible? Well it isn't! This science fair project will walk you through the steps of creating a simple video game. And once you've done that, you can take what you've learned and use it to come up with a game idea of your own, modify the game you've already created, or just sit back and enjoy playing your creation from this science fair project.
Video games are basically made up of a set of instructions, called a program, which tells the computer (or game console) what to do. The instructions include information like what characters, or other art, to display on the screen; what should happen when the player presses different buttons; and when specific sounds or songs should be played. If you were giving a set of instructions to a person, you'd probably write them down using a language that the person understood. For example, the Experimental Procedure for this science fair project is a list of instructions, written in English, telling you how to do this project. Similarly, programmers (people who write programs) use special languages to write computer instructions. These programming languages are specifically built to take advantage of how computers process information. Just as there are many different human languages, there are also many different programming languages. Different programming languages have different advantages and disadvantages—some are good for telling computers how to display images, while others are better at allowing the computer to do mathematical calculations quickly. Programmers choose which programming language to use based on the tasks they want the computer to perform, and which languages the programmer knows.
Figure 1. This program, in Scratch, has two scripts written for the cat sprite.
In this science fair project you'll create a video game using an easy-to-use programming language called ScratchTM. Scratch was created by researchers at the Massachusetts Institute of Technology (MIT) and is specifically made for beginning programmers—so even if you've never programmed anything before, you'll be able to use Scratch! Scratch uses blocks (simple instructions), which you can hook together, by dragging and dropping with your mouse, to make longer and more complex instructions. Each set of blocks is called a script. In the Scratch program shown in Figure 1, above, there are two scripts—one that instructs the computer to make the cat "walk 10 steps when the player hits the right arrow key" and another script that instructs the computer to "play a meow sound when the player hits the space bar."
The game you will create in this science fair project is a game of chase between a cat and a dog. The cat will be controlled by the player, and the dog will be controlled by the computer. The game will end when the dog catches the cat. Ready to program? Let's get started! The references in the Bibliography and in the Experimental Procedure, below, will teach you all the programming skills you need to know to make this game. Once you've made the game, you may surprise yourself with all the fun additions and variations you can add to the game using your new programming skills!
Terms, Concepts, and Questions to Start Background Research
Program
Programmer
Sprite
Scratch
Script
Variable
Tune
Ratio
Constant
Code
Questions
What is an "if" statement? There are two types of "if" blocks in Scratch an "if-then" block and a "if-then-else" block; how do they differ?
What are the different ways you can get an action (or series of actions) to happen repeatedly in Scratch?
What is a variable and how are variables used in programming?
Bibliography
These resources will introduce you to Scratch:
Scratch Team, MIT. (n.d.). Scratch. Retrieved January 23, 2009, from http://scratch.mit.edu/
Disclaimer: Science Buddies occasionally provides information (such as part numbers, supplier names, and supplier weblinks) to assist our users in locating specialty items for individual projects. The information is provided solely as a convenience to our users. We do our best to make sure that part numbers and descriptions are accurate when first listed. However, since part numbers do change as items are obsoleted or improved, please send us an email if you run across any parts that are no longer available. We also do our best to make sure that any listed supplier provides prompt, courteous service. Science Buddies receives no consideration, financial or otherwise, from suppliers for these listings.
(The sole exception is any Amazon.com or Barnes&Noble.com link.)
If you have any comments (positive or negative) related to purchases you've made for science fair projects from recommendations on our site, please let us know. Write to us at
scibuddy@sciencebuddies.org.
Experimental Procedure
The instructions below divide the process of programming this video game into several manageable pieces. For each piece, you'll be given programming objectives and told which blocks are necessary to create the program. It'll be your task in this science fair project to figure out how the blocks should be hooked together to achieve the programming goals. Once the game is running, you'll make final changes to the game to improve the gameplay experience.
Installing and Getting Started with Scratch
To start this science fair project, you should go to the Scratch download page at scratch.mit.edu/download and follow the instructions there to download and install Scratch for free. If you need help installing it, read the Science Buddies Installing & Getting Started with Scratch page. This short guide will familiarize you with Scratch.
If this is your first time using Scratch or if you need a refresher, read the Science Buddies Scratch User Guide.
Programming the Cat
When you open Scratch, the default screen is a project file that already contains one sprite (all characters and objects in Scratch are referred to as sprites). The sprite for this game is a cat, which will be the character that the player controls during the game.
The cat sprite takes up a large amount of screen space. To make sure there is enough space on the screen to have the dog and the cat running around, you'll need to shrink the size of the cat sprite.
Click on the "Shrink sprite" button in the toolbar (it's near the top right, with four arrows pointing inward). The button will turn a faint blue color when it is "active." Note: If you are ever confused about where something is on the interface (the screen), look at the Scratch interface explanation in the Scratch Reference Guide (found on the Scratch Support page).
Once the "Shrink sprite" button is active, move your cursor over the cat and click several times—you will see the cat shrinking.
Shrink the cat until it is approximately 1 inch tall when you see it in presentation mode.
Press the "enter presentation mode" button to enter presentation mode.
Now you're ready to add blocks to control how the player moves the cat around the game screen. The order of the scripts does not matter, as they will all happen simultaneously. Use the blocks shown in Table 1, below, to create the following movements. Experiment with the blocks until your cat can do the following movements:
Once the game is started (i.e. the green flag is clicked), the cat is always in motion.
When the up arrow key is pressed, the cat will change direction so that it moves up.
When the down arrow key is pressed, the cat will change direction so that it moves down.
When the right arrow key is pressed, the cat will change direction so that it moves to the right.
When the left arrow key is pressed, the cat will change direction so that it moves to the left.
Table 1. All the blocks you need in order to add basic movement control to the cat are listed in this table.
Block
Type of Block
Description
Control
Runs script below it when green flag is clicked.
Control
Runs script below it when specified key is pressed.
Control
Runs the blocks inside over and over.
Motion
Points sprite in the specified direction.
Motion
Moves sprite forward or backward when you hold down the assigned key. The number of steps can be changed. Increase the number of steps to make a sprite move faster; decrease the number to make a sprite move slower.
Motion
Moves sprite to specified x and y position on the screen.
When playing a game, it is nice to know that each time you start the game (which is the same as pressing the green flag in Scratch) the player character (the cat in your game) will start in the same place. Modify your cat script(s) so that the cat always starts in the middle of the screen. The block(s) necessary to do this are also listed in Table 1.
Programming the Dog
In your game, the dog will be the computer-controlled character that chases the cat. To create a dog sprite, go to the area containing the new sprite buttons, just below the gameplay screen, and click on the "Choose new sprite from file" button. Figure 2, below, shows the location of the button. From the menu that appears, choose animals (under costumes) and select any dog sprite that you want.
Figure 2. Clicking on the new sprite button (circled in red) will cause a menu to open from which you can choose sprites. The dog sprite is located in the "animals" folder in the costumes section.
Using the same method as described in step 2 of "Programming the Cat," shrink the dog sprite until the cat and dog are similar sizes.
The programming goal is to have the dog chase the cat continuously. The blocks you'll need to do this are shown in Table 2. Experiment with the blocks until your dog:
Is always in the same starting position at the beginning of the game.
Continuously moves toward the cat, once the game is started.
Table 2. All the blocks needed to program the dog are listed in this table.
Block
Type of Block
Description
Control
Runs script below it when green flag is clicked.
Control
Runs the blocks inside over and over.
Motion
Points sprite in the specified direction.
Motion
Moves sprite forward or backward when you hold down the assigned key. The number of steps can be changed. Increase the number of steps to make a sprite move faster; decrease the number to make a sprite move slower.
Motion
Moves sprite to specified x and y position on the screen.
Programming the End of the Game
The goal of the game is for the player to keep the cat away from the dog—when the dog "catches" the cat, the game is over. The next few steps will help you create sprites and scripts to make the game end when the cat and dog sprites touch.
When the game ends, both the cat and the dog should stop moving. This means you need to tell the program, by editing an existing script, that all the scripts that are currently running need to end. You could place these instructions in either the cat or in the dog's scripts area, but for this science fair project, you'll put it in the cat's script area. All the blocks you need for this step are in Table 3, below. Experiment with the blocks until:
All the scripts will end if the cat and dog sprites touch. As long as the sprites do not touch, the cat will keep moving, exactly as you programmed in the "Programming the Cat" section above.
Table 3. All the new blocks needed to program the end of the game are listed in this table.
Block
Type of Block
Description
Control
Runs script below it when green flag is clicked.
Control
If the statement inside the hexagon is true, then the program will do the actions underneath the "if" part of the block. If the statement is false then the program will do the action underneath the "else" part of the block.
or
Control
Sends a message to all sprites, triggering them to do something, and then either continues with the next block without waiting for the triggered scripts or waits until the triggered scripts all finish before continuing with the next block.
Control
Stops all scripts in all sprites.
Control
Runs script below when it receives the specified broadcast message.
Looks
Makes sprite appear on the stage.
Looks
Makes sprite disappear from the stage.
Once the cat and dog sprites touch, you need to communicate to the player that the game is over. The blocks needed to do this are also listed in Table 3.
Go to the area containing the new sprite buttons and click on the "Paint new sprite" button.
Using the tools you find there, create a text sprite, which reads "Game Over."
Now you need to create scripts that keep the "Game Over" sprite hidden until your "game over" conditions are met (i.e. the cat and dog sprites touch). Experiment with the blocks to do this.
Hint: You may find that you have a problem—when the cat and dog sprites touch, all the scripts stop, which means you can't run a script that shows the Game Over sprite. Look up more information about the "broadcast and wait" block. How could this block help solve the problem? Would it go in the scripts for the cat sprite or for the Game Over sprite?
Remember to make sure that the Game Over sprite is hidden when you restart the game after losing.
Programming the Score Board
The last part of the program is a metric by which the player can decide how well he or she is doing in the game. Many games keep track of your score, which allows players to compare how well they do relative to other players, or to other times they've played the game.
Since the goal of the game is for the player (as the cat) to avoid getting caught by the dog for as long as possible, you'll use time, in seconds, as the score. You could put the script for scorekeeping in any of the sprites, but since time is independent of the dog, cat, or Game Over sprites, you'll put it in the stage. To write the script, you'll need to create a variable—time.
Click on the Variables button in the block palette, and then on the "Make a variable" button.
When prompted, type "time" for the name of the variable and click OK.
Once you click OK, you'll see the time variable appear in the block palette and on the screen. If you want to make the variable disappear from the screen, uncheck (by clicking on it) the checkbox next to the time variable.
Once you've created the time variable, you will need to write a script using it. Experiment with the blocks to do the following. All the blocks you'll need for this step are shown in Table 4, below.
Display the time on the screen so that the player can see it. Note: This can either be done by keeping the checkbox checked next to the time variable in the variables menu, or by using a block that instructs the program to show the variable.
When the game is started, time should start at zero and increase by 1 every second until the end of the game.
Table 4. All the blocks needed to program the end of the game are listed in this table.
Block
Type of Block
Description
Control
Runs script below it when green flag is clicked.
Control
Runs the blocks inside over and over.
Control
Waits specified number of seconds, then continues with next block.
Vairable
Shows the variable monitor on the stage.
Variable
Sets the variable to the specified value.
Variable
Changes the variable by specified amount.
Tuning the Game
Congratulations! You've successfully programmed a game! But when you play it, you may find that the game is too easy, too hard, or just not quite "fun" yet. Game makers often have to experiment with the different gameplay variables to make a game feel fun and have just the right level of difficulty, which is called tuning. In this science fair project, there are two gameplay variables that can be tuned: the speed of the dog and the speed of the cat.
Increasing the number of steps a sprite takes makes it move faster. Decreasing the number of steps a sprite takes makes it move slower.
What happens to the score of the game when you increase the speed of both the dog sprite and the cat sprite, but keep the ratio of their speeds constant?
Start with both the cat and the dog moving at a rate of one step.
Play the game at least three times using the "1 step" speed settings. After each round of the game, record your score (which in this case is the number of seconds you can keep the cat away from the dog) in a data table, like Table 5, in your lab notebook. Also take notes on how "fun" you feel the game is using the "1 step" speed settings. Note: If a game lasts longer than 5 minutes, stop and record the game score as >300 (greater than 300 seconds).
Keep increasing the speeds of the cat and dog, but remember to keep the ratio constant; for example, the next speed setting would be 2 steps for each, then 3 steps for each, and so on. For each speed setting, play the game three times and record your scores in your data table. Write down how "fun" you feel the game is at each setting, compared to the other speed settings.
Stop the experiment when you get to a speed setting where the average of your game trials is 5 seconds or less.
Make an XY graph with speed on the x-axis, versus the score on the y-axis. You can make the graph by hand, or use a website like Create a Graph to make the graph on the computer and print it.
What happens to the game score as the speed of the characters increases? What speed setting feels the most fun?
Table 5. Below is an example of what the data table for the speed changes with a constant cat-to-dog speed ratio should look like. Notice that both the cat and the dog's speeds increase, but the ratio between the speeds of the two characters remains constant.
Cat's Speed (# steps)
Dog's Speed (# steps)
Speed Ratio (Cat:Dog)
Game Score (seconds)
Notes on how "fun" the game is:
Trial 1
Trial 2
Trial 3
Average of Trials
1
1
1:1
2
2
1:1
What happens to the score of the game when you change the ratio between the speeds of the two characters?
From your constant speed ratio experiment in step 2, pick the speed setting that you felt was the most fun, and keep the cat's speed fixed at that speed.
Increase the dog's speed by one step. Play the game three times and record your scores in a data table, like the one in Table 6, in your lab notebook. Also take notes on how "fun" you feel the game is.
Keep increasing the dog's speed, one step at a time, retesting, and recording the scores and "fun" factor in your data table. Repeat this step until you reach a speed ratio where the average game score is less than 5 seconds. Remember, do not change the cat's speed.
Once you've reached a speed ratio where the game ends too quickly (5 seconds or less per game), then go back to the dog speed you started with. This time, keep decreasing the dog's speed, one step at a time, retesting, and recording the scores and "fun" factor in your data table. Stop decreasing when either the average game score is greater than 300 seconds or the dog's speed is 1.
Make an XY graph with the speed ratio on the x-axis, versus the score on the y-axis. To graph the speed ratio, you'll need to convert the ratio to a fraction and then to a decimal value. If you're unsure how to do this, ask your math teacher or consult the website about ratios, listed in the Bibliography.
What happened to the game score as the cat-to-dog speed ratio increased? How about as it decreased? Which speed setting was the most fun?
Table 6. Below is an example of what the data table for the speed ratio changes should look like. Notice that the cat's speed remains constant; only the dog's speed increases and decreases.
Cat's Speed (# steps)
Dog's Speed (# steps)
Speed Ratio (Cat:Dog)
Convert Ratio to Decimal
Game Score (seconds)
Notes on how "fun" the game is:
Trial 1
Trial 2
Trial 3
Average of Trials
3
2
3:2
1.5
3
3
1:1
1
3
4
3:4
0.75
Using what you learned from your experiments in steps 2 and 3 of this section, choose the most fun speed settings for the dog and the cat characters. Now you've tuned your game!
You can continue to create more additions to make your game even more fun. For example:
Add sound effects.
Make a fun background (stage) for the game to take place on.
Use the "Costumes" feature and different versions of the sprites to try to animate the dog and cat so that they look like they're running.
Try some of the Variations listed below, or think of your own ideas!
Make the game more complex by adding another item (like a ball of yarn or a fish) for the player (as the cat) to try to collect. Have the item appear in random locations on the screen. Change the scoring system such that the more items a player collects, the more points he or she accumulates.
Modify the game so that the dog's behavior is more complicated. Make the dog wander around randomly until the cat comes "close" to the dog, and then have the dog "chase" the cat. Hint: you'll have to define what "close" is in mathematical terms.
Add multiple levels of difficulty, such that the game becomes more difficult the longer someone plays.
To do this science fair project, you'll need to download and install Scratch, a free programming language from MIT Media Labs. For more information, see the Experimental Procedure section.
Cost
Very Low (under $20)
Safety
No issues
Share this Project Idea!
Donate to Science Buddies
Sponsor
Sponsored by generous support from the AMD Foundation
Level Up from Game Player to Game Designer
AMD Changing the Game inspires youth to learn critical STEM skills by creating their own video games.
If you like this project, you might enjoy exploring
related careers.
Computer Programmer
Computers are essential tools in the modern world, handling everything from traffic control, car welding, movie animation, shipping, aircraft design, and social networking to book publishing, business management, music mixing, health care, agriculture, and online shopping. Computer programmers are the people who write the instructions that tell computers what to do.
Computer Software Engineer
Are you interested in developing cool video game software for computers? Would you like to learn how to make software run faster and more reliably on different kinds of computers and operating systems? Do you like to apply your computer science skills to solve problems? If so, then you might be interested in the career of a computer software engineer.
Software Quality Assurance Engineer & Tester
Software quality assurance engineers and testers oversee the quality of a piece of software's development over its entire life cycle. Their goal is to see to it that the final product meets the customer's requirements and expectations in both performance and value. During the software life cycle, they verify (officially state) that it is possible for the software to accomplish certain tasks. They detect problems that exist in the process of developing the software, or in the product itself. They try and make things not work (try to "break" the software) by creating errors or combinations of errors that a user might make. For example, if a user enters a period or a pound sign for a password, will that break the software? They seek to anticipate potential issues with the software before they become visible. At the end of the life cycle, they reflect upon how problems or bugs arose, and figure out ways to make the software development process better in the future.
Join Science Buddies
Become a Science Buddies member! It's free! As a member you will be the first to receive our new and innovative project ideas, news about upcoming science competitions, science fair tips, and information on other science related initiatives.
Support Science Buddies
If this website has helped you, won't you consider a small gift so we may continue developing resources to help teachers and students?
It's free! As a member you will be the first to receive our new and innovative project ideas, news
about upcoming science competitions, science fair tips, and information on other science related initiatives.