示例#1
0
 def open_menu(self):
     if self.comp_game.game_over: self.started = False
     pg.mixer.stop()
     Snd.bg_menu()
     pg.event.set_grab(False)
     self.comp_menu.open()
     pg.mouse.set_visible(False)
示例#2
0
 def preparation(self):
     """
     Do all actions per one frame
     """
     # Colliding
     points = Collider.rockets_meteors()
     wounds = Collider.ship_meteors(self.ship)
     self.comp_overlay.score.up(points)
     if wounds: self.comp_overlay.health.down()
     if self.comp_overlay.health.is_dead():
         self.ship.kill()
         Snd.ex_ship()
         Animation.on_sprite(
             "ship", self.ship,
             max(self.ship.rect.size) * Conf.Ship.ANIM_SCALE)
         self.losing_timer = Conf.System.FPS * Conf.Game.LOSE_DELAY
         self.game_over = True
         Snd.game_over()
     # Spawning
     if Conf.Meteor.BY_TIME:
         if self.meteor_timer == 0:
             self.meteor_timer = (Conf.System.FPS *
                                  Conf.Meteor.PERIOD) // 1000
             Spawner.meteor()
     else:
         Spawner.all_meteors()
     Spawner.all_pieces(False)
     # Decrementing timers
     self.meteor_timer = max(0, self.meteor_timer - 1)
     self.rocket_timer = max(0, self.rocket_timer - 1)
示例#3
0
文件: ship.py 项目: T1GIT/SpaceBattle
 def _wear_fire(self, with_fire: bool):
     if with_fire != self.with_fire:
         self.with_fire = with_fire
         self.image = pg.transform.rotate(
             self.texture_fire if with_fire else self.texture_normal,
             self.angle - 90)
         self.rect = self.image.get_rect(center=self.rect.center)
         if with_fire: Snd.engine()
示例#4
0
文件: ship.py 项目: T1GIT/SpaceBattle
 def shoot(self):
     rocket = Rocket()
     ctr = self.rect.center
     rad = radians(-self.angle)
     x = ctr[0] + self.half_height * cos(rad)
     y = ctr[1] + self.half_height * sin(rad)
     rocket.locate(x, y, -self.angle)
     Snd.shoot()
     rocket.add(Group.ROCKETS, Group.ALL)
示例#5
0
文件: menu.py 项目: T1GIT/SpaceBattle
    def create_menu(self, settings, about):
        """
        Create menus: Main. Responsible for setting up the main menu.
        A picture is selected for the background. Customizable theme, colors, font size, etc.
        A name entry line is added, a play button that redirects to the start function of the game,
        similarly with the about and exit buttons.
        """

        # Theme
        theme = pygame_menu.themes.Theme(
            selection_color=Conf.Menu.THEME_COLOR,
            title_bar_style=pygame_menu.widgets.
            MENUBAR_STYLE_NONE,  # Separating header and body
            title_offset=(Conf.Menu.Title.X_OFFSET,
                          Conf.Menu.Title.Y_OFFSET - 20),
            title_font=self.title_font,
            title_font_color=(255, 255, 255),
            title_font_size=Conf.Menu.Title.SIZE,
            background_color=Img.get_menu(),
            widget_font=self.widget_font,
            widget_font_color=(255, 255, 255),
            widget_font_size=40,
            widget_margin=(0, 40),
            menubar_close_button=False)

        # Initialisation
        menu = pygame_menu.Menu(
            Conf.Window.HEIGHT,
            Conf.Window.WIDTH,
            title='SPACE BATTLE',
            theme=theme,
            onclose=lambda: pygame_menu.events.DISABLE_CLOSE,
            mouse_motion_selection=True)

        # Layout
        menu.add_button('     Play     ',
                        self.window.start,
                        font_size=60,
                        margin=(0, 50))
        menu.add_button('   Settings   ', settings)
        menu.add_button('     Info     ', about)
        menu.add_button('     Exit     ', lambda: exit(69))

        # Sound
        self.engine.set_sound(pygame_menu.sound.SOUND_TYPE_CLICK_MOUSE,
                              Snd.click(),
                              volume=Snd.get_volume(Conf.Sound.Volume.SFX))
        menu.set_sound(self.engine, recursive=True)

        return menu
示例#6
0
 def ship_meteors(ship: Ship):
     """
     Checks the collision of a meteor and a ship.
     Causes an explosion animation if a collision occurs
     """
     touched = pg.sprite.spritecollide(ship, Group.METEORS, False)
     result = 0
     for meteor in touched:
         if Collider.collide_by_mask(ship, meteor):
             Snd.wound()
             Animation.on_sprite("meteor", meteor, max(meteor.rect.size))
             meteor.kill()
             result += 1
     return result
示例#7
0
 def rockets_meteors():
     """
     Checks the collision of a meteor and a rocket.
     Causes an explosion animation if a collision occurs
     """
     touched = pg.sprite.groupcollide(Group.METEORS, Group.ROCKETS, False, False)
     result = 0
     meteor: Meteor
     for meteor, rockets in touched.items():
         for rocket in rockets:
             if Collider.collide_by_mask(meteor, rocket):
                 Snd.ex_meteor()
                 if meteor.is_alive():
                     meteor.wound()
                     Animation.on_sprite("meteor", rocket, max(meteor.rect.size) / 2)
                 else:
                     Animation.on_sprite("meteor", meteor, max(meteor.rect.size))
                     meteor.kill()
                     result += 1
                 if Conf.Rocket.DESTROYABLE:
                     rocket.kill()
     return result
示例#8
0
 def show(self):
     Thread(target=lambda: (pg.time.delay(100), Img.cache())).start()
     Snd.bg_menu()
     self.comp_menu.open()
示例#9
0
 def close_menu(self):
     pg.mixer.stop()
     Snd.bg_game()
     self.comp_menu.close()
     pg.mouse.set_visible(False)
     pg.event.set_grab(True)