def do_collisions(self): for ship in self.ships: for planet in self.planets: if util.collide(ship.xyr, planet.xyr): self.on_planet_collision(ship, planet) for waypoint in self.waypoints: if util.collide(ship.xyr, waypoint.xyr): self.on_waypoint_collision(ship, waypoint)
def move_drop(self): if self.drop: self.drop.move(self.enemy_velocity) if collide(self.drop, self.player): self.lives += 1 self.drop = None elif self.drop.y + self.drop.get_height() > HEIGHT: self.drop = None
def check(self,position): sprites = util.collide(self.unit,self.group,position) for el in sprites: if el.kind==FoodBlockKind.APPLE: self.needapple = True self.group.remove(el) self.score = el.score del el return True return False
def check(self, position): sprites = util.collide(self.unit, self.group, position) for el in sprites: if el.kind == FoodBlockKind.APPLE: self.needapple = True self.group.remove(el) self.score = el.score del el return True return False
def move_enemies(self): for enemy in self.enemies[:]: enemy.move(self.enemy_velocity) enemy.move_lasers(self.laser_velocity, self.player) if random.randrange(0, 4 * 60) == 1: enemy.shoot() if collide(enemy, self.player): self.player.health -= 20 self.enemies.remove(enemy) elif enemy.y + enemy.get_height() > HEIGHT: self.lives -= 1 self.enemies.remove(enemy)
def collison(self, obj): return collide(obj, self)
def check(self, position): sprites = util.collide(self.unit, self.group, position) for el in sprites: return True return False
def main(): game_start = False game_over = False FPS = 60 level = 0 enemies = [] clock = pygame.time.Clock() background = Background(screen, WIDTH, HEIGHT) background.render() player = Player(screen, 350, 400, -5) while True: clock.tick(FPS) # set fps if not game_start: gui.main_menu(screen) bg_music.set_volume(1) if press_enter(): game_start = True elif game_over: screen.fill((0, 0, 0)) # clear screen background.render() gui.show_game_over(screen) # show game over screen bg_music.set_volume(0) if press_enter(): level = 0 player.health = 100 player.x = 350 player.y = 400 player.lasers = [] enemies = [] game_over = False else: background.scroll(0.5) bg_music.set_volume(0.6) # if no enemy left, level up and generate more if (len(enemies) == 0): level += 1 enemies = util.genrate_enemies(screen, level) # ----------------------------- enemies ----------------------------- for enemy in enemies: enemy.move() # move downward # if enemy is rendering(if enemy is on screen) if enemy.rendering: enemy.shoot() # shoot lasers enemy.operate_lasers() # move lasers # if enemy ship enters base (if it pass by player and goes down the screen) if enemy.in_base(): player.health -= 20 enemies.remove(enemy) # check if player laser hits enemy ship for laser in player.lasers: if util.collide(enemy, laser): explosion_sound.play() player.lasers.remove(laser) enemies.remove(enemy) # check if enemy laser collides with player for laser in enemy.lasers: if util.collide(player, laser): enemy.lasers.remove(laser) player.health -= 10 enemy.render() # -----------------------------player----------------------------- player.init_controller() player.operate_lasers() player.render() if player.gameover(): game_over = True gui.show_level(screen, level) pygame.display.update() util.check_quit(pygame) # check for quit event