[英]2D Python game map update after movement
我目前正在開發使用文本作為 python 中的紋理的 2D 游戲。 這個游戲的主要思想是收集寶藏並試圖逃離幽靈又名怪物。 經過一番努力,我終於為玩家和怪物添加了移動系統。 一切都在正常工作,因為坐標正在正確更改(函數 info() 將顯示它們),但是我的 map 沒有更新,我不知道如何更新它。
Map 為 12x12
#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")
生成玩家、怪物和寶藏等“對象”。 在玩家一代期間,將會有一代人的願景。
#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"]]='💰'
程序中使用的所有函數
#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
最后是玩家和怪物的移動系統
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()
我希望這很清楚。 如果它不能正常工作,我將發送一個完整版本的代碼。
謝謝你的幫助!
首先, map
是內置的 python。 建議不要將其用作變量名。
其次,我會讓 map 創建一個 function 並使用它返回的 object。
這是使用游戲 map 的set
和程序生成的示例代碼。 使用集合操作,我們將防止它們與 map 中的對象重疊。 我將使用tuple
作為職位。
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
循環中發生任何更新。 您可以使用另一個 function 來執行此操作。
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
一些輔助函數:
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))
function這一招沒有檢測到撿寶,照樣走鬼。 這些需要添加。
主循環看起來像這樣:
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.