コード例 #1
0
ファイル: bots.py プロジェクト: LewB/GridBug
 def setup(self):
     ### Setup the game. ###
     self.tip = games.Message(value="Press <Enter> key to Move, <End> Key to Quit.",
                         color=color.blue,
                         size=24, x=320, y=40,
                         interval=0, is_collideable = False)
     self.tip._tickable = False
     self.tip.TYPE = "TP"
     games.gscreen.add(self.tip)
     self.refresh()
コード例 #2
0
 def end(self):
     """ End the game. """
     # Show 'game over' for 5 seconds.
     end_message = games.Message(value = "Game Over",
     size = 90, color = color.red,
     x = games.screen.width/2,
     y = games.screen.height/2,
     lifetime = 5 * games.screen.fps,
     after_death = games.screen.quit,
     is_collideable = False)
     games.screen.add(end_message)
コード例 #3
0
 def end(self):
     """ Завершает игру. """
     # 5-секундное отображение 'Game Over'
     end_message = games.Message(value="Game Over",
                                 size=90,
                                 color=color.red,
                                 x=games.screen.width / 2,
                                 y=games.screen.height / 2,
                                 lifetime=5 * games.screen.fps,
                                 after_death=games.screen.quit,
                                 is_collideable=False)
     games.screen.add(end_message)
コード例 #4
0
ファイル: main.py プロジェクト: mcredstone/python
 def update(self):
     image = games.load_image("Pizza.gif")
     if self.bottom > games.screen.height:
         gameovermessage = games.Message(value="GAME OVER",
                                         size=50,
                                         color=color.red,
                                         x=games.screen.width / 2,
                                         y=games.screen.height / 2,
                                         lifetime=5 * games.screen.fps,
                                         after_death=games.screen.quit)
         games.screen.quit()
     self.y += 3
コード例 #5
0
ファイル: 12_astrocrash-08.py プロジェクト: Snapman7/asteroid
    def advance(self):
        """ Переводит игру на очередной уровень. """
        self.level += 1
        life_up = ''
        if self.level in self.bonus:
            self.life.value += 1
            life_up = ' + bonus life'

        # зарезервированное пространство вокруг корабля
        BUFFER = 150
        self.speed_asteroid += 1
        # создание новых астеройдов
        for i in range(self.level):
            # вычислим x и y, чтобы от корабля они отстояли минимум на BUFFER пикселов
            # сначала выберем минимальные отступы по горизонтали(x-axis) и вертикали(y-axis)
            x_min = random.randrange(BUFFER)
            y_min = BUFFER - x_min

            # исходя из этих минимумов, сгенерируем расстояния от корабля по горизонтали и вертикали
            x_distance = random.randrange(x_min, games.screen.width - x_min)
            y_distance = random.randrange(y_min, games.screen.height - y_min)

            # исходя из этих расстояний, вычислим экранные координаты
            x = self.ship.x + x_distance
            y = self.ship.y + y_distance

            # если необходимо, вернём объект внутрь окна
            x %= games.screen.width
            y %= games.screen.height

            # создадим астеройд
            new_asteroid = Asteroid(game=self,
                                    x=x,
                                    y=y,
                                    size=Asteroid.LARGE,
                                    speed=self.speed_asteroid)
            games.screen.add(new_asteroid)

        # отображение номера уровня
        level_message = games.Message(value="Level " + str(self.level) +
                                      life_up,
                                      size=40,
                                      color=color.black,
                                      x=games.screen.width / 2,
                                      y=games.screen.width / 10,
                                      lifetime=3 * games.screen.fps,
                                      is_collideable=False)
        games.screen.add(level_message)

        # звуковой эффект перехода на новый уровень (кроме первого)
        if self.level > 1:
            self.sound.play()
コード例 #6
0
ファイル: bots.py プロジェクト: LewB/GridBug
 def end(self):
     ### End the game. ###
     games.gscreen._clear_events()
     games.gscreen.clear()
     # show 'Game Over' for 5 seconds
     end_message = games.Message(value = "Game Over",
                                 size = 80,
                                 color = color.red,
                                 x = games.gscreen.width/2,
                                 y = games.gscreen.height/2,
                                 # interval = 500 * games.gscreen.fps,
                                 # after_death = games.gscreen.quit,
                                 is_collideable = False)
     games.gscreen.add(end_message)
     self.refresh()
     for i in range(1,200,1):
         games.gscreen._wait_frame()
     games.pygame.quit()
     sys.exit(0)
コード例 #7
0
ファイル: 12_astrocrash-08.py プロジェクト: Snapman7/asteroid
 def end(self):
     """ Жизнь -- """
     self.life.value -= 1
     if self.life.value == 0:
         """ Завершает игру. """
         # 5-секундное отображение 'Game Over'
         end_message = games.Message(value="Game Over",
                                     size=90,
                                     color=color.red,
                                     x=games.screen.width / 2,
                                     y=games.screen.height / 2,
                                     lifetime=5 * games.screen.fps,
                                     after_death=games.screen.quit,
                                     is_collideable=False)
         games.screen.add(end_message)
         return True
     else:
         self.ship = Ship(game=self,
                          x=games.screen.width / 2,
                          y=games.screen.height / 2)
         games.screen.add(self.ship)
コード例 #8
0
    def advance(self):
        """ Advance to the next game level. """
        self.level += 1

        # amount of space around the ship to preserve when creating asteroids
        BUFFER = 150

        # create new asteroids
        for _ in range(self.level):
            # calculate an x an y at least BUFFER distance from the ship
            # choose minimum distance along x-axis and y-axis
            x_min = random.randrange(BUFFER)
            y_min = BUFFER - x_min

            # choose distance along x-axis and y-axis based on minimum distance
            x_distance = random.randrange(x_min, games.screen.width - x_min)
            y_distance = random.randrange(y_min, games.screen.height - y_min)

            # calculate location based on distance
            x = self.ship.x + x_distance
            y = self.ship.y + y_distance

            # wrap around screen, if necessary
            x %= games.screen.width
            y %= games.screen.height

            # create the asteroid
            new_asteroid = Asteroid(game=self, x=x, y=y, size=Asteroid.LARGE)
            games.screen.add(new_asteroid)

        # display level number
        level_message = games.Message(value = "Level " + str(self.level), size = 40, \
            color = color.yellow, x = games.screen.width / 2, y = games.screen.width / 10, \
            lifetime = 3 * games.screen.fps, is_collideable = False)
        games.screen.add(level_message)

        # play new level sound (except at first level)
        if self.level > 1:
            self.sound.play()
コード例 #9
0
 def end(self):
     """ End the game message. """
     end_message = games.Message(value = "Game Over", size = 90, color = color.red, \
         x = games.screen.width / 2, y = games.screen.height / 2, lifetime = 5 * games.screen.fps, \
         after_death = games.screen.quit, is_collideable = False)
     games.screen.add(end_message)
コード例 #10
0
class Game(object):
    """ The game itself."""
    def __init__(self):
        """ Initialize Game object."""
        # set level
        self.level = 0

        # load sound for level advance
        self.sound = games.load_sound("level.wav")

        # create score
        self.score = games.Text(value = 0, size = 30, color = color.white, top = 5, right = games.screen.width - 10, is_collideable = False)
        games.screen.add(self.score)

        # create Player's ship
        self.ship = Ship(game = self, x = games.screen.width/2,
        y = games.screen.height/2)
        games.screen.add(self.ship)
    def play(self):
        """ Play the game. """
        # begin theme music
        games.music.load("theme.mid")
        games.music.play(-1)

        # load and set background
        nebula_image = games.load_image("nebula.jpg")
        games.screen.background = nebula_image

        # advance to level 1
        self.advance()

        # start play
        games.screen.mainloop()

    def advance(self):
        """ Advance to the next game level. """
        self.level += 1

    def main():
    astrocrash = Game()
    astrocrash.play()

# amount of space around ship to preserve when creating asteroids
BUFFER = 150

# create new asteroids
for i in range(self.level):
    # calculate an x and y at least BUFFER distance from the ship

    # choose minimum distance along x-axis and y-axis
    x_min = random.randrange(BUFFER)
    y_min = BUFFER - x_min

    # choose distance along x-axis based on minimum distance
    x_distance = random.randrange(x_min, games.screen.width - x_min)
    y_distance = random.randrange(y_min, games.screen.height - y_min)

    # calculate location based on distance
    x = self.ship.x + x_distance
    y = self.ship.y + y_distance

    # wrap around screen, if necessary
    x %= games.screen.width
    y %= games.screen.height

    # create the asteroid
    new_asteroid = Asteroid(game = self, x = x, y = y, size = Asteroid.LARGE)

    games.screen.add(new_asteroid)

    # display level number
    level_message = games.Message(value = "Level " + str(self.level),
    size = 40,
    color = color.yellow,
    x = games.screen.width/2
    y = games.screen.height/2
    lifetime = 3 * games.screen.fps,
    is_collideable = False)

    # play new level sound (except at first level)
    if self.level > 1:
        self.sound.play()
    def end(self):
        """ End the game. """
        # Show 'game over' for 5 seconds.
        end_message = games.Message(value = "Game Over",
        size = 90, color = color.red,
        x = games.screen.width/2,
        y = games.screen.height/2,
        lifetime = 5 * games.screen.fps,
        after_death = games.screen.quit,
        is_collideable = False)
        games.screen.add(end_message)

class Wrapper(games.Sprite):
    """ A Sprite that wraps around the screen. """
    def update(self):
        """ Wrap sprite around the screen."""
        if self.top > games.screen.height:
            self.bottom = 0
        if self.bottom < 0:
            self.top = games.screen.height
        if self.left > games.screen.width:
            self.right = 0
        if self.right < 0:
            self.left = games.screen.width
    def die(self):
        """ Destroy self."""
        self.destroy()

class Collider(Wrapper):
    """ A Wrapper that can collide with another object. """
    def update(self):
        """ Check for overlapping sprites."""
        super(Collider, self).update()

        if self.overlapping_sprites:
            for sprite in self.overlapping_sprites:
                sprite.die()
            self.die()
    def die(self):
        """ Destroy self and leave explosion behind. """
        new_explosion = Explosion(x = self.x, y = self.y)
        games.screen.add(new_explosion)
        self.destroy()

class Asteroid(Wrapper):
    """ An asteroid which floats across the screen. """
    SMALL = 1
    MEDIUM = 2
    LARGE = 3
    POINTS = 30
    total = 0
    Asteroid.total += 1
    images = {SMALL: games.load_image("asteroid_small.bmp"),
              MEDIUM: games.load_image("asteroid_med.bmp"),
              LARGE: games.load_image("asteroid_big.bmp") }
    SPEED = 2

    def __init__(self, x, y, size):
        """ Initialize asteroid sprite. """
        super(Asteroid, self).__init__(
            image = Asteroid.images[size],
            x = x, y = y,
            dx = random.choice([1, -1]) * Asteroid.SPEED * random.random()/size,
            dy = random.choice([1, -1]) * Asteroid.SPEED * radom.random()/size)
        self.size = size
        self.game = game

    def update(self):
        """ Wrap around screen. """
        if self.top > games.screen.height:
            self.bottom = 0

        if self.bottom < 0:
            self.top = games.screen.height

        if self.left > games.screen.height
            self.right = 0
        if self.right < 0:
            self.left = games.screen.width
        # check if missile overlaps any other object
        if self.overlapping_sprites:
            for sprite in self.overlapping_sprites:
                sprite.die()

    def main():
        # establish background
        nebula_image = games.load_image("nebula.jpg")
        games.screen.background = nebula_image

        # create 8 asteroids
        for i in range(8):
            x = random.randrange(games.screen.width)
            y = random.randrange(games.screen.height)
            size = random.choice([Asteroid.SMALL, Asteroid.MEDIUM, Asteroid.LARGE])
            new_asteroid = Asteroid(x = x, y = y, size = size)
            games.screen.add(new_asteroid)

class Ship(Wrapper):
    """ The player's ship. """
    image = games.load_image("ship.bmp")
    VELOCITY_STEP = .03
    ROTATION_STEP = 3
    sound = games.load.sound("thrust.wav")
    # apply thrust based on up arrow key
    # Add SFX queue whenever key pressed
    if games.keyboard.is_pressed(games.K_UP):
        Ship.sound.play()
    # change velocity components based on ship's angle
    angle = self.angle * math.pi / 180 # convert to radians
    # sin and cos of velocity for angular movement
    self.dx += Ship.VELOCITY_STEP * math.sin(angle)
    self.dy += Ship.VELOCITY_STEP * -math.cos(angle)
    # cap velocity in each direction
    self.dx = min(max(self.dx, -Ship.VELOCITY_MAX), Ship.VELOCITY_MAX)
    self.dy = min(max(self.dy, -Ship.VELOCITY_MAX), Ship.VELOCITY_MAX)
    # create the ship
    the_ship = Ship(image = Ship(image = Ship.image,
    x = games.screen.width/2
    y = games.screen.height/2)
    games.screen.add(the_ship))
    def __init__(self, x, y):
        """ Initialize ship sprite. """
        super(Ship, self).__init__(image = Ship.image, x = x, y = y)
        self.missile_wait = 0
    def update(self):
        """ Rotate based on keys pressed. """
        if games.keyboard.is_pressed(games.K_LEFT):
            self.angle -= Ship.ROTATION_STEP
        if games.keyboard.is_pressed(games.K_RIGHT):
            self.angle += Ship.ROTATION_STEP
        # wrap the ship around screen
        if self.top > games.screen.height:
            self.bottom = 0
        if self.bottom < 0:
            self.top = games.screen.height
        if self.left > games.screen.width:
            self.right = 0
        if self.right < 0:
            self.left = games.screen.width
        # if waiting until the ship can fire next, decrease wait
        if self.missile_wait > 0:
            self.missile_wait -= 1
        # fire missile if spacebar pressed and missile wait is over
        if games.keyboard.is_pressed(games.K_SPACE) and self.missile_wait = 0:
            new_missile = Missile(self.x, self.y, self.angle)
            games.screen.add(new_missile)
            self.missile_wait = Ship.MISSILE_DELAY
    def die(self):
        """ Destroy ship and end game. """
        self.game.end()
        super(Ship, self).die()
コード例 #11
0
 def end_game(self):
     """ End the game. """
     end_message = games.Message(value = "Game Over", size = 90, color = color.red, x = games.screen.width / 2, y = games.screen.height / 2, lifetime = 5 * games.screen.fps, after_death = games.screen.quit)
     games.screen.add(end_message)