简体   繁体   中英

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“.

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().

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 ). It would be better to change player's 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. 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). It will prevent the situation when the map is overwritten by moving characters. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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