Exemplo n.º 1
0
nm = None
(wpr, wpc) = (None, None)
path = []

while nm != Constants.NO_DIR:
    nm = pf.next_move()

    # The following code is only to draw the path calculated by the PathFinder.
    (tmp_wpr, tmp_wpc) = pf.current_waypoint()
    if wpr != tmp_wpr or wpc != tmp_wpc:
        if len(path) != 0:
            reset_drawn_path()
        path = pf.current_path()
        draw_path(path)
        wpr = tmp_wpr
        wpc = tmp_wpc
    if g.player.pos[0] == 15 and g.player.pos[1] == 0:
        g.current_board().move(blocker, Constants.LEFT, 1)
    if g.player.pos[0] == 19 and g.player.pos[1] == 1:
        reset_drawn_path()
        path = pf.current_path()
        draw_path(path)
        g.current_board().place_item(blocker, 20, 0)

    # Now we use the direction to move the player and display the board.
    g.move_player(nm, 1)
    g.actuate_npcs(1)
    g.animate_items(1)
    redraw()
    time.sleep(0.1)
Exemplo n.º 2
0
             game.object_library = object_history
             game.save_board(1, current_file)
     break
 elif key == 'S':
     save_current_board()
     info_messages.append("Board saved")
 elif key == 'm':
     if menu_mode == 'full':
         menu_mode = 'hidden'
     else:
         menu_mode = 'full'
     game.update_menu_entry('main', None,
                            '\n=== Menu (' + menu_mode + ') ===')
 elif current_menu == 'main':
     if key == 'w':
         game.move_player(Constants.UP, 1)
     elif key == 's':
         game.move_player(Constants.DOWN, 1)
     elif key == 'a':
         game.move_player(Constants.LEFT, 1)
     elif key == 'd':
         game.move_player(Constants.RIGHT, 1)
     elif key == "k" and edit_mode:
         place_and_go(current_object, game.player.pos[0],
                      game.player.pos[1], Constants.DOWN)
     elif key == "i" and edit_mode:
         place_and_go(current_object, game.player.pos[0],
                      game.player.pos[1], Constants.UP)
     elif key == "j" and edit_mode:
         place_and_go(current_object, game.player.pos[0],
                      game.player.pos[1], Constants.LEFT)
Exemplo n.º 3
0
    'k': ['smoke'],
}, {
    'q': 'Why do mummies like Christmas so much?',
    'a': 'Because of all the wrapping.',
    'k': ['wrap'],
}]

while key != 'q':
    refresh_screen(game, p, current_menu)
    Utils.debug(f"Current game speed: {game_speed}")
    Utils.debug(f"Current menu: {current_menu}")
    key = Utils.get_key()

    if current_menu == 'main_menu':
        if key == 'w' or key == '8':
            game.move_player(cst.UP, 1)
        elif key == 's' or key == '2':
            game.move_player(cst.DOWN, 1)
        elif key == 'a' or key == '4':
            p.model = sprite_player['left']
            game.move_player(cst.LEFT, 1)
        elif key == 'd' or key == '6':
            p.model = sprite_player['right']
            game.move_player(cst.RIGHT, 1)
        elif key == '9':
            p.model = sprite_player['right']
            game.move_player(cst.DRUP, 1)
        elif key == '7':
            p.model = sprite_player['left']
            game.move_player(cst.DLUP, 1)
        elif key == '1':
Exemplo n.º 4
0
 while g.state != Constants.STOPPED:
     g.score += g.player.pos[1] - g.player.last_x
     if g.player.pos[1] - g.player.last_x > 0:
         # That is clearly where the difficulty should be:
         # the smaller the recuperation time, the bigger the score should be
         # awarded
         # 0.2 is hard
         # The timer automatically refill but with less and less time the further
         # the player goes.
         g.timer += 1 / g.current_level
         # On the other end, the player gains more points the further he goes
         g.score += int((g.player.pos[1] - g.player.last_x) - 1 /
                        (g.player.pos[1] - g.player.last_x))
     g.player.last_x = g.player.pos[1]
     if key == Utils.key.LEFT:
         g.move_player(Constants.LEFT, 1)
     elif key == Utils.key.RIGHT:
         g.move_player(Constants.RIGHT, 1)
     # elif key == Utils.key.DOWN:
     #     gravity_friction -= 0.1
     # elif key == Utils.key.UP:
     #     gravity_friction += 0.1
     elif key == Utils.key.SPACE:
         if g.player.dy == 0 and len(g.neighbors(1, g.player)) > 0:
             # Jump
             # Start with sound
             jump_wave_obj.play()
             g.player.max_y = g.player.pos[0] - 3
             g.player.dy = 1
     elif key == "X":
         g.stop()
Exemplo n.º 5
0
        b.place_item(d, r, c)
    # Redraw the screen and wait to see the nice animation of the algorithm working.
    # Otherwise it's instantanous.
    redraw_screen(g)
    time.sleep(0.1)
    # Now make recursive calls to fill the cells around this one.
    flood_filler(g, b, r + 1, c)
    flood_filler(g, b, r - 1, c)
    flood_filler(g, b, r, c + 1)
    flood_filler(g, b, r, c - 1)


# Using the Game object just to load the board easily
g = Game()
b = g.load_board("/home/arnaud/Code/Games/hgl-editor/Filler.json", 1)
g.player = Player(model=Graphics.Sprites.MAGE)
g.change_level(1)
# We need the player out of our way
g.move_player(Constants.LEFT, 5)

# Now let's flood the floor!!
flood_filler(g, b, 5, 6)

# Show the animated lake for 200 turns (~40 secondes)
i = 0
while i < 200:
    i += 1
    redraw_screen(g)
    g.animate_items(1)
    time.sleep(0.2)
Exemplo n.º 6
0
class GameNeighborTestCase(unittest.TestCase):
    def setUp(self):
        super().setUp()
        self.game = Game()
        self.board = self.game.load_board("hac-maps/kneighbors.json", 1)
        self.game.player = Player(name="player")
        self.game.change_level(1)
        self.tree26 = self.board.item(2, 6)
        self.treasure38 = self.board.item(3, 8)
        self.tree39 = self.board.item(3, 9)
        self.wall45 = self.board.item(4, 5)
        self.wall46 = self.board.item(4, 6)
        self.wall47 = self.board.item(4, 7)
        self.wall57 = self.board.item(5, 7)
        self.npc77 = self.board.item(7, 7)
        self.treasure1212 = self.board.item(12, 12)
        self.tree1310 = self.board.item(13, 10)
        self.npc168 = self.board.item(16, 8)

    def test_player_neighbors(self):
        testcases = (
            ([3, 3], []),
            ([3, 5], [self.tree26, self.wall45, self.wall46]),
            ([3, 6], [self.tree26, self.wall45, self.wall46, self.wall47]),
            ([3, 7], [self.tree26, self.wall46, self.wall47, self.treasure38]),
            ([4, 8], [self.wall47, self.wall57, self.treasure38, self.tree39]),
            ([5, 6], [self.wall45, self.wall46, self.wall47, self.wall57]),
        )

        for pos, expected_board_items in testcases:
            with self.subTest(str(pos)):
                self.game.player.pos = pos
                actual_board_items = self.game.neighbors()
                assertion_message = " ".join([
                    readable_board_items(expected_board_items),
                    "!=",
                    readable_board_items(actual_board_items),
                ])
                self.assertSetEqual(
                    set(expected_board_items),
                    set(actual_board_items),
                    assertion_message,
                )

    def test_npc_neighbors(self):
        def move_player_next_to_npc77():
            pos_next_to_npc = [7, 8]
            self.game.move_player(Constants.DOWN, pos_next_to_npc[0])
            self.game.move_player(Constants.RIGHT, pos_next_to_npc[1])
            self.assertEqual(pos_next_to_npc, self.game.player.pos)

        actual_board_items = self.game.neighbors(1, self.npc77)
        self.assertSetEqual(set(), set(actual_board_items))

        move_player_next_to_npc77()
        actual_board_items = self.game.neighbors(1, self.npc77)
        self.assertSetEqual({self.game.player}, set(actual_board_items))

    def dump_board_items(self):
        h, w = self.board.size
        for y in range(h):
            for x in range(w):
                boarditem = self.board.item(y, x)
                assert isinstance(boarditem, BoardItem)
                if isinstance(boarditem, BoardItemVoid):
                    continue
                print(readable_board_item(boarditem))