Python 2D寻宝避怪游戏移动功能开发问题求助
Let's break down what's going wrong and get your player moving properly. That error you're hitting is a classic mix-up with how you're accessing and modifying your game map—let's dive in.
First: Why That Error Happens
The line throwing the error (like map[player['y_p']] = (map[player['y_p']] + 1)) is trying to add an integer (1) to an entire row of your map (which is a list). Python doesn't let you concatenate a list and an integer, hence the crash. You're trying to update the player's position, but you're modifying the wrong part of the map structure entirely.
Step 1: Rewrite the Movement Logic Correctly
Proper player movement needs to follow these steps:
- Calculate the new position based on input
- Check if the new position is valid (not a wall, within map bounds)
- Reset the old player position to its original state
- Update the player's coordinates
- Place the player at the new position
- Refresh the player's vision (since they moved)
Here's the fixed movement section, with comments explaining each change:
# Movement 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)') movement_choice = input('Please select your movement choice - [W,A,S,D]\n').lower() # Calculate new position new_x = player["x_p"] new_y = player["y_p"] if movement_choice == 'w': new_y -= 1 # W moves up (decreasing y, since map[0] is the top row) elif movement_choice == 'a': new_x -= 1 elif movement_choice == 's': new_y += 1 elif movement_choice == 'd': new_x += 1 else: print('ERROR! Please select one of the options in the question') print_map() info() continue # Skip rest of logic if input is invalid # Validate new position: not a wall and within playable bounds if 1 <= new_x <= map_width - 2 and 1 <= new_y <= map_height - 2 and map[new_y][new_x] != 1: # Reset old player position to a vision tile (matches your initial setup) map[player["y_p"]][player["x_p"]] = '' # Update player's coordinates to the new position player["x_p"] = new_x player["y_p"] = new_y # Place the player icon at the new position map[player["y_p"]][player["x_p"]] = player_character # Refresh player vision: clear old vision tiles, then draw new ones # First, reset old vision tiles back to empty (0) for y in range(max(1, player["y_p"] - player_view), min(map_height - 1, player["y_p"] + player_view + 1)): for x in range(max(1, player["x_p"] - player_view), min(map_width - 1, player["x_p"] + player_view + 1)): if map[y][x] == '': map[y][x] = 0 # Draw new vision tiles around the player's new position for y in range(player["y_p"] - player_view, player["y_p"] + player_view + 1): for x in range(player["x_p"] - player_view, player["x_p"] + player_view + 1): if 1 <= x <= map_width - 2 and 1 <= y <= map_height - 2 and map[y][x] == 0: map[y][x] = '' else: print("Can't move there! That's a wall or out of bounds.") print_map() info()
Step 2: Fix Other Hidden Bugs
Your code has a few more issues that will cause problems once movement works:
1. Broken Vision Calculation
Original line:
for j,element in enumerate(row[player["x_p"]]- player_view :player["x_p"]+1 + player_view):
This is incorrect—row[player["x_p"]] grabs a single tile from the row, then subtracts player_view (which will throw an error if the tile is a string like your player icon). Fix it to slice the row properly:
for j, element in enumerate(row[player["x_p"] - player_view : player["x_p"] + 1 + player_view]):
Apply this fix in your initial vision setup code too.
2. Monster/Treasure Spawn Bug
You're using map_width instead of map_height for vertical spawn positions:
monster = { 'y_m':random.randint(1, map_width - 2), } # Wrong! Should be map_height treasure = { 'y_t':random.randint(1, map_width - 2), } # Same mistake
Change map_width to map_height here to keep monsters/treasure within the map's vertical bounds.
3. info() Function Placement
You defined info() inside the menu == 1 block, which means it gets recreated every time the player starts a new game. Move it outside the while loop so it's only defined once.
Final Tips
- Test changes incrementally: first fix the movement error, then tackle vision and spawn bugs.
- If you want to track explored vs. unexplored areas later, consider using a separate "hidden map" to store original tile states instead of overwriting them with vision tiles.
内容的提问来源于stack exchange,提问作者AKaczus

