In this task, we will learn the usage of pygame
for game development. We will get started with an example that opens a blank window and write a couple of lines to monitor events so that we can quit the program if the window is closed.
We will go through the important features of the pygame
module, which is necessary to build our arcade game, including playing sounds and controlling the menu.
As mentioned earlier, this project is based on the book Making Games with Python & Pygame (http://inventwithpython.com/makinggames.pdf). In this task, we will go through select features of the pygame
module. It is important that you familiarize yourself with the different features available under the pygame
module.
pygame
and the sys
module:import pygame,sys
pygame.locals
, since it contains several constant variables:from pygame.locals import *
pygame
module, we need to initialize the module:pygame.init()
pygame.Surface
object that is stored in a variable called DISPLAYSURF
:DISPLAYSURF = pygame.display.set_mode((400, 300))
pygame.display.set_caption('Hello World!')
while True: # main game loop for event in pygame.event.get(): if event.type == QUIT: pygame.quit() sys.exit() pygame.display.update()
Since the sound effects form the crux of an arcade game, we need to know how to play sounds using the pygame
platform.
beeps.wav
file, available along with the downloads of this file. The file needs to be copied into the same directory as the python script that runs the game as follows:soundObj = pygame.mixer.Sound('beeps.wav')
play()
method as follows:while True: # main game loop soundObj.play()
stop()
method.simple_example.py
example. (http://www.pygame.org/project-MenuClass-1260-.html.)menu = cMenu(x, y, h_pad, v_pad, orientation, number, background,buttonList)
x
and y
refer to the location of the origin of the menu object on the game screenh_pad
and v_pad
refer to the spacing between the buttons in the horizontal and vertical directionsorientation
parameter refers to the arrangement of the buttons on the screen, that is, 'horizontal'
or 'vertical'
number
parameter refers to the number of buttons that can be accommodated in a single row (when arranged horizontally) or in a single column (when arranged vertically)background
parameter refers to the surface on which the menu has to be createdbuttonList
parameter refers to the list of buttons that we want on the screenmenu = cMenu(50, 50, 20, 5, 'vertical', 100, screen, [('Start Game', 1, None), ('Score Board', 2, None), ('Exit', 3, None)])
menu.set_center(True, True)
menu.set_alignment('center', 'center')
state = 0 prev_state = 1
pygame.event.wait()
method detects any keyboard or mouse events and updates the current state accordingly.pygame.event.post(pygame.event.Event(EVENT_CHANGE_STATE, key = 0))
rect_list, state = menu.update(e, state)
:while True: if prev_state != state: pygame.event.post(pygame.event.Event(EVENT_CHANGE_STATE, key = 0)) prev_state = state e = pygame.event.wait() if e.type == pygame.KEYDOWN or e.type == EVENT_CHANGE_STATE: if state == 0: rect_list, state = menu.update(e, state) elif state == 1: print 'Start Game!' state = 0 elif state == 2: print 'Load Game!' state = 0 elif state == 3: print 'Options!' state = 0 else: print 'Exit!' pygame.quit() sys.exit() # Quit if the user presses the exit button if e.type == pygame.QUIT: pygame.quit() sys.exit() # Update the screen pygame.display.update()
3.145.20.193