Don't like this style? Click here to change it! blue.css

Class 16: (Aside) PyGame

We're running out of classes and I believe in the power of having you be creative with your new Python skills. So this week we'll wrap up Python with a fairly simple game engine that you might consider using in your final project, pygame.

Installing

This is the first time we're not using a browser-based environment in this course. This has two impacts: you can run a more responsive and immersive program, your program won't be as portable...

Please head to the PyGame download page and grab the version that is appropriate for you.

Depending on your platform (Windows, Mac, Linux) you'll have different options. PyGame for Windows can work with Python3, but on other platforms you might need to only use Python2. Another issue is that you'll need 32-bit Python.

Install Task 1: Get Python and PyGame installed on your machine, execute the command import pygame without an error. This might take a while, work with the people around you, call me over, etc. If you have succeeded then walk your neighbor through it too.

On this mac I did the following from the terminal:

Run Hello Task 2: Make the following script run and show a circle.

Game Basics

So the big idea of a graphical video game is that 20-60 times per second we wipe the screen and redraw all of the "things" in our game. So almost all games have an infinite loop at their heart.

Drawing. Pygame lets you draw basic geometric shapes using their pygame.draw.* methods. If you want to simulate a shape moving around the screen you should just slightly adjust the coordinates of the thing in each game loop.

Move Task 3: Have the circle go right as well as down.

Of course a game is most fun when a user interacts with it. So in this snippet we'll react to a key press. Pygame has some handy look-ups for seeing which keys are being pressed during a given game loop:

React Task 4: Adjust the code so that it no longer moves automatically but by up/down/left/right.

The other classic user interaction is mouse movements. Pygame is not 'event' based like many other graphical engines, but you can get the mouse position during any loop and use those coordinates to adjust what you draw.

Print Task 5: When you press down the mouse button leave another copy of the text at that place. (Make an array of text positions to blit to.)

Mouse clicks: we can detect when a mouse button has been pressed down and when it has been released. You can use the release of a mouse button being released as the moment of the click, or the moment of the mouse button going down.

Click Task 6: When you click have the circle stop moving. Then when you click again have the circle resume moving.

Game Assets

You can find open source assets at http://opengameart.org/

Images. You're not really going to draw interesting things just using pygames basic shapes. Most of the time you'll use some pretty art. Pygame lets you load an image and "blit" it to the screen at some place.

Get the image here (right click and save as): Wizard

Background Task 7: Find a background image and put it behind the wizard.

Sound Effects. Likewise you can use PyGame's mixer methods to play sounds (even to loop them).

You'll need a laser sound: laser.wav (save as laser.wav)

Music Task 8: Find background music and play it lightly behind the sound of the laser.

Transforms

I can flip and scale my "surfaces":

Rotate Task 9: Take a look at the pygame documentation for pygame.transform.rotate and make the wizard rotate clock-wise when a mouse button is pressed down.

Explore More

Documentation, tutorials, examples, and more:

http://www.pygame.org/docs/index.html

Daily Challenge

Meet with your group and make a rough sketch of the game you plan to build for project 2.