#!/usr/bin/python import pygame import sys from pygame.locals import KEYDOWN, KEYUP, K_SPACE, K_ESCAPE, K_RIGHT, K_LEFT width = 320 height = 240 imageWidth = 32 imageHeight = 32 goingLeft = True invaderHeight = 0 gunLeft = False gunRight = False gunXpos = (width/2)-(imageWidth/2) delay = 10 class BoxSprite(pygame.sprite.Sprite): image = None def __init__(self, initial_position): pygame.sprite.Sprite.__init__(self) if BoxSprite.image is None: BoxSprite.image = pygame.image.load("ball.png") self.image = BoxSprite.image self.rect = self.image.get_rect() self.rect.topleft = initial_position self.next_update_time = 0 # update() hasn't been called yet. self.yPos = initial_position[1] def update(self, current_time, left, right): global goingLeft, invaderHeight, imageWidth, delay # Update every 2 milliseconds = 1/500th of a second. if self.next_update_time < current_time: # If we're at the left or right the screen, switch directions. if self.rect.topleft[0] == left: goingLeft = False invaderHeight += 1 elif self.rect.topleft[0] == right-imageWidth: goingLeft = True invaderHeight += 1 if goingLeft == True: self.rect.topleft = [self.rect.topleft[0]-1, self.rect.topleft[1]] else: self.rect.topleft = [self.rect.topleft[0]+1, self.rect.topleft[1]] self.rect.topleft = [self.rect.topleft[0], invaderHeight+self.yPos] self.next_update_time = current_time + delay class missile(pygame.sprite.Sprite): image = None def __init__(self, initial_position): pygame.sprite.Sprite.__init__(self) if missile.image is None: missile.image = pygame.image.load("arrow.png") self.image = missile.image self.rect = self.image.get_rect() self.rect.topleft = initial_position self.next_update_time = 0 # update() hasn't been called yet. def update(self, current_time, left, right): global missile # Update every 2 milliseconds = 1/500th of a second. if self.next_update_time < current_time: # If we're reached the top then stop if self.rect.topleft[1] == 0: missiles.remove(self) self.kill() return else: self.rect.topleft = [self.rect.topleft[0], self.rect.topleft[1]-1] self.next_update_time = current_time + 4 class gun(pygame.sprite.Sprite): image = None def __init__(self): global width, imageHeight, gunXpos pygame.sprite.Sprite.__init__(self) if gun.image is None: gun.image = pygame.image.load("gun.png") self.image = gun.image self.rect = self.image.get_rect() self.rect.topleft = [gunXpos, height-imageHeight] self.next_update_time = 0 # update() hasn't been called yet. def update(self, current_time, left, right): global gunXpos, width, imageWidth # check update if self.next_update_time < current_time: if gunLeft and gunXpos>0: gunXpos -= 1 if gunRight and gunXpos