banner



Raspberry Pi Design Your Own Video Game

Design Your Own Video Game with a Raspberry Pi

Abstract

Do you play video games on a console or smartphone? Have you ever wished you had the power to change how a game worked, or even to create your very own game? This project will show you how to make your very own video game and controller using the Raspberry Pi Projects Kit. Check out the video to see what this simple, but fun, project looks like. Of course, you can design the looks and gameplay of your game any way you like!

Credits

Ben Finio, PhD, Science Buddies

Cite This Page

General citation information is provided here. Be sure to check the formatting, including capitalization, for the method you are using and update your citation, as needed.

MLA Style

Finio, Ben. "Design Your Own Video Game with a Raspberry Pi." Science Buddies, 21 Dec. 2020, https://www.sciencebuddies.org/science-fair-projects/project-ideas/CompSci_p059/computer-science/video-game-raspberry-pi. Accessed 19 Nov. 2021.

APA Style

Finio, B. (2020, December 21). Design Your Own Video Game with a Raspberry Pi. Retrieved from https://www.sciencebuddies.org/science-fair-projects/project-ideas/CompSci_p059/computer-science/video-game-raspberry-pi

Last edit date: 2020-12-21

Introduction

A Raspberry Pi (Figure 1) is a tiny computer that you can use to write programs and build and control your own electronic circuits, like the game controller circuit in this project. The circuit uses sensors to gather information from the world around it. You can use information from those sensors to tell your computer program what to do. Many electronic devices we use every day, like phones and video game controllers, use sensors in the same way.

raspberry pi 3B+
Figure 1. A Raspberry Pi computer. (Image credit SparkFun Electronics, licensed under Creative Commons Attribution 2.0).

You can do this project just for fun, or turn it into an engineering or science project. To make it an engineering project, you will need to follow the engineering design process. Start by defining what you want your video game to be able to do, then work through the rest of the design process until you have a completed project to show off at your science fair.

If you want to use this for a science project, you will need to follow the scientific method. Start by coming up with a question for which you can use the video game as a tool to help answer. For example, does age or video game-playing experience affect a person's ability to score highly in the game?

News Feed on This Topic

, ,

Materials and Equipment Product Kit Available

This project is written for the Science Buddies Raspberry Pi Projects Kit. There are two kit options available:

  • Raspberry Pi Projects Kit (if you do not already own a Raspberry Pi), comes with:
    • Raspberry Pi model 3B+
    • Micro-USB power cable
    • HDMI cable*
    • Clear plastic case for Raspberry Pi
    • All the circuit components to do all 8 Science Buddies Raspberry Pi projects
    • 16GB SD card preloaded with Raspbian and quick link to Science Buddies instructions
  • Raspberry Pi Circuit Parts Only Kit (if you already own a model 2B or higher), comes with:
    • All the circuit components to do all 8 Science Buddies Raspberry Pi projects
    • 16GB SD card preloaded with Raspbian and quick links to Science Buddies instructions

To set up your Raspberry Pi, you will also need the following parts (not included in either kit option):

  • TV or computer monitor with HDMI input*
  • USB keyboard and mouse

*If you have a TV or monitor with a DVI or VGA input, you will need an adapter. If your monitor does not have built-in speakers, you will need external speakers or headphones. See FAQ for more information.

To do this specific project, you will need the following circuit parts from your kit:

  • Male-male jumper wires (5)
  • Pushbuttons (2)

Recommended Project Supplies

Get the right supplies — selected and tested to work with this project.

Experimental Procedure

Introduction

Do you play video games? Have you ever wished you could change a feature in a game you have played, or even make your own game? That's exactly what you will get to do in this project! This project will show you how to get started with a simple arcade-style game, but you can add on to the game as much as you want. If you have not already, you will need to set up your Raspberry Pi before you begin. Here is a video of the basic game and some possible additions:

Starting Your Program

If you have never written a program in Scratch before, watch this video before you continue:

Raspberry Pi Scratch 2 Tutorial (Science Buddies Kit)

In this project you will be controlling Scratch characters on screen. The individual characters are called sprites, and the area where they move around is called the stage. Every new program starts out with a blank white stage and a single cat sprite, like this:

raspberry pi video game scratch scree
Figure 2

In this project you will write small sections of code and test them one at a time. This will give you a better understanding of how your video game works, so you can make your own changes later.

The first thing you will do is write a simple program that lets you move the cat sprite around the screen. Create the following program (see the tips below Figure 3 if you need help):

raspberry pi video game scratch screen
Figure 3

  • To create a variable, click Data then Make a variable.
  • The move __ steps and turn __ degrees blocks can be found under Motion.

Now, run your program by clicking the green flag. The cat should continuously move forward, while you use the arrow keys to steer it left and right. Try stopping your program, entering different values for the cat_speed and cat_turning variables, then running the program again. Experiment with a few different values until you feel like you have good "control" of the cat. For example, if the cat moves too fast to control easily, then decrease the speed. If it takes too long to turn, increase the turning angle. There are no right or wrong numbers; it is a matter of personal preference.

Finally, the rectangular blue button in the upper-left corner of the stage can be used to make the stage full-screen. You can use this button to make the game full-screen when you play, then click it again when you need to edit your program.

raspberry pi video game scratch screen
Figure 4

Adding a Second Sprite

Remember from the introduction video, above, that there are two sprites in the game, the cat (which you control), and a dog, which automatically chases the cat. Next, you will need to create the dog sprite and give it its own program (each individual sprite's program is called a script). Click the Choose sprite from library button next to "New sprite":

raspberry pi video game scratch scree
Figure 5

Search for the Dog1 sprite under Animals then click OK (of course, if you don't want to do a classic cat-and-dog chase, you can pick something else!). You will now see both the dog and the cat in the Sprites area of the screen. When you select the Dog1 sprite, you will see that the script area of the screen is now blank. The script you wrote earlier only applies to the Cat1 sprite.

raspberry pi video game scratch screen
Figure 6

Write a new script for the dog sprite (make sure you have the dog sprite selected, not the cat):

raspberry pi video game scratch scree
Figure 7

Now, run your program again. You should still be able to control the cat, and the dog will automatically follow the cat around! Adjusting the dog_speed variable will change the game's difficulty. The faster the dog moves, the harder it will be to avoid.

Finally, do not forget to save your program!

Making it a Game

So, now you can control an on-screen character, but there is no way to win or lose your game. The following improvements will help make this feel more like a real video game:

  • Make the game end when the dog catches the cat.
  • Add a way to keep score.
  • Make the dog and cat go back to their original starting locations when you run the program.
  • Hide some of the variables that clutter up the stage area.

To make these improvements, modify your cat's script to look like this:

raspberry pi video game scratch screen
Figure 8

Next, modify the dog's script to look like this (remember that the dog sprite has its own "Scripts" tab, so you have to click on the dog to edit its program).

raspberry pi video game scratch screen
Figure 9

Now, when you run the program, the cat should automatically go to the middle of the screen, and the dog should go to the left side of the screen. When the dog catches the cat, the cat will stop and display your final score. You can restart the game by clicking the green flag.

Do not forget to save your work!

Building Your Own Controller

To build your circuit, you will need to use a breadboard. If you have not used a breadboard before, watch this video before you continue:

If you have ever played a video game console like Xbox®, PlayStation® or Nintendo®, then you have used a video game controller. Have you ever wondered how the inside of a controller works? Now you will get a chance to build your own simple controller on a breadboard using two buttons. This video will give you a quick introduction to the buttons that come with your kit:

To build the circuit, you will just need jumper wires and the two pushbuttons.

M-M jumper wires
Figure 10

raspberry pi video game push buttoon
Figure 11

Follow the directions below to assemble the circuit. Note that the buttons each have four pins.


Part Picture Reference First Hole Second Hole
Black M-M jumper wire J17
(Pi Wedge GND)
Ground bus
(right side)
Blue M-M jumper wire J22 Ground bus
(right side)
Blue M-M jumper wire J27 Ground bus
(right side)
Red M-M jumper wire J20 A7
(Pi Wedge G4)
Green M-M jumper wire J25 A8
(Pi Wedge CE1)
Pushbutton raspberry pi video game push buttoon E20

E22

F20

F22

Pushbutton raspberry pi video game push buttoon E25

E27

F25

F27

raspberry pi video game breadboard
Figure 12

Your controller should now look like this:

raspberry pi video game controller
Figure 13


By default, the input pins read a high value (3.3 volts [V]). When you press a button, this connects the input pins directly to ground, "pulling" them down to 0 V. Your program will monitor the input pins and use them to turn the cat left or right.


Modifying Your Program

Right now, your program is set to work with the arrow keys. You need to modify it to work with the buttons on your controller. Change the script for your cat sprite to look like this, by adding two if statements to monitor the input pins, and deleting the when key pressed sections. (right-click them and select "delete").

raspberry pi video game scratch screen
Figure 14

Now, run your program again. Instead of using the arrow keys, you should be able to control the cat by pushing the two buttons on your breadboard.


If your program was working before, but does not work when you try to use the buttons on the breadboard, there are two things you should check:

  • Make sure your program looks exactly like the one in Figure 14. Be especially careful to make sure the correct blocks are inside each if statement, and that you have the correct pins selected in the gpio __ is high? block. If that code is wrong, the buttons will not make your cat turn.
  • Always double-check to make sure everything is connected properly on your breadboard. For example, if you put the red jumper wire into hole A6 instead of hole A7, then it will not be connected to the correct pin on the Pi Wedge. Look closely at your breadboard to make sure everything matches the diagram above.
  • Make sure your buttons do not come loose. The buttons' metal pins are not as long as most of your other circuit components, so they do not go into the breadboard as far. Make sure you press the buttons down firmly so they do not fall out of the breadboard.

Going Further

Once you have your basic game working, there are plenty of changes you can implement to make the game even more exciting. Here are just a few ideas:

  • Use the if on edge, bounce block under Motion to make sprites "bounce" off the edges of the stage, instead of running partially off the screen and getting stuck.
  • Add "power-ups" to the game. For example, add more sprites that give the cat a speed boost (by increasing the "cat_speed" variable) when it touches them. You can do this using the touching __? block under Sensing.
  • For a really advanced challenge, make the power-up sprites disappear when the cat touches them, and reappear in random locations. You can do this with the show and hide blocks under Looks and the pick random __ to __ block under Operators.
  • Give the game a selectable difficulty level; for example, easy, medium, and hard, where the dog moves faster with each increase in difficulty. You can make "buttons" for the player to click on to select the difficulty using the when this sprite clicked block under Events.
  • Rather than letting the game run forever until the dog catches the cat, make it so the player wins the game when reaching a certain score.
  • Create a feature to keep track of the high score.
  • Make the game even harder by adding a second sprite that also chases the cat.
  • Use other sensors to control the cat instead of buttons. See the Create Light Up Artwork with a Raspberry Pi project for instructions on using the light sensor, or the Invent an Interactive Toy with a Raspberry Pi project for instructions on using the magnet sensor.

.

Troubleshooting

For troubleshooting tips, please read our FAQ: Design Your Own Video Game with a Raspberry Pi.

If you like this project, you might enjoy exploring these related careers:

Career Profile

If you've ever watched a cartoon, played a video game, or seen an animated movie, you've seen the work of multimedia artists and animators. People in these careers use computers to create the series of pictures that form the animated images or special effects seen in movies, television programs, and computer games. Read more

Career Profile

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. Read more

Frequently Asked Questions (FAQ)

Q: I need help with a question, related to my Science Buddies Raspberry Pi Projects Kit or Raspberry Pi Circuits Parts Only Kit, not listed here. Who can I ask?

A: Science Buddies has a special area of our Ask an Expert forums dedicated to the Raspberry Pi Projects Kit. Please note that the forums are staffed by volunteers, and it may take a few days to get a response. If you are doing a science project, please do not wait until the day before the project is due to ask an urgent question.

Ask an Expert

The Ask an Expert Forum is intended to be a place where students can go to find answers to science questions that they have been unable to find using other resources. If you have specific questions about your science fair project or science fair, our team of volunteer scientists can help. Our Experts won't do the work for you, but they will make suggestions, offer guidance, and help you troubleshoot.

Ask an Expert

Contact Us

If you have purchased a kit for this project from Science Buddies, we are pleased to answer any question not addressed by the FAQ above.

In your email, please follow these instructions:

  1. What is your Science Buddies kit order number?
  2. Please describe how you need help as thoroughly as possible:

    Examples

    Good Question I'm trying to do Experimental Procedure step #5, "Scrape the insulation from the wire. . ." How do I know when I've scraped enough?
    Good Question I'm at Experimental Procedure step #7, "Move the magnet back and forth . . ." and the LED is not lighting up.
    Bad Question I don't understand the instructions. Help!
    Good Question I am purchasing my materials. Can I substitute a 1N34 diode for the 1N25 diode called for in the material list?
    Bad Question Can I use a different part?

Contact Us

Related Links

  • Science Fair Project Guide
  • Other Ideas Like This
  • Computer Science Project Ideas
  • Video & Computer Games Project Ideas
  • My Favorites

News Feed on This Topic

, ,

Looking for more science fun?

Try one of our science activities for quick, anytime science explorations. The perfect thing to liven up a rainy day, school vacation, or moment of boredom.

Find an Activity

Explore Our Science Videos

Dancing Robot

Delayed Release Pill Challenge

Delayed Release Pill Challenge

Squishy Circuits Classroom Activity Part 1

Squishy Circuits Classroom Activity Part 1

Thank you for your feedback!

Free science fair projects.

Raspberry Pi Design Your Own Video Game

Source: https://www.sciencebuddies.org/science-fair-projects/project-ideas/CompSci_p059/computer-science/video-game-raspberry-pi

Posted by: warnersittort.blogspot.com

0 Response to "Raspberry Pi Design Your Own Video Game"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel