Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import keyboard
- import time
- height = 10
- width = 8
- player = (5, 5)
- walls = [
- (1, 1), (4, 6), (3, 4)
- ]
- boxes = [(3, 3), (3, 2), (6, 6)]
- portals = [(4, 4), (2, 2)]
- def print_world():
- world = [[" "] * width for _ in range(height)]
- for wall in walls:
- world[wall[1]][wall[0]] = " XX "
- for portal in portals:
- world[portal[1]][portal[0]] = " :: "
- for box in boxes:
- world[box[1]][box[0]] = " [] "
- world[player[1]][player[0]] = "('')"
- print("-----" * width)
- for i in world:
- print("|" + " ".join(i) + "|")
- print("-----" * width)
- print()
- def is_in_world(coord):
- return coord[0] in range(width) and coord[1] in range(height)
- def get_action():
- # action = input()
- action = ""
- if keyboard.is_pressed("w"):
- action = "u"
- elif keyboard.is_pressed("s"):
- action = "d"
- elif keyboard.is_pressed("a"):
- action = "l"
- elif keyboard.is_pressed("d"):
- action = "r"
- return action
- shift = {
- "l": (-1, 0),
- "r": (1, 0),
- "u": (0, -1),
- "d": (0, 1)
- }
- print_world()
- while not (set(portals) <= set(boxes)):
- while True:
- action = get_action()
- if action in set("lrud"):
- break
- current_shift = shift[action]
- next_cell_1 = (player[0] + current_shift[0],
- player[1] + current_shift[1])
- if not is_in_world(next_cell_1) or next_cell_1 in walls:
- continue
- if next_cell_1 in boxes:
- next_cell_2 = (next_cell_1[0] + current_shift[0],
- next_cell_1[1] + current_shift[1])
- can_move_box = is_in_world(next_cell_2) and next_cell_2 not in walls and next_cell_2 not in boxes
- if not can_move_box:
- continue
- boxes.remove(next_cell_1)
- boxes.append(next_cell_2)
- player = next_cell_1
- print_world()
- time.sleep(0.5)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement