Building Video Games for Fun with PyGame
Shannon -jj Behrens | @jjinux | jjinux.com
Sometimes, it's a lot of fun to be an amateur
PyGame
Introduction
Installation
Tutorial
Pummel the Chimp
Importing Modules
Please forgive the style; this isn't my code ;)
pygame imports things recursively
The locals module has commonly used constants and functions (optional)
Warn if fonts or the mixer aren't available
Loading Images
Create platform independent file paths
Just die if we can't load images
Convert the image in order to optimize it for our display in order to blit it (i.e. copy it to the screen) faster
A colorkey is the color that should be used to represent transparency
RGB white is (255, 255, 255)
Loading Sounds
If pygame can't initialize the mixer, just make the sounds do nothing
If it can, but we can't load the sound file, then just die
Fist Part 1
This is part 1 of our Fist object
It inherits from Sprite which is like an image in the game
A sprite has an image and a rect for the location
Sprites have an update method which is called once per frame
Move the fist to where the mouse is
Fist Part 2
Track whether the user is punching with the punching instance variable
If the user punches, move the fist slightly (on previous slide)
The punch method takes a target parameter
The punch method returns true if the fist is colliding with the target
Chimp Part 1
If the chimp is dizzy, he's spinning
Otherwise, he's walking from side to side
Use the move instance variable to keep track of which direction the chimp is moving
Use the dizzy instance variable to control the spinning motion the chimp does when it gets hit
Chimp Part 2
To walk, just change self.rect
If the chimp hits the edge of the screen, flip the image around and move the other way
When spinning, rotate 360 degrees 12 degrees per frame
When rotating, always start with the original image to compensate for quality loss at each step
Chimp Part 3
If the chimp was punched, just set dizzy to 1
Save a reference to the original image so that we can mutate self.image
Main Part 1
This is the main method for the game
Initialize pygame, set up the display, and set a window title
Make the mouse invisible since we draw the fist wherever the mouse is
Main Part 2
Create a background "surface" that is the same size as our screen
Then fill it with an off-white color
Note, we haven't yet drawn the background; we've just created a surface object
Main Part 3
If the font subsystem couldn't load, we just ignore this part
Create a new Font using a TrueType font (use None for the default) and a size
Render the string using the font with antialiasing turned on in a dark gray
Center the text on the background
Finally "blit" (i.e. copy or paste) the text onto the background
Main Part 4
We haven't actually shown anything yet
Now blit the background onto the screen
We use a technique called "double buffering"
Imagine a painting with two sides. You paint one side while the user looks at the other side. When you're ready, you flip the painting around and start working on painting the other side.
Main Part 5
Create a clock to control our framerate so the game doesn't run too fast
Create the two sounds that we'll use for sound effects
Create the Chimp and the Fist sprites
RenderPlain is a "sprite group" which takes care of rendering our two sprites, fist and chimp
Main Part 6
This is an infinite loop
All games run in some sort of a loop
Inside the loop, you generally check for user input, update the state of the world, and then draw it to the screen
Run at 60 frames per second
Main Part 7
On each frame, process all the events
If the user closed the window or hit escape, exit the loop
If the user pressed the mouse, tell the fist to try to punch the chimp
If it succeeds, tell the chimp it was punched
Play the relevant sounds
On mouse up, tell the fist to unpunch
Main Part 8
Calling update on the sprite group calls update for all of the sprites in the group
This gives each sprite a chance to update itself
The chimp will step a little to the side or spin a little if it's dizzy
The fist will update based on the position of the mouse
Main Part 9
Draw the background onto the screen to erase everything
This is slightly inefficient, but okay for this game
Draw all the sprites
Flip the display which shows everything we've drawn at the same time
Quitting
Outside of the loop, just call pygame.quit()
At the bottom of the file, call main()
That's it!
PyWeek
Introduction
Some Thoughts
Some Tips
TicTacToe3
Introduction
Implementation
Hellacopy
Introduction
Implementation
Other Things
Scratch
I've had a lot of luck teaching kids how to write video games using Scratch
It's perfect for kids in the 7-12 age range
I brought three of my students ;)
There are some really nice tutorials
And of course, I encourage them to tweak things once they've made it through the tutorial
High School Sim
For high school kids, I teach them how to build a text-based high school simulation using just Python
And of course, tweaking the simulation and making it more complex and realistic is where all the fun lies
Raspberry Pi and Kano
Teaching kids on Raspberry Pi's makes everything better
I'm especially fond of Kano and Kano OS
Over the summer, I setup a lab at my house and teach groups of up to 10 kids
It really helps to have multiple helpers when doing this
Unity
One of these days, I'm going to take the time to learn a professional, 3D video game framework like Unity
I've heard there's a nice class on Udemy, and there's a Black Friday sale going on ;)
Phaser
I'm also tempted to try out Phaser which is "A fast, fun and free open source HTML5 game framework"
Closing