Beispiel #1
0
class GameOverBox:
    """Displays a game over box."""

    def __init__(self, frame, image, text="Game Over", delay=2500, \
                 offset=None, color=None, font=None, sound=None):
        """Initializes the Game Over Box."""
        self.delay = delay
        self.image = None
        self.sound = sound
        if image:
            self.image = ImageBox(frame, image)
        self.text = TextBox(frame, text, offset, font, color)

    def play_sound(self):
        if self.sound:
            sound.play(snd_name)

    def draw(self):
        if self.image:
            self.image.draw()
        self.text.draw()

    def activate(self):
        self.draw()
        self.play_sound()
        pygame.display.update()
        pygame.time.delay(self.delay)
Beispiel #2
0
class GamePausedBox:
    """Displays a pause box."""
    def __init__(self,
                 frame,
                 image=None,
                 text="Game Paused - press any key to continue",
                 egen=None):
        """Initializes the Pause Box."""
        self.image = ImageBox(frame, image)
        self.text = TextBox(frame, text)
        if egen == None:
            egen = EventGenerator()
        self.egen = egen
        self.elis = AnyKeyListener(self.pause_ended)
        self.egen.add_listener(self.elis)

    def pause_ended(self):
        """Pause ended."""
        self.egen.remove_listener(self.elis)

    def draw(self):
        """Draws the Pause Box."""
        self.image.draw()
        self.text.draw()

    def activate(self):
        self.draw()
        pygame.display.update()
        self.egen.event_loop()
Beispiel #3
0
class GamePausedBox:
    """Displays a pause box."""

    def __init__(self, frame, image=None, text=config.PAUSE_TEXT, egen=None):
        """Initializes the Pause Box."""
        self.image = ImageBox(frame, image)
        self.text = TextBox(frame, text)
        if egen is None:
            egen = EventGenerator()
        self.egen = egen
        self.elis = AnyKeyListener(self.pause_ended)
        self.egen.add_listener(self.elis)

    def pause_ended(self):
        """Pause ended."""
        self.egen.remove_listener(self.elis)

    def draw(self):
        """Draws the Pause Box."""
        self.image.draw()
        self.text.draw()

    def activate(self):
        self.draw()
        pygame.display.update()
        self.egen.event_loop()
Beispiel #4
0
class TitleScreen:
    """Shows title image and menu."""
    def __init__(self, screen, egen, title_rect, image, menu, menu_rect, moves):
        self.screen = screen
        self.events = egen
        self.events.add_callback(self)
        frame = Frame(self.screen, title_rect)
        self.title = ImageBox(frame, image)
        frame = Frame(self.screen, menu_rect)
        self.menu = TextMenuBox(frame, menu, self.events, moves)

    def update(self):
        self.menu.draw()
        pygame.display.update()

    def run(self):
        """Shows the title menu."""
        self.screen.clear()
        self.title.draw()
        self.menu.draw()
        self.events.event_loop()
        self.menu.deactivate()
        self.events.remove_callback(self)
Beispiel #5
0
class TitleScreen:
    """Shows title image and menu."""
    def __init__(self, screen, egen, title_rect, image, menu, menu_rect,
                 moves):
        self.screen = screen
        self.events = egen
        self.events.add_callback(self)
        frame = Frame(self.screen, title_rect)
        self.title = ImageBox(frame, image)
        frame = Frame(self.screen, menu_rect)
        self.menu = TextMenuBox(frame, menu, self.events, moves)

    def update(self):
        self.menu.draw()
        pygame.display.update()

    def run(self):
        """Shows the title menu."""
        self.screen.clear()
        self.title.draw()
        self.menu.draw()
        self.events.event_loop()
        #self.menu.deactivate()
        self.events.remove_callback(self)
Beispiel #6
0
 def test_image_box(self):
     """Display image with colorful boxes."""
     ib = ImageBox(self.frame, "test_data/tiles.xpm")
     ib.draw()
     pygame.display.update()
Beispiel #7
0
class HighscoreBox:
    """
    View for high scores.
    This class manages entering names
    and storing the high scores in a file.
    """
    def __init__(self, frame, egen, highscores, highscore_image, textpos):
        """
        frame - Frame instance
        egen - EventGenerator instance
        """
        self.frame = frame
        self.egen = egen
        self.textpos = textpos
        self.highscores = highscores
        self.image = ImageBox(frame, highscore_image)
        
        # for entering new entries
        self.entering = False
        self.name = ""
        self.score = 0

    def draw(self):
        """Draws the highscore list."""
        self.image.draw()
        i = 0
        for score,name in self.highscores.scores:
            y_offset = i * 30
            text = "%8i - %s"%(score, name)
            self.frame.print_text(text, self.textpos+Vector(0,y_offset), \
                    DEMIBOLD_BIG, BLUE,\
                    )
            i += 1
        if self.entering:
            self.frame.print_text("High Score! Please enter your name:",\
                    self.textpos+Vector(0,y_offset+50), DEMIBOLD_BIG, BLUE)
            self.frame.print_text(self.name,\
                    self.textpos+Vector(0, y_offset+80), DEMIBOLD_BIG, BLUE)

    def enter_score(self, score):
        """
        If the given score fits into the list,
        a name will be requested.
        """
        if self.highscores.is_in_highscores(score):
            self.entering = True
            self.entered_score = score
            self.text = ''

    def entered(self, text):
        self.name = text

    def update(self):
        self.draw()
        pygame.display.update()

    def done(self, text=None):
        self.entering = False
        self.egen.listeners = []

    def activate(self):
        self.egen.add_callback(self)
        if self.entering:
            # add new score
            elis = TextEnteringListener(self.entered, self.done)
            self.egen.add_listener(elis)
            self.egen.event_loop()
            self.highscores.insert_entry(self.name, self.entered_score)
        # display highscores and wait for a key
        elis = AnyKeyListener(self.done)
        self.egen.add_listener(elis)
        self.egen.event_loop()
        self.egen.remove_listener(elis)
        self.egen.remove_callback(self)
 def test_image_box(self, frame, image_filename):
     """Display image with colorful boxes."""
     ib = ImageBox(frame, image_filename)
     ib.draw()
     pygame.display.update()
Beispiel #9
0
class HighscoreBox:
    """
    View for high scores.
    This class manages entering names
    and storing the high scores in a file.
    """
    def __init__(self, frame, egen, highscores, highscore_image, textpos):
        """
        frame - Frame instance
        egen - EventGenerator instance
        """
        self.frame = frame
        self.egen = egen

        self.textpos = Vector(textpos)
        self.highscores = highscores
        self.image = ImageBox(frame, highscore_image)

        # for entering new entries
        self.entering = False
        self.name = ""
        self.score = 0

    def draw(self):
        """Draws the highscore list."""
        self.image.draw()
        i = 0
        for score, name in self.highscores.scores:
            y_offset = i * 30
            text = "%8i - %s" % (score, name)
            self.frame.print_text(text, self.textpos + Vector(0, y_offset),
                    config.DEMIBOLD_BIG, config.BLUE)
            i += 1
        if self.entering:
            self.frame.print_text("High Score! Please enter your name:",
                    self.textpos + Vector(0, y_offset + 50),
                    config.DEMIBOLD_BIG, config.BLUE)
            self.frame.print_text(self.name,
                    self.textpos + Vector(0, y_offset + 80),
                    config.DEMIBOLD_BIG, config.BLUE)

    def enter_score(self, score):
        """
        If the given score fits into the list,
        a name will be requested.
        """
        if self.highscores.is_in_highscores(score):
            self.entering = True
            self.entered_score = score
            self.text = ''

    def entered(self, text):
        self.name = text

    def update(self):
        self.draw()
        pygame.display.update()

    def done(self, text=None):
        self.entering = False
        self.egen.listeners = []

    def activate(self):
        self.egen.add_callback(self)
        if self.entering:
            # add new score
            elis = TextEnteringListener(self.entered, self.done)
            self.egen.add_listener(elis)
            self.egen.event_loop()
            self.highscores.insert_entry(self.name, self.entered_score)
        # display highscores and wait for a key
        elis = AnyKeyListener(self.done)
        self.egen.add_listener(elis)
        self.egen.event_loop()
        self.egen.remove_listener(elis)
        self.egen.remove_callback(self)
Beispiel #10
0
 def test_image_box(self, frame, image_filename):
     """Display image with colorful boxes."""
     ib = ImageBox(frame, image_filename)
     ib.draw()
     pygame.display.update()
Beispiel #11
0
class Colors:
    def __init__(self, screen):
        self.screen = screen
        self.frame = Frame(self.screen, Rect(20, 20, 800, 700))
        self.tile_factory = TileFactory('data/colortiles.conf')
        self.current_level = 2
        self.level_loader = levels
        self.score = 0

        self.level = None
        self.player = None
        self.events = None
        self.ghosts = []
        self.update_mode = None
        self.status_box = None
        self.image_box = None

        self.create_level()
        self.create_player()
        self.create_ghosts()
        self.create_status_box()

        self.collided = False
        self.mode = None
        self.update_mode = self.update_ingame

    def create_level(self):
        self.otherFrame = Frame(self.screen, Rect(0, 0, 800, 700))
        self.image_box = ImageBox(self.otherFrame, "data/background.bmp")
        self.image_box.draw()
        tmap = TiledMap(self.frame, self.tile_factory)
        self.level = ColorsLevel(
            self.level_loader.getlevel(self.current_level), tmap,
            self.level_loader.getGhostSpeed(self.current_level))

    def draw(self):
        self.update_mode()
        self.level.draw()
        self.player.update()
        self.player.draw()
        for g in self.ghosts:
            g.update()
            g.draw()
        self.status_box.draw()
        pygame.display.update()
        self.check_collision(self.player.sprite.pos)
        time.sleep(0.005)

    def create_player(self):
        self.player = Player(
            self.frame, self.tile_factory,
            self.level_loader.getplayerpos(self.current_level), self.level)
        self.player.set_direction(DOWN)

    def run(self):
        startsound.play()
        pygame.mixer.music.load("music/shootingstars.ogg")
        pygame.mixer.music.play(-1)
        self.events = EventGenerator()
        self.events.add_listener(FigureMoveListener(self.player.move))
        self.events.add_listener(FigureMoveListener(self.unstuck_ghosts))
        self.events.add_listener(FigureColorListener(self.player.set_color))
        self.events.add_listener(ExitListener(self.exit_game))
        with draw_timer(self, self.events):
            self.events.event_loop()

    def create_ghosts(self):
        self.ghosts = []
        for pos in self.level_loader.getghostpos(self.current_level):
            self.ghosts.append(
                Ghost(self.frame, self.tile_factory, pos, self.level))

    def unstuck_ghosts(self, pos):
        for g in self.ghosts:
            g.unstuck()

    def check_collision(self, pos):
        if self.player.collision(self.ghosts):
            self.update_mode = self.update_die
            self.player.die()
            self.collided = True
        else:
            field = self.level.at(pos)
            if field in '123456':
                time.sleep(1)
                self.update_mode = self.update_level_complete
            elif field == 's':
                self.level.remove_soul(pos)
                self.score += 100

    def update_die(self):
        """finish movements"""
        if self.player.sprite.finished:
            time.sleep(1)
            self.player.lives = self.player.lives - 1
            if self.player.lives == 0:
                deadsound.play()
                self.complete_level(1)
                self.events.exit_signalled()
                self.player.lives = 4
                self.run()
            else:
                self.status_box.data['lives'] = self.player.lives
                self.score -= 50
                self.reset_level()
                self.events.empty_event_queue()
                self.update_mode = self.update_ingame

    def update_level_complete(self):
        """finish movement"""
        if self.player.sprite.finished:
            time.sleep(1)
            self.complete_level(self.current_level)

    def reset_level(self):
        self.player.sprite.pos = self.level_loader.getplayerpos(
            self.current_level)
        self.create_ghosts()

    def update_ingame(self):
        self.check_collision(self.player.sprite.pos)

    def create_status_box(self):
        frame = Frame(self.screen, Rect(700, 20, 200, 50))
        data = {
            'lives': 3,
            'level': self.current_level - 1,
        }
        self.status_box = DictBox(frame, data)

    def complete_level(self, current_level):
        self.current_level = current_level + 1
        if self.current_level > 7:
            pygame.quit()
            exit()
        self.frame = Frame(self.screen, Rect(32, 32, 720, 720))
        self.tile_factory = TileFactory('data/colortiles.conf')
        self.level_loader = levels

        self.level = None
        self.player = None
        self.events = None
        self.ghosts = []
        self.update_mode = None
        self.status_box = None

        self.create_level()
        self.create_player()
        self.create_ghosts()
        self.create_status_box()

        self.collided = False
        self.mode = None
        self.update_mode = self.update_ingame
        self.run()

    def exit_game(self):
        pygame.quit()
        exit()