Refresher: getting logged in and started
Basic Python/PyGame:
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"
Now try running the program using the command
./game1.py
(It should display "Game console initialized" in the command window.)
This takes several steps:
(Note that the height and width are the number of pixels high/wide for the display.)
#! /usr/bin/python print "Game console initialized" import pygame pygame.init() screenSize = width,height = 240,180 display = pygame.display.set_mode(screenSize) |
You should briefly see your game display pop up on the screen, and in your command window you will see "Game console initialized" (plus a bunch of junk from the sound card error message when running this in the lab).
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) |
Let's at least add an instruction to keep the display open until the player clicks the
x |
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)
It should open a black game window, which should remain open until you click the X in the upper right corner of the window.
Add the following code just above the while loop:
# load an image and keep track of the rectangular # section of the display it gets placed in starImage = pygame.image.load("star.gif") starBox = starImage.get_rect()Next, add the following code inside the while loop, just above the pygame.time.delay
# define the colour black (RGB values) black = 0,0,0 # fill the screen buffer with black display.fill(black) # update the buffer to include the image of the star display.blit(starImage, starBox) # redraw the screen from the buffer pygame.display.flip()The whole program should now look something like this:
#! /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) # load an image and keep track of the rectangular # section of the display it gets placed in starImage = pygame.image.load("star.gif") starBox = starImage.get_rect() # 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 # define the colour black (RGB values) black = 0,0,0 # fill the screen buffer with black display.fill(black) # update the buffer to include the image of the star display.blit(starImage, starBox) # redraw the screen from the buffer pygame.display.flip() # pause for 100 milliseconds (one-tenth of a second) # before going on to repeat the cycle again pygame.time.delay(100) |
Add the following code just above the while loop:
speed = [1,1]
Add the following code just above the display.blit:
starBox = starBox.move(speed)
Try running the program (./game1.py) and experimenting with different speed combinations.
Notice that when the star goes outside the display it just keeps on going forever.
# flip the horizontal speed value if the star # goes off the left or the right of the display if starBox.left < 0 or starBox.right > width: speed[0] = - speed[0]
# flip the vertical speed value if the star # goes off the top or the bottom of the display if starBox.top < 0 or starBox.bottom > height: speed[1] = - speed[1]
The final version of the program should now look something like this:
#! /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) # load an image and keep track of the rectangular # section of the display it gets placed in starImage = pygame.image.load("star.gif") starBox = starImage.get_rect() # set an initial speed for the star speed = [1,1] # 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 # move the star at its current speed starBox = starBox.move(speed) # flip the horizontal speed value if the star # goes off the left or the right of the display if starBox.left < 0 or starBox.right > width: speed[0] = - speed[0] # flip the vertical speed value if the star # goes off the top or the bottom of the display if starBox.top < 0 or starBox.bottom > height: speed[1] = - speed[1] # define the colour black (RGB values) black = 0,0,0 # fill the screen buffer with black display.fill(black) # update the buffer to include the image of the star display.blit(starImage, starBox) # redraw the screen from the buffer pygame.display.flip() # pause for 100 milliseconds (one-tenth of a second) # before going on to repeat the cycle again pygame.time.delay(100) |