首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >运动后2D Python游戏地图更新

运动后2D Python游戏地图更新
EN

Stack Overflow用户
提问于 2022-01-12 19:10:04
回答 1查看 106关注 0票数 -1

我目前的工作是2D游戏,这是使用文本作为一个纹理在python。这个游戏的主要思想是收集宝藏,并试图逃离鬼,又名怪物。经过大量的斗争,我终于为玩家和怪物增加了运动系统。随着坐标的适当变化(函数info()将显示它们),一切都正常工作,但是我的地图没有更新,我也不知道如何更新它。

地图是12x12

代码语言:javascript
复制
#map generation

map_width = 12
map_height = 12
player_view = 1
map = [([1] * map_width)] + ([([1] + ([0] * (map_width - 2)) + [1]) for _ in range(map_height - 2)]) + [([1] * map_width)]
def print_map():
  for row in map:
    print(*row, sep='\t')
    print("\n")

生成玩家、怪物和宝藏等“物品”。在玩家的世代中,会有未来的愿景产生。

代码语言:javascript
复制
#player generation + vision
    
  player = {
      "x_p":random.randint(1,map_width  - 2),
      "y_p":random.randint(1,map_height - 2),
      
  }
  map[player["y_p"]][player["x_p"]] = ‘’
  for i,row in enumerate(map[player["y_p"]- player_view :player["y_p"]+1 + player_view]):
    for j,element in enumerate(row[player["x_p"]- player_view :player["x_p"]+1 + player_view]):
      if (element == 0):
        map[player["y_p"] - player_view + i][player["x_p"] - player_view +j]=''

#monster generation

  monster = {
      'y_m':random.randint(1, map_width - 2),
  }
  possible_x = []
  for i,e in enumerate(map[monster['y_m']]):
    if e == 0:
      possible_x.append(i)
  monster['x_m'] = random.choice(possible_x)
  map[monster["y_m"]][monster["x_m"]]=''

#treasure generation

  treasure = {
      'y_t':random.randint(1, map_width - 2),
  }
  possible_x = []
  for i,e in enumerate(map[treasure['y_t']]):
    if e == 0:
      possible_x.append(i)
  treasure['x_t'] = random.choice(possible_x)
  map[treasure["y_t"]][treasure["x_t"]]=''

所有在程序中使用的函数

代码语言:javascript
复制
#printing info about characters, map dimensions and icons used

def info():
    print('\n')
    #print_map()
    #print('\n')
    print('Map dimentions are',map_width, 'x', map_height, '(including walls!)\n')
    print('Player -',player, 'Icon used:',player_character, '\n')
    print('Monster -',monster, 'Icon used: .\n')
    print('Treasure -',treasure, 'Icon used: .\n')
    print('Player vision value is', player_view,', and is represented with icon: .\n')
    print('Other icons used, 0 as empty area and 1 as walls. \n')

#generating number 1 or -1 randomly in order to select direction of monster movement

def monster_random_values():
  exclude = [0]
  movement_value = random.randint(-1,1)
  return my_custom_random() if movement_value in exclude else movement_value

最后是玩家和怪物的运动系统。

代码语言:javascript
复制
loop = True
while loop:

    # movment system
    movment_choice = input('Please select your movment choice - [W,A,S,D]\nIf you want to exit press [Q]\n')
    if movment_choice == 'W' or movment_choice == 'w':
        print_map()
        player['y_p'] = (player['y_p'] + 1)
        #print(map[player['y_p']])
        info()
    elif movment_choice == 'A' or movment_choice == 'a':
        player['x_p'] = (player['x_p'] - 1)
        #print(map[player['x_p']])
        info()
    elif movment_choice == 'S' or movment_choice == 's':
        player['y_p'] = (player['y_p'] - 1)
        #print(map[player['y_p']])
        info()
    elif movment_choice == 'D' or movment_choice == 'd':
        player['x_p'] = (player['x_p'] + 1)
        #print(player['x_p'])
        info()
    elif movment_choice == 'Q':
      print('Thank you for playing the game!\n See you soon!')
      loop = False
    else:
        print('ERROR! Please select one of the options in the question')

    #monster movment
    possible_movment = random.randint(1, 3)
    if possible_movment == 1:
      monster['y_m'] = (monster['y_m'] + monster_random_values())
      monster['x_m'] = (monster['x_m'] + monster_random_values())
    elif possible_movment == 2:
      monster['y_m'] = (monster['y_m'] + monster_random_values())
    elif possible_movment == 3:
      monster['x_m'] = (monster['x_m'] + monster_random_values())
      info()

我希望这是明确的。如果它不能正常工作,我将发送一个完整版本的代码。

谢谢你的帮助!

EN

回答 1

Stack Overflow用户

发布于 2022-01-12 22:16:58

首先,map是python内置的。建议不要将其用作变量名。

第二,我会使创建映射成为一个函数,并使用它返回的对象。

下面是一个使用set和游戏地图的过程生成的示例代码。使用集合的操作,我们将防止它们在地图中的重叠对象。我将使用tuple作为职位。

代码语言:javascript
复制
import random
BORDER = '1'

def create_map(map_width, map_height, num_treasures=1):
    locations = set((i, j) for i in range(map_height) for j in range(map_width))  # All available board coordinates
    player = (random.randint(1, map_width - 2), random.randint(1, map_height - 2))  # Player position
    locations = locations.difference({player})  # Remove player position from available positions
    border = ({(0, i) for i in range(map_width)}.union({(map_height - 1, i) for i in range(map_width)})).union(
        {(i, 0) for i in range(map_height)}.union({(i, map_height - 1) for i in range(map_height)}))  # All borders
    locations = locations.difference(border)  # Remove border from available locations
    vision = {(player[0] + dx, player[1] + dy) for dx in [-1, 0, 1] for dy in [-1, 0, 1]}.intersection(
        locations)  # 3 x 3 vision box. Player is not used since its location is not available in locations
    monster = random.choice(list(locations))  # Monster location selected randomly from available locations
    locations = locations.difference({monster})  # Remove monster from available locations
    treasures = set([])  # Do the same thing as monster with treasures in a loop
    for i in range(num_treasures):
        treasure = random.choice(list(locations))
        treasures.add(treasure)
        locations.remove(treasure)
    my_map = list(list([0] * map_width) for _ in range(map_height))  # Empty map
    my_map[player[0]][player[1]] = ''  # Set player
    my_map[monster[0]][monster[1]] = ''  # Set monster
    for x, y in border:  # Set border
        my_map[x][y] = BORDER
    for x, y in vision:  # Set vision
        my_map[x][y] = ''
    for x, y in treasures:  # Set treasures
        my_map[x][y] = ''
    return my_map, player, monster, treasures

最后,我没有看到在游戏while循环中发生任何更新。您可以使用另一个函数来完成这个任务。

代码语言:javascript
复制
def move(my_map, player, player_move):
    # Legal move inputs
    if player_move in ['a', 'A']:  
        player_move = (0, -1)
        vision_remove = [(-1, 1), (0, 1), (1, 1)]
    elif player_move in ['w', 'W']:
        player_move = (-1, 0)
        vision_remove = [(1, -1), (1, 0), (1, 1)]
    elif player_move in ['d', 'D']:
        player_move = (0, 1)
        vision_remove = [(-1, -1), (0, -1), (1, -1)]
    elif player_move in ['s', 'S']:
        player_move = (1, 0)
        vision_remove = [(-1, -1), (-1, 0), (-1, 1)]
    else:
        return False, my_map, player
    neighbors = [(-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 1), (1, -1), (1, 0), (1, 1)]
    # New position
    x = player[0] + player_move[0]
    y = player[1] + player_move[1]
    if my_map[x][y] != BORDER:  # Can't move into borders
        my_map[player[0]][player[1]] = 0
        my_map[x][y] = ''
        # Update vision while keeping treasure, ghost, border icons
        for dx, dy in vision_remove:  # Remove old vision
            value = my_map[player[0] + dx][player[1] + dy]
            my_map[player[0] + dx][player[1] + dy] = 0 if value == '' else value
        for dx, dy in neighbors:  # Add new vision
            value = my_map[x + dx][y + dy]
            my_map[x + dx][y + dy] = '' if value == 0 else value
        return True, my_map, (x, y)
    return False, my_map, player

一些助手函数:

代码语言:javascript
复制
def to_string(my_map):
    return '\n'.join('\t'.join(str(x) for x in y) for y in my_map)


def print_map(my_map):
    print(to_string(my_map))

移动功能并不能检测到捡起宝藏,走进鬼魂的状态。这些都需要增加。

主循环如下所示:

代码语言:javascript
复制
my_map, player, monster, treasures = create_map(12, 12, 3)
updated = True
while True:
    # movement system
    if updated:
        updated = False
        print_map(my_map)
    movement_choice = input('Please select your movment choice - [W,A,S,D]\nIf you want to exit press [Q]\n')
    if movement_choice in ['q', 'Q']:
        print('Thank you for playing the game!\n See you soon!')
        break
    if movement_choice not in ['w', 'W', 'a', 'A', 's', 'S', 'd', 'D']:
        print('ERROR! Please select one of the options in the question')
    updated, my_map, player = move(my_map, player, movement_choice)
    hit, my_map, monster = move_monster(my_map, monster)
    if hit:
        print('Oh no! The monster got you.')
        print_map(my_map)
        break
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70687119

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档