简体   繁体   English

Python 2D游戏动作问题

[英]Python 2D game movement issue

I have a problem in my small game project on which I am currently working.我目前正在处理的小型游戏项目存在问题。 I tried many times to create movement for my game, unfortunately nothing works or in other words i don't know how to start in order it to work.我多次尝试为我的游戏创造运动,不幸的是没有任何效果,或者换句话说,我不知道如何开始才能让它发挥作用。 It would be awesome if someone would give me small push in the right direction.如果有人能在正确的方向上给我小小的推动,那就太棒了。 The most common Error that occurred was “can only concatenate list (not "int") to list“.最常见的错误是“can only concatenate list (not "int") to list”。

Thank you!谢谢!

The main point of the game is to collect the treasure and run away from the monster.游戏的重点是收集宝藏并逃离怪物。 If you want to check all icons etc. you can use function info().如果要检查所有图标等,可以使用 function info()。

import random

#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")

loop = True
while loop:
  
  #main menu
  
  menu = int(input('Welcome!\n''Select one of the option:\n''[1] to start the game\n''[2] to exit the game\n'))
  if menu == 1: 
    
    #player customization
    
    player_character = []
    pc = int(input('Please select one of available characters:\n''[1] - 🙄\n''[2] - 😧\n''[3] - 🤠\n''[4] - 😑\n'))
    if pc == 1:
      player_character = '🙄'
    elif pc == 2:
      player_character = '😧'
    elif pc == 3:
      player_character = '🤠'
    else:
      player_character = '😑'
    
    #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"]] = player_character
    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"]]='💰'

      # movment system
    print('Select [W] to move (positive y)')
    print('Select [A] to move (negative x)')
    print('Select [S] to move (negative y)')
    print('Select [D] to move (positive x)')
    movment_choice = input('Please select your movment choice - [W,A,S,D]\n')
    if movment_choice == 'W' or movment_choice == 'w':
      map[player['y_p']] = (map[player['y_p']] + 1)
      print(map[player['y_p']])
    elif movment_choice == 'A' or movment_choice == 'a':
      map[player['x_p']] = (map[player['x_p']] - 1)
      print(map[player['x_p']])
    elif movment_choice == 'S' or movment_choice == 's':
      map[player['y_p']] = (map[player['y_p']] - 1)
      print(map[player['y_p']])
    elif movment_choice == 'D' or movment_choice == 'd':
      map[player['x_p']] = (map[player['x_p']] + 1)
      print(map[player['x_p']])
    else:
      print('ERROR! Please select one of the options in the question') 
    print_map()

   
    
  #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')
      
   
    info()

  elif menu == 2:
    loop = False
  else:
    print('ERROR! Please select one of the options above!!\n')

You're trying to modify the map content on movement (eg +1 ).您正在尝试修改 map 移动内容(例如+1 )。 It would be better to change player's position:最好更换播放器的position:

    movment_choice = input(
        'Please select your movment choice - [W,A,S,D]\n')
    if movment_choice == 'W' or movment_choice == 'w':
        player['y_p'] = (player['y_p'] + 1)
        print(map[player['y_p']])
    elif movment_choice == 'A' or movment_choice == 'a':
        player['x_p'] = (player['x_p'] - 1)
        print(map[player['x_p']])
    elif movment_choice == 'S' or movment_choice == 's':
        player['y_p'] = (player['y_p'] - 1)
        print(map[player['y_p']])
    elif movment_choice == 'D' or movment_choice == 'd':
        player['x_p'] = (player['x_p'] + 1)
        print(player['x_p'])
    else:
        print('ERROR! Please select one of the options in the question')

Currently the main loop is off, as on each iteration it starts from the main menu and map generation.目前主循环已关闭,因为每次迭代都从主菜单和 map 生成开始。 The map should be generated on start and it would be better to split it between static things, like the treasure and things that can move (monsters and the player). map 应该在开始时生成,最好将其拆分为 static 事物,例如宝藏和可以移动的事物(怪物和玩家)。 It will prevent the situation when the map is overwritten by moving characters.它将防止 map 被移动字符覆盖的情况。 When printing the map, first load the static objects (eg display_map=static_map ), then add all dynamic objects to display_map and print it.打印 map 时,首先加载 static 对象(例如display_map=static_map ),然后将所有动态对象添加到display_map并打印。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM