コード例 #1
0
ファイル: BigAsteroid.py プロジェクト: juanfdg/Asteroids
    def kill(self):
        groups = self.groups()  # Store the groups on which the asteroid was
        super().kill()  # Exclude the asteroid from the groups

        Sounds.big_explosion_sound()

        spread_angle = gauss(0, 45)
        spread_speed = gauss(0, 1)

        vel_dir1 = (cos(radians(self.direction+spread_angle)),
                    sin(radians(self.direction+spread_angle)))
        vel_dir2 = (cos(radians(self.direction-spread_angle)),
                    sin(radians(self.direction-spread_angle)))

        speed1 = []
        speed1.append(self.speed[0] + spread_speed*vel_dir1[0])
        speed1.append(self.speed[1] + spread_speed*vel_dir1[1])

        speed2 = []
        speed2.append(self.speed[0] - spread_speed*vel_dir2[0])
        speed2.append(self.speed[1] - spread_speed*vel_dir2[1])

        # Add two smaller asteroids to the groups
        new_asteroids = [MediumAsteroid(self.screen, self.x, self.y, speed1,
                                        vel_dir1),
                         MediumAsteroid(self.screen, self.x, self.y, speed2,
                                        vel_dir2)]
        for group in groups:
            group.add(new_asteroids)
コード例 #2
0
ファイル: TitleScreen.py プロジェクト: juanfdg/Asteroids
    def __init__(self, display):
        super().__init__(display)

        # Title screen music
        Sounds.play_title_song()

        # Calculate position of game title and buttons
        self.display_width_factor = display.get_width() / 800
        self.display_height_factor = display.get_height() / 640
        self.GAME_TITLE_CENTER = (self.display_width_factor * 400,
                                  self.display_height_factor * 200)
        self.PLAY_BUTTON_CENTER = (self.GAME_TITLE_CENTER[0],
                                   self.GAME_TITLE_CENTER[1] +
                                   self.display_height_factor * 150)
        self.SCORE_BUTTON_CENTER = (self.PLAY_BUTTON_CENTER[0],
                                    self.PLAY_BUTTON_CENTER[1] + 50)

        # Create game title and buttons
        self.game_title = TextBox(self.display, self.GAME_TITLE_CENTER,
                                  self.GAME_TITLE_FONT_SIZE, "METEORITOS")
        self.play_button = TextButton(self.display, self.PLAY_BUTTON_CENTER,
                                      self.BUTTON_FONT_SIZE, "PLAY GAME")
        self.score_button = TextButton(self.display, self.SCORE_BUTTON_CENTER,
                                       self.BUTTON_FONT_SIZE, "HIGH SCORES")

        # Create background asteroids
        self.asteroids = pg.sprite.Group()
        for i in range(12):
            if i % 3 == 0:
                self.asteroids.add(SmallAsteroid(display))
            elif i % 3 == 1:
                self.asteroids.add(MediumAsteroid(display))
            else:
                self.asteroids.add(BigAsteroid(display))
コード例 #3
0
ファイル: Bullet.py プロジェクト: juanfdg/Asteroids
    def __init__(self, screen, x, y, direction):
        super().__init__(x, y, direction, [
            self.SPEED_MODULE * cos(radians(direction)),
            self.SPEED_MODULE * sin(radians(direction))
        ], screen, 'bullet.png', 0.6)

        self.age = 0  # Number of cycles since the bullet was cast

        Sounds.shoot_sound()
コード例 #4
0
ファイル: SmallSaucer.py プロジェクト: juanfdg/Asteroids
    def update(self):
        self.bullet_timer = (self.bullet_timer + 1) % 100
        if self.bullet_timer == 75:
            self.shoot()

        super().update()

        # Decreases sound_fader on each frame
        self.sound_fader = self.sound_fader / 1.035
        Sounds.small_saucer_sound(self.sound_fader)
コード例 #5
0
    def __init__(self, display):
        super().__init__(display)

        # Setting sounds
        self.sounds = Sounds()
        Sounds.stop_title_song()

        # Factor for decreasing of the beat period
        self.period_factor = 3e-4

        # Create a score counter to assign each player's score
        self.score_counter = ScoreCounter()

        # Create players
        self.player = BotPlayer(self,
                                display.get_width() // 2,
                                display.get_height() // 2, 1, 1)
        self.players = [self.player]
        self.visible_players = pg.sprite.Group()
        self.visible_players.add(self.player)

        # Round counter
        self.round = 1

        # Flag for game over
        self.game_over = False

        # Create background asteroids
        self.asteroids = pg.sprite.Group()
        for i in range(12):
            if i % 3 == 0:
                self.asteroids.add(SmallAsteroid(display))
            elif i % 3 == 1:
                self.asteroids.add(MediumAsteroid(display))
            else:
                self.asteroids.add(BigAsteroid(display))

        # Create a variable for counting time since last saucer died
        self.frames_since_saucer = 0
        self.SAUCER_SPAWN_TIME = 2000
        self.saucers = pg.sprite.Group()

        # Add all sprites to active sprites group
        self.active_sprites.add(self.visible_players)
        for player in self.visible_players:
            self.active_sprites.add(player.shot_bullets)
        self.active_sprites.add(self.saucers)
        for saucer in self.saucers:
            self.active_sprites.add(saucer.saucer_shot_bullets)
        self.active_sprites.add(self.asteroids)
コード例 #6
0
ファイル: GameOverScreen.py プロジェクト: juanfdg/Asteroids
    def __init__(self, display, players, remain_sprites):
        super().__init__(display)

        # Game Over music
        Sounds.game_over()

        self.score_communicator = ScoreCommunicator('db/scores_db.csv')

        # Calculate position of game title and buttons
        self.display_width_factor = display.get_width() / 800
        self.display_height_factor = display.get_height() / 640
        self.SCREEN_TITLE_CENTER = (self.display_width_factor * 400,
                                    self.display_height_factor * 50)
        self.PLAYER_BOX_CENTER = (self.SCREEN_TITLE_CENTER[0],
                                  self.SCREEN_TITLE_CENTER[1] +
                                  self.display_height_factor * 200)
        self.PLAYER_NAME_ENTRY_BOX_CENTER = (self.PLAYER_BOX_CENTER[0],
                                             self.PLAYER_BOX_CENTER[1] +
                                             self.display_height_factor * 50)
        self.RETRY_BUTTON_CENTER = (self.PLAYER_NAME_ENTRY_BOX_CENTER[0],
                                    self.PLAYER_NAME_ENTRY_BOX_CENTER[1] +
                                    self.display_height_factor * 100)
        self.MAIN_MENU_BUTTON_CENTER = (self.RETRY_BUTTON_CENTER[0],
                                        self.RETRY_BUTTON_CENTER[1] +
                                        self.display_width_factor * 50)

        # Create screen title and buttons
        self.screen_title = TextBox(self.display, self.SCREEN_TITLE_CENTER,
                                    self.SCREEN_TITLE_FONT_SIZE, "GAME OVER")
        self.player_box = TextBox(self.display, self.PLAYER_BOX_CENTER,
                                  self.PLAYER_NAME_ENTRY_BOX_FONT_SIZE)
        self.player_name_entry_box = TextEntryBox(
            self.display, self.PLAYER_NAME_ENTRY_BOX_CENTER,
            self.PLAYER_NAME_ENTRY_BOX_FONT_SIZE)
        self.retry_button = TextButton(self.display, self.RETRY_BUTTON_CENTER,
                                       self.RETRY_BUTTON_FONT_SIZE, "RETRY")
        self.main_menu_button = TextButton(self.display,
                                           self.MAIN_MENU_BUTTON_CENTER,
                                           self.RETRY_BUTTON_FONT_SIZE,
                                           "MAIN MENU")

        # Receive players information
        self.players = []
        for player in players:
            self.players.append(player)

        # Create background with remaining game objects
        self.remain_sprites = remain_sprites
コード例 #7
0
ファイル: SettingsScreen.py プロジェクト: juanfdg/Asteroids
    def update(self, event):
        super().update(event)

        # If esc is pressed, switch to previous scene
        if event.type == pg.KEYDOWN:
            if event.key == pg.K_ESCAPE:
                self.switch_to_scene(self.previous_scene)

        # If default is pressed change sound_param
        self.default.update(event)
        if self.default.get_clicked():
            Sounds.sound_param = 1
            Sounds.unpause_title_song()

        # If bonus is pressed change sound_param
        self.bonus.update(event)
        if self.bonus.get_clicked():
            Sounds.sound_param = 2
            Sounds.unpause_title_song()

        # If none is pressed change sound_param
        self.none.update(event)
        if self.none.get_clicked():
            Sounds.sound_param = 0
            Sounds.pause_title_song()

        # If resume button is clicked, back to the previous scene
        self.resume_button.update(event)
        if self.resume_button.get_clicked():
            self.switch_to_scene(self.previous_scene)

        # If log button is clicked, switch logging state of previous scene, if possible
        self.log_button.update(event)
        if self.log_button.get_clicked():
            if self.previous_scene.__class__.__name__ == "GameScreen":
                self.previous_scene.logging = not self.previous_scene.logging

        # If main menu button is clicked, switches to title screen
        self.main_menu_button.update(event)
        if self.main_menu_button.get_clicked():
            self.switch_to_scene(TitleScreen.TitleScreen(self.display))
コード例 #8
0
    def kill(self):
        for bullet in self.saucer_shot_bullets:
            bullet.kill()
        super().kill()

        Sounds.big_explosion_sound()
コード例 #9
0
class TrainerScreen(ScreenBase):
    def __init__(self, display):
        super().__init__(display)

        # Setting sounds
        self.sounds = Sounds()
        Sounds.stop_title_song()

        # Factor for decreasing of the beat period
        self.period_factor = 3e-4

        # Create a score counter to assign each player's score
        self.score_counter = ScoreCounter()

        # Create players
        self.player = BotPlayer(self,
                                display.get_width() // 2,
                                display.get_height() // 2, 1, 1)
        self.players = [self.player]
        self.visible_players = pg.sprite.Group()
        self.visible_players.add(self.player)

        # Round counter
        self.round = 1

        # Flag for game over
        self.game_over = False

        # Create background asteroids
        self.asteroids = pg.sprite.Group()
        for i in range(12):
            if i % 3 == 0:
                self.asteroids.add(SmallAsteroid(display))
            elif i % 3 == 1:
                self.asteroids.add(MediumAsteroid(display))
            else:
                self.asteroids.add(BigAsteroid(display))

        # Create a variable for counting time since last saucer died
        self.frames_since_saucer = 0
        self.SAUCER_SPAWN_TIME = 2000
        self.saucers = pg.sprite.Group()

        # Add all sprites to active sprites group
        self.active_sprites.add(self.visible_players)
        for player in self.visible_players:
            self.active_sprites.add(player.shot_bullets)
        self.active_sprites.add(self.saucers)
        for saucer in self.saucers:
            self.active_sprites.add(saucer.saucer_shot_bullets)
        self.active_sprites.add(self.asteroids)

    def update(self, event):
        super().update(event)

        # Play back beat
        self.sounds.theme_song()
        self.sounds.set_period(self.sounds.period - self.period_factor)

        # If esc is pressed, switch to settings screen
        # if event.type == pg.KEYDOWN:
        #    if event.key == pg.K_ESCAPE:
        #       self.switch_to_scene(SettingsScreen(self.display, self, self.active_sprites))

        # Create a random saucer
        if not self.saucers.__nonzero__():
            self.frames_since_saucer += 1

        if self.frames_since_saucer >= self.SAUCER_SPAWN_TIME:
            i = randint(1, 2)

            # Saucers were removed for training purpose, because of their difficulty
            # if i == 1:
            #    saucer = BigSaucer(self.display)
            # else:
            #    saucer = SmallSaucer(self.display, self.round, self.player)

            #self.saucers.add(saucer)
            #self.active_sprites.add(self.saucers)
            #for saucer in self.saucers:
            #    self.active_sprites.add(saucer.saucer_shot_bullets)

            # Whenever the first saucer appears in a round, other saucers will spawn
            # more often until the round is over
            self.frames_since_saucer = 1200 + randint(1, 5) * 100

        # Todo: Create a round function to call rounds
        # If game is not over and all asteroids and saucers were destroyed, call a new round
        if not (self.asteroids.__nonzero__() or self.saucers.__nonzero__()):
            self.round += 1

            # Reset back beat period
            self.sounds.set_period(Sounds.INITIAL_PERIOD)

            # Create asteroids
            for i in range(12):
                if i % 3 == 0:
                    self.asteroids.add(SmallAsteroid(self.display))
                elif i % 3 == 1:
                    self.asteroids.add(MediumAsteroid(self.display))
                else:
                    self.asteroids.add(BigAsteroid(self.display))

            # Add all sprites to active sprites group
            self.active_sprites.add(self.visible_players)
            for player in self.visible_players:
                self.active_sprites.add(player.shot_bullets)
            self.active_sprites.add(self.asteroids)

            # Respawn all visible players in their initial positions
            for player in self.visible_players:
                player.vanish()
                player.respawn()

            # Restart saucers time counting
            self.frames_since_saucer = 0

        # Make all collisions
        for player in self.players:
            player.check_bullets_collision(self.asteroids)
            player.check_bullets_collision(self.saucers)
        for player in self.visible_players:
            player.check_self_collision(self.asteroids)
            player.check_self_collision(self.saucers)
            for saucer in self.saucers:
                player.check_self_collision(saucer.saucer_shot_bullets)

        # Look for respawned players and add them again to visible players
        for player in self.players:
            if player not in self.visible_players and player.is_visible:
                self.visible_players.add(player)
                self.active_sprites.add(player)

        # Update all active sprites but the players
        for sprite in self.active_sprites:
            if sprite.__class__.__name__ not in [
                    'Player', 'UserPlayer', 'BotPlayer'
            ]:
                sprite.update()
        for saucer in self.saucers:
            self.active_sprites.add(saucer.saucer_shot_bullets)

        # Update players based on active sprites
        for player in self.players:
            player.update(event, self.active_sprites)
            self.active_sprites.add(player.shot_bullets)

    # Render all text boxes and draw all sprites
    def render(self):
        self.display.fill(self.BG_COLOR)
        self.active_sprites.draw(self.display)
        for player in self.players:
            player.render()
            player.draw_debug()
コード例 #10
0
ファイル: Ship.py プロジェクト: juanfdg/Asteroids
    def kill(self):
        super().kill()

        Sounds.big_explosion_sound()
コード例 #11
0
ファイル: GameScreen.py プロジェクト: juanfdg/Asteroids
    def __init__(self, display):
        super().__init__(display)

        # Setting game logging
        self.logging = False

        # Dialog box to indicate logging
        self.display_width_factor = display.get_width() / 800
        self.display_height_factor = display.get_height() / 640
        self.LOG_BOX_CENTER = (self.display_width_factor * 400,
                               self.display_height_factor * 50)
        self.log_box = TextBox(self.display, self.LOG_BOX_CENTER,
                               self.LOG_BOX_FONT_SIZE, "* REC")

        # Setting sounds
        self.sounds = Sounds()
        Sounds.stop_title_song()

        # Factor for decreasing of the beat period
        self.period_factor = 3e-4

        # Create a score counter to assign each player's score
        self.score_counter = ScoreCounter()

        # Create players
        self.player = UserPlayer(self,
                                 display.get_width() // 2,
                                 display.get_height() // 2, 1)
        self.players = [self.player]
        self.visible_players = pg.sprite.Group()
        self.visible_players.add(self.player)

        # Round counter
        self.round = 1

        # Flag for game over
        self.game_over = False

        # Create background asteroids
        self.asteroids = pg.sprite.Group()
        for i in range(12):
            if i % 3 == 0:
                self.asteroids.add(SmallAsteroid(display))
            elif i % 3 == 1:
                self.asteroids.add(MediumAsteroid(display))
            else:
                self.asteroids.add(BigAsteroid(display))

        # Create a variable for counting time since last saucer died
        self.frames_since_saucer = 0
        self.SAUCER_SPAWN_TIME = 2000
        self.saucers = pg.sprite.Group()

        # Add all sprites to active sprites group
        self.active_sprites.add(self.visible_players)
        for player in self.visible_players:
            self.active_sprites.add(player.shot_bullets)
        self.active_sprites.add(self.saucers)
        for saucer in self.saucers:
            self.active_sprites.add(saucer.saucer_shot_bullets)
        self.active_sprites.add(self.asteroids)
コード例 #12
0
ファイル: SmallAsteroid.py プロジェクト: juanfdg/Asteroids
    def kill(self):
        super().kill()

        Sounds.small_explosion_sound()