Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

My version of Dungeon game.

I'm fairly happy overall, but it mutated into a 'Minefield' game with a bigger grid and a function (collision) to tell you when you are close to a mine (instead of a monster). However I think this function is a bit scrappy and could be improved. Would like to add now levels (more mines per levels) and a score board.

import random

CELLS = []
for x in range(0,10):
    for y in range(0,10):
        CELLS.append((x,y))



def get_locations(diff):    
    monster = []
    for i in range(diff):
        monster.append(random.choice(CELLS))
    door = random.choice(CELLS)
    start = random.choice(CELLS)  
    if door in monster or start in monster or door == start:
        return get_locations(diff)


    return monster, door, start


def draw_map(player,pastmoves):
  print(' _  _  _  _  _  _  _  _  _  _ ')
  tile = '|{}'

  for idx, cell in enumerate(CELLS):
    if not idx in [9,19,29,39,49,59,69,79,89,99]:
      if cell == player:
        print(tile.format('X'), end='')
      elif cell in pastmoves:
        print(tile.format('*'), end='')
      else:
        print(tile.format('_'), end='')
    else:
      if cell == player:
        print(tile.format('X|'))
      elif cell in pastmoves:
        print(tile.format('*|'))
      else:
        print(tile.format('_|'))

def collison(mymove):
    newmove1 =  mymove[0]+1, mymove[1]
    newmove2 =  mymove[0]-1, mymove[1]
    newmove3 =  mymove[0], mymove[1]+1
    newmove4 =  mymove[0], mymove[1]-1
    newmove5 =  mymove[0]+1, mymove[1]+1
    newmove6 =  mymove[0]-1, mymove[1]-1
    newmove7 =  mymove[0]+1, mymove[1]-1
    newmove8 =  mymove[0]-1, mymove[1]+1


    potmove = newmove1,newmove2,newmove3,newmove4,newmove5,newmove6,newmove7,newmove8

    return  potmove        



def move_player(player, move):
  # player = (x, y)
  x, y = player

  if move == 'LEFT':
    y -= 1
  elif move == 'RIGHT':
    y += 1
  elif move == 'UP':
    x -= 1
  elif move == 'DOWN':
    x += 1

  return x, y


def get_moves(player):
  moves = ['LEFT', 'RIGHT', 'UP', 'DOWN']
  # player = (x, y)

  if player[1] == 0:
    moves.remove('LEFT')
  if player[1] == 9:
    moves.remove('RIGHT')
  if player[0] == 0:
    moves.remove('UP')
  if player[0] == 9:
    moves.remove('DOWN')

  return moves


def reveal_map(monster):
  print(' _  _  _  _  _  _  _  _  _  _ ')
  tile = '|{}'

  for idx, cell in enumerate(CELLS):
    if not idx in [9,19,29,39,49,59,69,79,89,99]:
      if cell in monster:
        print(tile.format('M'), end='')
      elif cell == door:
        print(tile.format('D'), end='')
      else:
        print(tile.format('_'), end='')
    else:
      if cell in monster:
        print(tile.format('M|'))
      elif cell == door:
        print(tile.format('D|'))
      else:
        print(tile.format('_|'))


print("Welcome to the Minefield")
diflvl = input("how many mines can you take?")
monster, door, player = get_locations(int(diflvl))

pastmoves = []


while True:
  moves = get_moves(player)  
  print("You're currently in {}".format(player))
  pastmoves.append(player)

  draw_map(player,pastmoves)

  print("You can move {}".format(','.join(moves)))
  print("Enter QUIT to quit")

  if [i for i in monster if i in collison(player)]:
    print("WARM")
  else:
    print("Cold")

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



  if move == 'QUIT':
    reveal_map(monster)
    break

  if move in moves:
    player = move_player(player, move)
  else:
    print("** Walls are hard, stop walking into them! **")
    continue

  if player == door:
    print("You escaped!")
    reveal_map(monster)
    break
  elif player in monster:
    print("BANG! you hit a mine!")
    reveal_map(monster)
    break

tag me your code

1 Answer

Try not to have code run outside of a function or class. Maybe put it into a function called Game or Main?

Other then that I like it! Looks fun to play. Maybe you could add levels, or a GUI next? I can't wait to see what you create!

Goodluck! --Ricky