#Galaga Version .02 #2007-02-20 Derek Mcdonald ################################################################################################################# #import pygame os and sys libraries import pygame, os, sys from pygame.locals import* ################################################################################################################# #define global variables x = 400 y = 540 ex = 400 ey = 50 ymax = 0 xmax = 750 xmin = 30 vel = 1 sy = y + -5 sx = x #the clock for keeping movement in time clock = pygame.time.Clock() #initialize pygame pygame.init() #create window and set up background image window = pygame.display.set_mode ((800, 600)) pygame.display.set_caption('Galaga!') surface = pygame.display.get_surface() background = os.path.join(os.path.dirname(__file__), 'starfield1.bmp') screen = pygame.image.load (background).convert() #define function to draw player ship on X, Y plane def pship(x,y): ship = os.path.join(os.path.dirname(__file__), 'pship.bmp') playership = pygame.image.load (ship).convert() surface.blit (playership, (x, y)) return x, y #Define function to draw the enemy ship on X, Y plane def eship(ex,ey): eship = os.path.join(os.path.dirname(__file__), 'eship.bmp') enemyship = pygame.image.load (eship).convert() surface.blit (enemyship, (ex, ey)) return ex, ey #Define function to move the enemy ship def emove(): global xmax global xmin global vel global ex if vel == 0: if ex != xmax: ex = ex + 1 elif ex == xmax: vel = 1 if vel == 1: if ex != xmin: ex = ex - 1 elif ex == xmin: vel = 0 clock.tick(70) #Define functions to control fire the player weapon when fire command is issued def hit(sx, sy): laser3 = os.path.join(os.path.dirname(__file__), 'hit.bmp') hit = pygame.image.load (laser3).convert() surface.blit(hit,(ex,ey)) def miss(sx,sy): laser2 = os.path.join(os.path.dirname(__file__), 'miss.bmp') miss = pygame.image.load (laser2).convert() surface.blit(miss,(sx,sy)) def pfire(sx, sy): sx = x laser = os.path.join(os.path.dirname(__file__), 'laser.bmp') shot = pygame.image.load (laser).convert() surface.blit(shot,(sx,sy)) return sx, sy def pshoot(sx, sy): while sy <= ymax: sy = sy - 1 pfire(sx, sy) pygame.display.flip() clock.tick(80) #Define function to get input and controll exit commands fire command and movement of playership def input(events): global x global y for event in events: if event.type == QUIT: sys.exit(0) if event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT: if x != xmin: x = x - 10 if event.key == pygame.K_RIGHT: if x != xmax: x = x + 10 if event.key == pygame.K_SPACE: pshoot(x, y) #uncomment the following lines to debug input #else: # print event ################################################################################################################## #start loop while True: #refresh screen surface.blit (screen, (10,0)) #initiate player ship function pship(x,y) #initiate enemy ship function eship(ex,ey) #move the enemy ship emove() #initiate input function input (pygame.event.get()) #iterate loop pygame.display.flip()