Python Python Collections (2016, retired 2019) Dungeon Game Movement

Youssef Moustahib
Youssef Moustahib
7,776 Points

`Can someone please comment on Kennths code that I have included to show me what is happening?

Would be much appreciated:

import random
import os
# Draw grid
# Pick random location for the Player
# Pick random location for the exit door
# Pick random location for the monster
# Draw player in grid
# Take input for movement
# move player, unless invalid move (past edges of grid)
# check for win or loss
# clear screen, redraw grid



CELLS = [(0, 0), (1, 0), (2, 0), (3, 0), (4, 0),
         (0, 1), (1, 1), (2, 1), (3, 1), (4, 1),
         (0, 2), (1, 2), (2, 2), (3, 2), (4, 2),
         (0, 3), (1, 3), (2, 3), (3, 3), (4, 3),
         (0, 4), (1, 4), (2, 4), (3, 4), (4, 4),
]

def clear():
    if os.name == 'nt':
        os.system('cls')
    else:
        os.system('clear')

def moveplayer(player, move):
    x, y = player
    if move == "LEFT":
        x -= 1
    if move == "RIGHT":
        x +=1
    if move == "UP":
        y -= 1
    if move == "DOWN":
        y += 1
    return x, y



def getmoves(player):
    moves = ["LEFT", "RIGHT", "UP", "DOWN"]
    x, y = player
    if x == 0:
        moves.remove("LEFT")
    if x == 4:
        moves.remove("RIGHT")
    if y == 0:
        moves.remove("UP")
    if y == 4:
        moves.remove("DOWN")
    # if players y is == 0, they cant move up
    # if players y is == 4, they cant move down
    # if players x is == 0, they cant move left
    # if players x == 4, they cant move right
    return moves

def location():
    return random.sample(CELLS, 3)


monster, player, door = location()

while True:
    validmoves = getmoves(player)
    clear()
    print("Welcome to the dungeon")
    print("You're currently in room {}".format(player)) #fill with player position
    print("You can move with {}".format(', '.join(getmoves(player)))) #fill with available moves
    print("Enter QUIT to quit")

    move = input("> ").upper()

    if move == "QUIT":
        break
    if move in validmoves:
        player = moveplayer(player, move)
    else:
        print("\n ** the walls are hard! dont run into them! **\n")
        continue

    #good move? Change player position
    #bad move? dont change anything!
    #on the door? They win!
    # on the monster? They lose!
    #Otherwise, loop back around

[MOD: added ```python formatting]

Chris Freeman
Chris Freeman
Treehouse Moderator 58,878 Points

Can you please be more specific or narrow down what you need explained?