我刚写了这个龙与地下城小游戏,还没有完成,我没有写龙功能或用户撞墙时显示错误的功能。我只想将玩家“X”移动多少次,但我不能。这是代码:
import random
import os
def clear_screen():
os.system('cls' if os.name == 'nt' else 'clear')
dungeon = [(0,0),(0,1),(0,2),(0,3),(0,4),
(1,0),(1,1),(1,2),(1,3),(1,4),
(2,0),(2,1),(2,2),(2,3),(2,4),
(3,0),(3,1),(3,2),(3,3),(3,4),
(4,0),(4,1),(4,2),(4,3),(4,4)
]
def first_random_position():
return(random.choice(dungeon))
def make_dungeon(player_position):
print(" _ _ _ _ _")
for cell in dungeon:
y = cell[1]
if y < 4:
if cell == player_position:
print("|X", end = "")
else:
print("|_", end = "")
elif cell == player_position:
print("|X|")
else:
print("|_|")
def move_player(position,m_input):
x,y = position
if m_input.upper() == "UP":
x -= 1
elif m_input.upper() == "LEFT":
y -= 1
elif m_input.upper() == "RIGHT":
y += 1
elif m_input.upper() == "DOWN":
x += 1
position = x,y
return(x,y)
def main():
print("Welcome to the Dungeon!")
input("Press 'Enter' on your keyboard to start the game!")
first_pos = first_random_position()
make_dungeon(first_pos)
print("You are currently in room {}".format(first_pos))
print("Enter LEFT , RIGHT , UP and DOWN to move the 'X'")
print("Enter 'QUIT' to quit")
main_input = input("\n")
location = move_player(first_pos,main_input)
clear_screen()
make_dungeon(location)
main()
如您所见,我只能移动 X 一次,但我希望能够移动任意多次,但我不知道如何,我猜我应该写一个 while 循环吗?我试过了,但我失败了,我真的需要你的帮助。谢谢
相关分类