(Lab 2) Intro to Python/PyGame

Today we're going to create a simple visual demo using Python on a linux server.

Getting logged in and started

1. Log in with the account username and password supplied by the instructor


2.Open a web browser, use either the toolbar icon or the drop-down menu (Applications->Internet->Chromium), then go to today's lab (URL http://csci.viu.ca/~wesselsd and follow the link to the Games Workshop)

3. Open a terminal window (so you can enter linux commands) using either the toolbar terminal icon or the drop down menu (Applications->Systems Tools->Mate Terminal)

4. Open the gedit text editor (which we'll use to create our python program) using either the toolbar icon or going to the open (black) terminal window and typing the following command and pressing enter.
gedit game.py &
(The & at the end of the command will allow you to continue working in your command window and the editor at the same time.)

This should pop open a new editor window - from this point on we'll write our python code in the gedit window, and can keep typing linux commands in the MateTerminal window.

We should now have these windows open...

The editor window (for python code) The terminal window (for linux commands)

Basic Python/PyGame:

  1. In the (white) editor window: The basic structure of our python programs is to start (on the very top line) by identifying the interpretter python must use.

    Type the following as the first two lines of our python code:
    #! /usr/bin/python
    print "Game console initialized"

    Now click Save (top of the screen) to save our python code.

  2. In the (black) terminal window: enter the following command to make the new game.py file executable (so we can run our game):
    chmod u+x game.py

    Now, in the same window, try running the program using the command

    (It should display "Game console initialized" in the command window.)

  3. Back in the (white) editor window: Now we'll add the code needed to open the actual game display using pygame. We're going to add a number of new lines of code below our first two lines.

    This takes several steps:

  4. This might be a good point to add some comments to the code, as well as some blank lines to make it more readable.

    Comments are essentially programmer notes - they don't change the way the code behaves, they are just explanations of what the programmer was thinking when they developed the code.

    In Python, anything to the right of the # symbol is treated as a comment, and essentially ignored by the Python interpretter.

    Here's a rewrite of the program so far, but with some comments and whitespace:
    #! /usr/bin/python
    # display an initial message in the command window
    print "Game console initialized"
    # include (import) any required modules
    import pygame
    # initialize pygame
    # set an initial size for the game display,
    #     and open the display
    screenSize = width,height = 240,180
    display = pygame.display.set_mode(screenSize)

  5. The display isn't terribly interesting so far.

    Let's at least add an instruction to keep the display open until the player clicks the
    to close the window.

    Add the following code to your program, using tabs to indent the code (Python is picky about that):

    # this keeps cycling through the "while loop"
    #   until the player has clicked the close box
    keepPlaying = True
    while keepPlaying:
       # check for any events that need to be processed,
       #    e.g. the player clicking the close box
       for event in pygame.event.get():
          if event.type == pygame.QUIT:
             keepPlaying = False
       # pause for 100 milliseconds (one-tenth of a second)
       # before going on to repeat the cycle again

  6. Try running the game again (./game.py)

    It should open a black game window, which should remain open until you click the X in the upper right corner of the window.

  7. It still isn't too terribly interesting, so let's at least add an image to the display.

  8. Finally, let's get the star to bounce around inside the display.

Feel free to experiment with creating additional stars on the screen, play with speeds and trajectories, get them to change speeds when they bounce or collide with one another ...

#! /usr/bin/python

print "Game console initialized"

import pygame

screenSize = width,height = 240,180
display = pygame.display.set_mode(screenSize)

starImage = pygame.image.load("star.gif")
starBox = starImage.get_rect()

# add a second star
starImage2 = pygame.image.load("star.gif")
starBox2 = starImage2.get_rect()

keepPlaying = True
speed = [10,5]
speed2 = [5,10]
while keepPlaying:

	# see if it's time to end the game
	for event in pygame.event.get():
		# did someone click the X to close the window?
		if event.type == pygame.QUIT:
			keepPlaying = False

	# redraw the background
	black = 0,0,0

	# draw the stars on the screen
	starBox = starBox.move(speed)
	starBox2 = starBox2.move(speed2)

	# update their speed, changing direction if needed
	if starBox.left < 0 or starBox.right > width:
		speed[0] = - speed[0]
	if starBox.top < 0 or starBox.bottom > height:
		speed[1] = - speed[1]
	if starBox2.left < 0 or starBox2.right > width:
		speed2[0] = - speed2[0]
	if starBox2.top < 0 or starBox2.bottom > height:
		speed2[1] = - speed2[1]

	# see if they collide, print BOOM on the console if they do
	collide = True
	if starBox.left > starBox2.right:
		collide = False
	elif starBox.right < starBox2.left:
		collide = False
	elif starBox.top > starBox2.bottom:
		collide = False
	elif starBox.bottom < starBox2.top:
		collide = False
		print "Boom!"

	# update the display
	display.blit(starImage, starBox)
	display.blit(starImage2, starBox2)

game1.py (completed version 1)
game2.py (completed version 2)

You can continue on with the next lab in the workshop series, or see our main game workshop page for more information and ideas, or check out the pygame.org website for lots of game examples and resources.