[英]Python 2D game movement issue
我目前正在處理的小型游戲項目存在問題。 我多次嘗試為我的游戲創造運動,不幸的是沒有任何效果,或者換句話說,我不知道如何開始才能讓它發揮作用。 如果有人能在正確的方向上給我小小的推動,那就太棒了。 最常見的錯誤是“can only concatenate list (not "int") to list”。
謝謝!
游戲的重點是收集寶藏並逃離怪物。 如果要檢查所有圖標等,可以使用 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')
您正在嘗試修改 map 移動內容(例如+1
)。 最好更換播放器的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')
目前主循環已關閉,因為每次迭代都從主菜單和 map 生成開始。 map 應該在開始時生成,最好將其拆分為 static 事物,例如寶藏和可以移動的事物(怪物和玩家)。 它將防止 map 被移動字符覆蓋的情況。 打印 map 時,首先加載 static 對象(例如display_map=static_map
),然后將所有動態對象添加到display_map
並打印。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.