Beispiel #1
0
    def __init__(self,
                 x,
                 move_speed,
                 health,
                 collision_damage,
                 image_name,
                 turning_rate=0,
                 current_angle=270,
                 y=0,
                 boss=False,
                 name='') -> pygame.sprite.Sprite:
        pygame.sprite.Sprite.__init__(self)

        self.image_path = os.path.join('graphics', image_name)
        self.image = pygame.image.load(self.image_path).convert_alpha()
        self.image_original = self.image
        self.move_speed = move_speed
        self.turning_rate = turning_rate
        self.current_angle = ml.normalize_angle(current_angle)
        self.health = health
        self.max_health = health
        self.invincible = False
        self.collision_damage = collision_damage
        self.firing_timer = ml.time() - random.uniform(0, 2.0)
        self.spawn_time = ml.time()
        self.spawn_tick = ml.ticks
        self.is_boss = boss
        self.name = name
        self.phase = 1
        self.num_phases = None
        self.phase_change_time = ml.time()

        # Set up image, rect, and mask
        self.rect = self.image.get_rect()
        self.x = x
        self.y = y
        self.rect.center = x, self.y
        self.mask = pygame.mask.from_surface(self.image)

        # Add sprite to group
        self.add(ml.enemy_group)

        if self.is_boss:
            ml.update_boss_data(self.max_health, self.health, self.name)
Beispiel #2
0
 def damage(self, damage: int):
     self.health -= damage
     if self.health < 0:
         self.health = 0
     if self.is_boss:
         ml.update_boss_data(self.max_health, self.health, self.name)
Beispiel #3
0
 def check_health(self):
     if self.health <= 0:
         if self.is_boss:
             ml.update_boss_data(self.max_health, 0, self.name)
         self.kill()
         ml.boss_part_group.empty()
Beispiel #4
0
def game_over():
    """Shows the game over screen. Saves player score."""
    global player_move_up, player_move_left, player_move_down, player_move_right, \
        mouse_movement_active, shift_pressed, game_running
    logging.debug('Game Over')
    pygame.time.wait(1000)
    x_margin = 150

    # Reset player movement and event queue
    player_move_up = False
    player_move_down = False
    player_move_right = False
    player_move_left = False
    game_running = False
    shift_pressed = False
    pygame.event.clear()

    ml.update_boss_data(1, 0, '')

    # Clear window
    window.fill(BLACK)

    # Add player's score
    font = pygame.font.Font(ml.font, 30)
    score_font = pygame.font.Font(ml.font, 50)
    message_surface = font.render('Your final score:', True, WHITE)
    score_surface = score_font.render(str(ml.score), True, WHITE)
    message2_surface = font.render('Would you like to save your score?', True,
                                   WHITE)
    message3_surface = font.render('Score saved!', True, WHITE)
    no_button = ml.get_button_surface(button_width, button_height, 'No',
                                      button_font_size, WHITE)
    no_button_rect = pygame.Rect(x_margin,
                                 window.get_height() - 200, button_width,
                                 button_height)
    yes_button = ml.get_button_surface(button_width, button_height, 'Yes',
                                       button_font_size, WHITE)
    yes_button_rect = pygame.Rect(window.get_width() - button_width - x_margin,
                                  window.get_height() - 200, button_width,
                                  button_height)

    while True:
        # Clear the window
        window.fill(BLACK)

        # Event handling
        for event in pygame.event.get():
            if event.type == QUIT:
                terminate()
            # Escape returns to main menu
            elif event.type == KEYDOWN:
                if event.key == K_ESCAPE:
                    return

            elif event.type == MOUSEBUTTONUP:
                # noinspection PyArgumentList
                # No button returns to main menu
                if no_button_rect.collidepoint(event.pos):
                    return
                # Yes button asks player for their name, then adds to high score list
                elif yes_button_rect.collidepoint(event.pos):
                    player_name = ask_player_input('Please enter your name:')
                    # player_name is '' if player cancels input
                    if player_name:
                        window.blit(message3_surface,
                                    ((window.get_width() / 2) -
                                     (message3_surface.get_width() / 2),
                                     window.get_height() / 2 + 100))
                        logging.info('Saving score:   %s: %d' %
                                     (player_name, ml.score))
                        ml.add_highscore(player_name, ml.score)
                        pygame.display.update()
                        pygame.time.wait(1500)
                    return

        # Blit text and buttons
        window.blit(message_surface, ((window.get_width() / 2) -
                                      (message_surface.get_width() / 2),
                                      (window.get_height() / 2) - 150))
        window.blit(score_surface, ((window.get_width() / 2) -
                                    (score_surface.get_width() / 2),
                                    (window.get_height() / 2) - 85))
        window.blit(message2_surface, ((window.get_width() / 2) -
                                       (message2_surface.get_width() / 2),
                                       (window.get_height() / 2)))
        window.blit(no_button, no_button_rect)
        window.blit(yes_button, yes_button_rect)

        draw_cursor()
        pygame.display.update()
        clock.tick(FPS)