def __init__(self, target, img): """ Create a HomingMissle. Useful as an enemy or as a weapon for the player. arguments: target - A sprite that the homing missle atempts to track down and shoot img - A path to an image to use for the missle """ BaseSprite.__init__(self, img) self.init_position() self.target = target self._rotating_left = False self._rotating_right = False #the missle starts out moving self._accelerating = True #the missle starts out not knowing if it is #pointed at the target self.targetted = False self.explode_stage = 0 self.exploding = False self.explosion_sound = pygame.mixer.Sound(python_nameconfig.enemy_explode_sound) self.points = 1
def update(self): """update - Update internal data for a game tick""" BaseSprite.update(self) #this stops the guy completely #for frictionless physics like in a space game #you can decrement the velocity or even #delete these lines for asteroids-like physics if not self._accelerating: self.velocity_x = 0 self.velocity_y = 0 #manage the exploding animation if self.exploding: #do an explosion image for each tick self.explodestage += 1 e = self.explodestage if e < 8:#there are 7 explosion images e = str(e) img_name = python_nameconfig.guy_explode_stage + e + ".png" self.master_image = pygame.image.load(img_name) self._update_image() else:#explosion is done self.visible = False self.exploding = False self.kill() self.master_image = pygame.image.load(python_nameconfig.guy_img) self._update_image()
def __init__(self,x,y,orientation, img_name = None): """ Creates a Bullet. arguments: x - the starting x position y - the starting y position orientation - the starting orientation for rotating the sprite image img_name - optional name for an image file to use for the sprite's master image. By default, the sprite will look for an image called 'bullet.png'. """ img = python_nameconfig.default_bullet if img_name != None: img = img_name BaseSprite.__init__(self, img) self.launch_sound = pygame.mixer.Sound(python_nameconfig.guy_shoot_sound) self.launch_sound.play() self.explosionSound = pygame.mixer.Sound(python_nameconfig.guy_bullet_explode) self.orientation = orientation self.x = x self.y = y self._accelerating = True self.acceleration_divisor = .75 self.max_velocity = 100 self.max_ticks = 10 self.ticks = 0 self.exploding = False self.explodestage = 0 self._update_image()
def __init__(self): """Creates an Enemy """ BaseSprite.__init__(self, python_nameconfig.enemy_image) self.points = 1 self.explosion_sound = pygame.mixer.Sound(python_nameconfig.enemy_explode_sound) self.explode_stage = 0 self.exploding = False
def stop_acceleration(self): """ stop_acceleration - tells the sprite to stop accelerating. Depending on the game physics that may or my not cause the sprite to accually stop. """ BaseSprite.stop_acceleration(self) self.hum.stop()
def accelerate(self): """ accelerate - increase the sprites speed along it's current trajectory and plays the sound for the sprite's "moto". """ #only accelerate if the Guy is alive if self.alive() and not self.exploding: BaseSprite.accelerate(self) self.hum.play(-1)#loop sound endlessly
def update(self): """update - update internal data and position. Typically called by the game controller each game tick. """ BaseSprite.update(self) if self.exploding: #do an explosion image for each tick self.explode_stage += 1 e = self.explode_stage if e < 8: e = str(e) img_name = python_nameconfig.enemy_explode_stage + e + ".png" self.master_image = pygame.image.load(img_name) self._update_image() return else:#explosion is done self.visible = False self.exploding = False self.kill() return self.targetted = False #calculate target angle targx = self.target.x - self.x targy = self.target.y - self.y target_angle = ((math.atan2(targy,targx)) * 180)/ math.pi if target_angle < 0: target_angle = 360 + target_angle #translate the orientation to the # same system as the target angle fire_angle = 360 - self.orientation if fire_angle == 360: fire_angle = 0 fire_angle += 270 if fire_angle >= 360: fire_angle -= 360 #rotate to face the target delta = fire_angle - target_angle if delta > 5: self._rotating_left = True self._rotating_right = False elif delta < -5: self._rotating_left = False self._rotating_right = True else: self._rotating_left = False self._rotating_right = False self.targetted = True
def update(self): BaseSprite.update(self) if self.exploding: # do an explosion image for each tick self.explode_stage += 1 e = self.explode_stage if e < 8: e = str(e) img_name = python_nameconfig.enemy_explode_stage + e + ".png" self.master_image = pygame.image.load(img_name) self._update_image() return else: # explosion is done self.visible = False self.exploding = False self.kill() return
def __init__(self, bullets_group): """ Creates a Guy arguments: bullets_group - A pygame.SpriteGroup managed by the games collision detection system. """ BaseSprite.__init__(self, python_nameconfig.guy_img) self.bullets = bullets_group self.hum = pygame.mixer.Sound(python_nameconfig.guy_eng) self.explosionSound = pygame.mixer.Sound(python_nameconfig.guy_explode) self.exploding = False self.explodestage = 0 self.visible = True self.max_bullets = 4
def update(self): """update - update internal data and position. Typically called by the game controller each game tick. """ BaseSprite.update(self) self.ticks += 1 if not self.exploding and self.ticks > self.max_ticks: self.kill() if self.exploding: self.explodestage += 1 e = self.explodestage if e < 5: e = str(e) self.masterImage = pygame.image.load(python_nameconfig.bullet_explode_stage + e + ".png") self._update_image() else: self.kill()