打印RPG游戏的视觉地图

时间:2014-12-27 23:32:27

标签: python pygame

嗨,有男人和女孩,

所以我对Python相对较新(现在已经学习了大约2周)并且为了加强我的学习,我一直在努力建立自己的经典地下城/冒险游戏。游戏中有一个玩家,一个怪物(将来我希望包含5个或6个),一个钥匙和一个门。玩家必须首先搜索地牢,找到钥匙,然后找到门。此外,玩家可以拿起武器并攻击/躲避怪物。

如果玩家进入一个怪物所在的房间,那么这就会“唤醒怪物”并且它会开始追逐玩家,每回合都会靠近玩家。

我通过基于包含100个元组的列表(对于10 x 10 x-y坐标网格)打印出地图,在shell中构建了游戏的可视化表示。如果任何游戏元素的位置等于特定的x,y坐标,则它打印出一个代表性的数字(例如,如果玩家在第1,1点,它将在第1点打印出'X',1而不是'_'或如果它是怪物它将是'!')。

请参阅下文,了解当前地图的示例。方块目前很小(1个字符乘1个字符),我正在尝试将其扩展为3个字符乘3个字符(参见示例2),其中游戏元素值(如果有)打印在中心。

我遇到的问题是如何确定如何构建这些较大的方块,我很难过。随着越来越复杂/疯狂嵌套的for循环和if语句,我一直在搞错了一个多小时。想要避开这个问题,看看过去是否有人试图这样做/有任何方便的提示或建议。

提前感谢您的帮助! :)

乔伊

第一个脚本

def draw_map(p_loc, m_loc, prior_moves):
  left_side_indices = list(range(0, 100))
  del_multiple = 9

  #This for loop deletes every 9th element (ie. 9, 19, 29...99) and assigns the 
  #existing numbers from 0 to 98 to left_side_indices. The reason why I did this was 
  #because the cells that are on the right side of the grid must be printed differently 
  #than all of the other cells.
  for i in range(10):
    del left_side_indices[del_multiple]
    del_multiple = del_multiple + 9

  non_right_wall_grid = left_side_indices

  print('\n\n\n\n' + ' _' * 10)
  tile = '|{}'


  #unpacking the player location to x and y
  x, y = p_loc
  p_loc = (x, y)
  #unpacking the monster location to a and b (x and y coordinates respectively)
  a, b = m_loc
  m_loc = (a, b)

  #loops through the grid list which is a list of tuples (x, y)
  for idx, cell in enumerate(grid):
    if idx in non_right_wall_grid:
      if cell == player_loc:
        print(tile.format('X'), end="")
      elif cell == monster_loc and player.spotted_monster == True:
        print(tile.format('!'), end="")
      elif cell == door_loc and player.spotted_door == True:
        print(tile.format('O'), end="")
      elif cell in prior_moves:
        print(tile.format('.'), end="")
      else:
        print(tile.format('_'), end="")
    else:
      if cell == player_loc:
        print(tile.format('X|'))
      elif cell == monster_loc and player.spotted_monster == True:
        print(tile.format('!|'))
      elif cell == door_loc and player.spotted_door == True:
        print(tile.format('O'))
      elif cell in prior_moves:
        print(tile.format('.|'))
      else:
        print(tile.format('_|'))

  print('\n\n\n\n')

第一个脚本 - 输出

 _ _ _ _ _ _ _ _ _ _
|_|_|_|_|_|_|_|_|_|_|
|_|_|_|_|_|_|_|_|_|_|
|_|_|.|.|.|.|_|_|_|_|
|_|_|_|.|_|.|_|_|_|_|
|_|_|_|.|.|O|X|_|_|_|
|_|_|_|.|_|_|_|_|_|_|
|_|_|_|.|_|_|_|_|_|_|
|_|_|_|.|_|_|_|_|_|_|
|_|_|.|.|_|_|_|_|_|_|
|_|_|_|_|_|_|_|_|_|_|

You are currently in room (7, 5).
You currently do not have the key.
You are currently able to go ['LEFT', 'RIGHT', 'UP', 'DOWN'], type the appropriate word to check out that room.
Type QUIT to stop playing DUNGEON or HELP for help.

Player:  (7, 5)
Monster:  (2, 7)
Door:  (6, 5)
Key:  (9, 1)
Moves After Spotting:  0

下面的第二个脚本(这是我最终要努力建立的)

第二个脚本

print('\n\n ___' + '___' * 18 + '__ ')

for _ in range(10):
    print('|     ' * 10 + '|')
    print('|  {}  '.format('X') * 10 + '|')
    print('|_____' * 10 + '|')

print('\n\n')

第二个脚本 - 输出

 ___________________________________________________________ 
|     |     |     |     |     |     |     |     |     |     |
|  X  |  X  |  X  |  X  |  X  |  X  |  X  |  X  |  X  |  X  |
|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|
|     |     |     |     |     |     |     |     |     |     |
|  X  |  X  |  X  |  X  |  X  |  X  |  X  |  X  |  X  |  X  |
|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|
|     |     |     |     |     |     |     |     |     |     |
|  X  |  X  |  X  |  X  |  X  |  X  |  X  |  X  |  X  |  X  |
|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|
|     |     |     |     |     |     |     |     |     |     |
|  X  |  X  |  X  |  X  |  X  |  X  |  X  |  X  |  X  |  X  |
|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|
|     |     |     |     |     |     |     |     |     |     |
|  X  |  X  |  X  |  X  |  X  |  X  |  X  |  X  |  X  |  X  |
|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|
|     |     |     |     |     |     |     |     |     |     |
|  X  |  X  |  X  |  X  |  X  |  X  |  X  |  X  |  X  |  X  |
|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|
|     |     |     |     |     |     |     |     |     |     |
|  X  |  X  |  X  |  X  |  X  |  X  |  X  |  X  |  X  |  X  |
|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|
|     |     |     |     |     |     |     |     |     |     |
|  X  |  X  |  X  |  X  |  X  |  X  |  X  |  X  |  X  |  X  |
|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|
|     |     |     |     |     |     |     |     |     |     |
|  X  |  X  |  X  |  X  |  X  |  X  |  X  |  X  |  X  |  X  |
|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|
|     |     |     |     |     |     |     |     |     |     |
|  X  |  X  |  X  |  X  |  X  |  X  |  X  |  X  |  X  |  X  |
|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|

def draw_map():
  left_side_indices = list(range(0, 100))
  del_multiple = 9

  #This for loop deletes every 9th element (ie. 9, 19, 29...99) and assigns the existing numbers from 0 to 98 to left_side_indices
  for i in range(10):
    del left_side_indices[del_multiple]
    del_multiple = del_multiple + 9

  non_right_wall_grid = left_side_indices


  print('\n\n ___' + '___' * 18 + '__ ')

  for row in range(10):
    print('|     ' * 10 + '|')
    for column in range(10):
      if column in non_right_wall_grid:
        print('|  {}  '.format(tile_contents(player_loc, monster_loc, prior_moves, row, column)), end='')
      else:
        print('|  {}  |'.format(tile_contents(player_loc, monster_loc, prior_moves, row, column)))
    print('|_____' * 10 + '|')
  print('\n\n')


def tile_contents(p_loc, m_loc, prior_moves, row, column):
  x, y = p_loc
  p_loc = (x, y)
  a, b = m_loc
  m_loc = (a, b)
  coordinate = (column, row)

  if coordinate == p_loc:
    return('X')
  elif coordinate == m_loc and player.spotted_monster == True:
    return('!')
  elif coordinate == door_loc and player.spotted_door == True:
    return('O')
  elif coordinate in prior_moves:
    return('.')
  else:
    return('_')

1 个答案:

答案 0 :(得分:1)

如下:

def print_map():     
    print('\n\n ___' + '___' * 18 + '__ ')  
    for row in range(10):
        print('|     ' * 10 + '|')
        for column in range(10):
            print('|  {}  '.format(tile_contents(row, column)), end='')
        print('|')
        print('|_____' * 10 + '|')
    print('\n\n')


def tile_contents(row,column):
    # Put here all conditionals based on row, column positions of player, monsters
    # Return the character to be displayed in the middle of the cell

    return "X"

print_map()