CSCI 171 lab 2 (Jan. 19): intro to Python/PyGame

Refresher: getting logged in and started

  1. Log in with your account username and password (the instructor can look up your username if you forgot it, but the password can only be reset at the IT help desk in the library).

  2. Open a web browser (Applications->Internet->Ice Weasel) and go to today's lab (URL http://csciun1.mala.bc.ca:8080/~wesselsd then follow the links to CSCI 171 -> Labs -> Jan. 19th.)

  3. Open a command window (Applications->Accessories->Terminal) then go to your CSCI 171 directory (cd csci171)

  4. Create a lab2 directory (mkdir lab2) then go to it (cd lab2)

  5. Open the gedit editor, but this time use the command
    gedit game1.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.)


Basic Python/PyGame:

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

    In the editor, put the following as the first line of game1.py
    #! /usr/bin/python

    Below that, add the following then save the file:
    print "Game console initialized"

  2. In the command window (not the editor) run the following command to make the file executable:
    chmod u+x game1.py

    Now try running the program using the command
    ./game1.py

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

  3. Now we'll add the code needed to open the actual game display using pygame.

    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
    pygame.init()
    
    # 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
    x
    to close the window.

    Add the following code to your program:

    # 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
       pygame.time.delay(100)
           

  6. Try running the game again (./game1.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.