コード例 #1
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))
コード例 #2
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)
コード例 #3
0
ファイル: HighScoresScreen.py プロジェクト: juanfdg/Asteroids
    def __init__(self, display):
        super().__init__(display)

        # 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.BACK_BUTTON_CENTER = (self.SCREEN_TITLE_CENTER[0] -
                                   self.display_height_factor * 250,
                                   self.SCREEN_TITLE_CENTER[1] +
                                   self.display_height_factor * 50)
        self.CLEAR_SCORE_BUTTON_CENTER = (self.SCREEN_TITLE_CENTER[0] +
                                          self.display_height_factor * 250,
                                          self.BACK_BUTTON_CENTER[1])
        self.FIRST_SCORE_BOX_CENTER = (self.SCREEN_TITLE_CENTER[0],
                                       self.CLEAR_SCORE_BUTTON_CENTER[1] +
                                       self.display_height_factor * 50)

        # Create screen title and buttons
        self.screen_title = TextBox(self.display, self.SCREEN_TITLE_CENTER,
                                    self.SCREEN_TITLE_FONT_SIZE, "HIGH SCORES")
        self.back_button = TextButton(self.display, self.BACK_BUTTON_CENTER,
                                      self.BUTTON_FONT_SIZE, "BACK TO MENU")
        self.clear_score_button = TextButton(self.display,
                                             self.CLEAR_SCORE_BUTTON_CENTER,
                                             self.BUTTON_FONT_SIZE,
                                             "CLEAR SCORES")

        # Create vector of text boxes representing the scores table
        self.score_text_box = []

        # Create score communicator
        path = os.path.dirname(__file__)
        path = os.path.join(path, os.pardir, os.pardir, 'db', 'scores_db.csv')
        self.score_communicator = ScoreCommunicator(path)

        # 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))
コード例 #4
0
    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)
コード例 #5
0
import pygame
from models.asteroid.BigAsteroid import BigAsteroid

if __name__ == '__main__':
    FPS = 60
    SCREEN_W = 800
    SCREEN_H = 600
    SIZE = (SCREEN_W, SCREEN_H)

    pygame.init()
    screen = pygame.display.set_mode((640, 480))
    clock = pygame.time.Clock()
    pygame.display.set_caption('Asteroid Destruction Testing')

    asteroids = pygame.sprite.Group()
    asteroids.add(BigAsteroid(screen))

    while True:
        ev = pygame.event.poll()
        if ev.type == pygame.QUIT:
            break
        elif ev.type == pygame.KEYDOWN:
            if ev.key == pygame.K_ESCAPE:
                break
            elif ev.key == pygame.K_SPACE:
                for asteroid in asteroids:
                    asteroid.kill()

        screen.fill((0, 0, 0))

        asteroids.update()
コード例 #6
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)