Beispiel #1
0
    def __init__(self):
        """
            Initialize console, level, UI.
        """

        self.console = IO()  # Create a console

        self.ui = UI()  # Create user interface

        self.rng = RNG()
        self.noise = Noise(self.rng.rand)

        self.console.cprint(((W - len(G_LEVEL_MSG)) / 2, H / 2), G_LEVEL_MSG)  # Print generating level msg

        self.console.flush()  # And flush it

        self.dungeon = Dungeon(None)
        self.dungeon.levels[0] = Level((self.noise.getNoise, self.noise.getNoise1d), self.rng.rand, self.dungeon)
        self.dungeon.level = self.dungeon.levels[0]

        self.level = self.dungeon.level

        self.level.num = 0

        self.player = Player((PLAYERX, PLAYERY), self.level)  # Create player
        self.level.setPlayer(self.player)  # And put it into the level

        for x in range(self.level.w + 1):
            for y in range(self.level.h + 1):
                if self.level[x][y].retChar() == "<":
                    self.player.move((x, y), True, False)
                    break
Beispiel #2
0
class Game:
    """
        Main game class.
        Some basic functions. etc.
    """

    def __init__(self):
        """
            Initialize console, level, UI.
        """

        self.console = IO()  # Create a console

        self.ui = UI()  # Create user interface

        self.rng = RNG()
        self.noise = Noise(self.rng.rand)

        self.console.cprint(((W - len(G_LEVEL_MSG)) / 2, H / 2), G_LEVEL_MSG)  # Print generating level msg

        self.console.flush()  # And flush it

        self.dungeon = Dungeon(None)
        self.dungeon.levels[0] = Level((self.noise.getNoise, self.noise.getNoise1d), self.rng.rand, self.dungeon)
        self.dungeon.level = self.dungeon.levels[0]

        self.level = self.dungeon.level

        self.level.num = 0

        self.player = Player((PLAYERX, PLAYERY), self.level)  # Create player
        self.level.setPlayer(self.player)  # And put it into the level

        for x in range(self.level.w + 1):
            for y in range(self.level.h + 1):
                if self.level[x][y].retChar() == "<":
                    self.player.move((x, y), True, False)
                    break

    def mainLoop(self):
        """
            Main loop of game.
        """

        play = True  # The game started

        while play:
            self.level.draw(self.console)  # Draw level on self.console
            self.ui.draw(self.console)  # Draw UI
            self.console.flush()  # Flush console

            key = self.console.rkey()  # Read one key
            # From the console

            if key.c == 0:  # If no key was pressed
                for cell in self.level.redrawcells:  # Then redraw animated
                    self.level.drawCell(cell[0], cell[1], cell[2], cell[3], False)  # cells

            else:
                move = self.console.keyparse(key)  # Ask keyparser for move

                if move:  # Player has moved...
                    self.player.move(move)

                elif chr(key.c) == "x":  # ...Or hasn't.
                    # Build something
                    self.level[self.player.x][self.player.y].setType(False, False)

                    # Update cell
                    self.level.update((self.player.x, self.player.y))

                elif chr(key.c) == "z":
                    # Or dig
                    coords = self.console.askDirection()  # Get direction

                    if coords:
                        # Keyparser returned coordinates
                        (x, y) = coords
                        (x, y) = (x + self.player.x, y + self.player.y)

                        if self.level[x][y].isWall() and self.level[x][y].isDiggable():
                            # If you can dig that wall
                            self.level[x][y].setType(True, True)  # Dig it
                            self.level.update((x, y))  # And update

                elif chr(key.c) == "m":  # Show map for one _turn_
                    self.level.draw(self.console, forced=True)  # Draw level
                    self.console.flush()  # Ignoring FOV.

                    self.console.readKey()  # And wait for keypress

                elif chr(key.c) == "q":  # Quit
                    play = False

                elif chr(key.c) == "S":
                    self.save()
                elif chr(key.c) == "L":
                    self.console.cprint((5, 5), "%d" % self.level.num)
                    self.console.flush()
                    self.console.readKey()

                elif chr(key.c) == ">":
                    if self.level[self.player.x][self.player.y].retChar() == ">":
                        if self.level.num + 1 >= len(self.dungeon.levels):
                            self.dungeon.levels.append(
                                Level((self.noise.getNoise, self.noise.getNoise1d), self.rng.rand, self.dungeon)
                            )

                        self.level = self.dungeon.levels[self.player.level.num + 1]

                        self.level.setPlayer(self.player)
                        self.player.level = self.level

                        for x in range(self.level.w + 1):
                            for y in range(self.level.h + 1):
                                if self.level[x][y].retChar() == "<":
                                    self.player.move((x, y), True, False)
                                    break

                elif chr(key.c) == "<":
                    if self.level[self.player.x][self.player.y].retChar() == "<":
                        if self.level.num != 0:
                            self.level = self.dungeon.levels[self.player.level.num - 1]

                            self.level.setPlayer(self.player)
                            self.player.level = self.level

                            for x in range(self.level.w + 1):
                                for y in range(self.level.h + 1):
                                    if self.level[x][y].retChar() == ">":
                                        self.player.move((x, y), True, False)
                                        break

                for x in range(self.level.w):
                    for y in range(self.level.h):
                        if self.level[x][y].mob != None:
                            self.level[x][y].mob.ai()

            # This line is useful for stress testing the game.
            # self.player.move((random.randint(-1,1),random.randint(-1,1)))

            # End game if window is closed
            play = not libtcod.console_is_window_closed() and play

        def load():
            pass

        def save():
            pass