Presentation is loading. Please wait.

Presentation is loading. Please wait.

WITH PYGAME ON THE RASPBERRY PI

Similar presentations


Presentation on theme: "WITH PYGAME ON THE RASPBERRY PI"— Presentation transcript:

1 WITH PYGAME ON THE RASPBERRY PI
GETTING STARTED WITH PYGAME ON THE RASPBERRY PI Mark Vanstone Technical Director TechnoVisual education

2 Is available with the standard install of Raspian for Raspberry Pi
WHY PYGAME? Provides tools that are useful for all kinds of games, animation and audio programs. Is available with the standard install of Raspian for Raspberry Pi

3 Game Programming Concepts
Different types of games – RTS, FPS, Turn Based, Arcade The Game Environment Data Structures User Input

4 First Steps in Python Load Python 3 (IDLE)
Make sure the PyGame library is available Create a new Python file

5 Setup For Using PyGame Importing PyGame module
Define a window for the game A basic game loop Checking for exit events

6 Importing The PyGame Module
Load the PyGame and the Sys modules and from the PyGame module use all the default predefined constants. Then call the init() function in the PyGame module.

7 Define a Window for the Game
Create a constant called DISPLAYSURF and attach a display window to it by calling the PyGame module function set_mode with the size of 400 pixels by 300 pixels. Then set the title of the window with the set_caption function.

8 A Basic Game Loop Do the following forever: For each event object we are given by the PyGame module, do the following:

9 Checking For Exit Events
If the type of the event we are looking at is equal to the constant 'QUIT' then call the PyGame module function quit() and then the Sys module function exit(). *

10 Using Data to Draw and Move Elements on the Screen
Why do we need data? Defining the game elements in data Defining some colours Drawing elements to the screen Changing the data to make animation

11 Variables (Numbers or Strings)
Why Do We Need Data? Variables (Numbers or Strings) Arrays (Lists) Objects (Classes)

12 Defining the Game Elements in Data
(0,0) The Alien X co-ordinate starts with the value 200 and the Alien Y co-ordinate starts with the value 20.

13 Defining Some Colours Set up the named constants to equal the RGB values given.

14 Drawing Elements to the Screen
Fill the DISPLAYSURF window with black, draw the Alien and then update the display.

15 Drawing the Alien lines(Surface, color, closed, pointlist, width=1)
Set variable x to equal the value of alienX and y to the value of alienY. Create a set of points that represent a triangle. Draw the edges of the triangle using the PyGame lines function.

16 Changing the Data to Make Animation
If the direction of the alien is 0 then take 1 away from alien X co-ordinate. If the result is 0 change the alien direction to 1. If the direction of the alien is 1 then add 1 to alien X co-ordinate. If the result is 400 change the alien direction to 0.

17 Controlling the Speed of the Display
Set gameclock to the returned value of the PyGame function Clock(). Set the gameclock tick value to be 20 (maximum frame rate of 20). Run the rest of the gameloop. *

18 Using Game Logic to Make The Game
What makes a game interactive How to listen for key presses Make a laser element Checking to see if the laser hit the alien ship

19 What Makes a Game Interactive?
Player controls Game events

20 Drawing the Player Ship

21 How To Listen For Key Presses
Add a call to a function to check key presses inside our main loop.

22 Make a Laser Element Define the drawLaser function, if the Y co-ordinate of the laser is greater than or equal to 1 then make a co-ordinate list of points to draw and use it to draw a laser line in white. To move the laser, check if the Y co-ordinate is greater than 1 – if it is then take 5 off the Y co-ordinate value.

23 How is our Game Loop Shaping Up?

24 Check to See if The Laser Hit The Alien Ship
If the laser is inside the bounding box of the alien ship, move the alien ship off the screen.

25 Next Steps Multiple alien ships Alien ships shoot back Multiple lasers Score Alien ships move differently Use bitmap graphics for the ships Make explosion graphic when a ship is hit Make sounds when a laser is fired or a ship is hit for extra resources


Download ppt "WITH PYGAME ON THE RASPBERRY PI"

Similar presentations


Ads by Google