Exemplo n.º 1
0
 def went_out(self, width, height):
     if not out_of_bounds(width, height, self):
         self.appeared = True
     if out_of_bounds(width, height, self) and self.appeared:
         self.level += 1
         return True
     return False
Exemplo n.º 2
0
    def handle_collision(self):
        for player in self.players:
            for ammo in self.ammos:
                if player.collision(ammo):
                    Game.reload.play()
                    player.nb_ammo += ammo.quantity
                    ammo.randomize(Game.WIDTH, Game.HEIGHT)

            for fuel in self.fuels:
                if player.collision(fuel):
                    Game.fuel_sound.play(maxtime=300)
                    player.fill_oil()
                    fuel.randomize(Game.WIDTH, Game.HEIGHT)

            for i, laser in enumerate(player.laser):
                if out_of_bounds(Game.WIDTH, Game.HEIGHT, laser):
                    player.laser.pop(i)
                else:
                    for enemy in self.players:
                        if laser.collision(enemy):
                            Game.crash.play()
                            enemy.randomize(Game.WIDTH, Game.HEIGHT)
                            player.laser.pop(i)
                            break
                    laser.draw(Game.WIN)
Exemplo n.º 3
0
def main_game(level, lives):
    FPS = 60
    angle = 0
    nb_enemies = 5*level
    clock = pygame.time.Clock()
    big_font = pygame.font.SysFont('comicsans', 70)
    main_font = pygame.font.SysFont('comicsans', 50)
    small_font = pygame.font.SysFont('comicsans', 30)
    very_small_font = pygame.font.SysFont('comicsans', 20)
    run = True
    player_ship = Player(WIDTH // 2, HEIGHT // 2, 0, 0, 0, 0)

    aliens = [Enemy(0, 0, 1).randomize(WIDTH, HEIGHT, player_ship) for _ in range(nb_enemies)]
    fuel = Fuel(0, 0).randomize(WIDTH, HEIGHT)
    ammo = Ammo(0, 0).randomize(WIDTH, HEIGHT)
    rot_steer = 0.2
    forward_steer = 0.1

    # draw text and oil level on screen
    def draw_gui():

        # Draw text
        live_label = main_font.render("lives :" + str(lives), 1, (255, 255, 255))
        level_label = main_font.render("level " + str(level), 1, (255, 255, 255))
        ammo_label = main_font.render("ammo : " + str(player_ship.nb_ammo), 1, (255, 255, 255))
        enemy_label = main_font.render("enemy : " + str(len(aliens)), 1, (255, 255, 255))
        fps_label = very_small_font.render(str(int(round(clock.get_fps(), 0))) + " FPS", 1, (255, 255, 255))

        WIN.blit(live_label, (10, 10))
        WIN.blit(level_label, (WIDTH - level_label.get_width() - 10, 10))
        WIN.blit(ammo_label, (10, 20 + live_label.get_height()))
        WIN.blit(enemy_label, (10, 30 + ammo_label.get_height() + live_label.get_height()))

        WIN.blit(fps_label, (1, 1))

        # Draw oil level
        oil_label = small_font.render("oil level", 1, (255, 255, 255))
        WIN.blit(oil_label, (WIDTH - oil_label.get_width() - 10, 40 + level_label.get_height()))

        pygame.draw.rect(WIN, (0, 255, 0), (WIDTH - 50, 100, 20, 300), 2)
        # oil level goes from y=102 to y=298
        percentage = player_ship.get_oil_percentage()
        if percentage:
            pygame.draw.rect(WIN, (255, 0, 0), (WIDTH - 50 + 2,
                                                int((102 * percentage) + (400 * (1 - percentage))),
                                                20 - 3,
                                                int((298 * (percentage)))))

    # game loop
    while run and len(aliens)>0:
        clock.tick(FPS)
        bg.draw(WIN)

        player_ship.turn_off()
        keys = pygame.key.get_pressed()

        if player_ship.oil_level != 0:
            engine_keys = [pygame.K_LEFT, pygame.K_RIGHT, pygame.K_UP,
                           pygame.K_a, pygame.K_d, pygame.K_w]
            if max([keys[k] for k in engine_keys]):
                player_ship.turn_on()

            if keys[pygame.K_LEFT] or keys[pygame.K_a]:
                player_ship.speed_angle += rot_steer
            if keys[pygame.K_RIGHT]or keys[pygame.K_d]:
                player_ship.speed_angle -= rot_steer

            if keys[pygame.K_UP] or keys[pygame.K_w]:
                angle = np.radians(player_ship.angle + 90)
                player_ship.speed += np.array([np.cos(angle),
                                               np.sin(-angle)]) * forward_steer

        if keys[pygame.K_SPACE]:
            player_ship.shoot()

        if keys[pygame.K_RETURN]:
            run = False

        if keys[pygame.K_p]:
            pause_label = big_font.render("Pause - press P to unpause", 1, (255, 255, 255))
            WIN.blit(pause_label, (WIDTH // 2 - pause_label.get_width() // 2, HEIGHT // 2))
            pygame.display.update()
            pygame.time.wait(100)
            paused = True
            while paused:
                for event in pygame.event.get():
                    if event.type == pygame.QUIT:
                        quit()
                    if event.type == pygame.KEYDOWN and event.key == pygame.K_p:
                        paused = False
                    if event.type == pygame.KEYDOWN:
                        print(event.key)
            pygame.time.wait(100)

        fuel.draw(WIN)

        if player_ship.collision(fuel):
            fuel_sound.play(maxtime=300)
            player_ship.fill_oil()
            fuel.randomize(WIDTH, HEIGHT)

        ammo.draw(WIN)

        if player_ship.collision(ammo):
            reload.play()
            player_ship.nb_ammo += ammo.quantity
            ammo.randomize(WIDTH, HEIGHT)

        for i,enemy in enumerate(aliens):
            enemy.next_state()
            if enemy.went_out(WIDTH, HEIGHT):
                enemy.randomize(WIDTH, HEIGHT, player_ship)

            enemy.draw(WIN)
            if player_ship.collision(enemy):
                crash.play()
                player_ship.health -= 20
                aliens.pop(i)
                enemy.randomize(WIDTH, HEIGHT, player_ship)

        if player_ship.health <= 0 or (not player_ship.collision(bg)):
            run = False

        for i, laser in enumerate(player_ship.laser):
            laser.next_state()
            if out_of_bounds(WIDTH, HEIGHT, laser):
                player_ship.laser.pop(i)
            else:
                for i2, enemy in enumerate(aliens):
                    if laser.collision(enemy):
                        crash.play()
                        aliens.pop(i2)
                        enemy.randomize(WIDTH, HEIGHT, player_ship)
                        player_ship.laser.pop(i)
                        break
                laser.draw(WIN)

        player_ship.next_state()
        player_ship.draw(WIN)

        draw_gui()

        pygame.display.update()

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                quit()

    if len(aliens) == 0 and player_ship.health>0:
        return True
    return False