我正在使用Python制作一个基本的文本冒险RPG游戏。我的问题在于移动系统,其中地牢被分成每个都带有坐标的正方形。向玩家询问移动位置的坐标,然后将其传递到函数move2,然后程序检查一堆if语句,类似于下面的语句,并打印地板的相应地图。每个坐标都有一个if语句,因此有40个不同的if语句,每个语句都有一个映射图像。问题是在要求玩家坐标后没有任何反应。程序在询问坐标后结束,但没有给出任何错误(我知道我正在输入正确的坐标。)
move = input("\n To move around the room Hero, simply think the coordinates of the tile you want to move to! However, you can only move one tile at a time Ex: You are at tile 5,4. You can move to 5,3 5,5 or 4,4")
move2(move)
我为糟糕的代码道歉。我确信有一个更好的方法可以做到这一点,但我还不知道......
def move2 (move):
while move == "5,1" "5,2" "5,3" "5,4" "5,5" "5,6" "5,7" "5,8" "4,1" "4,2" "4,3" "4,4" "4,5" "4,6" "4,7" "4,8" "3,1" "3,2" "3,3" "3,4" "3,5" "3,6" "3,7" "3,8" "2,1" "2,2" "2,3" "2,4" "2,5" "2,6" "2,7" "2,8" "1,1" "1,2" "1,3" "1,4" "1,5" "1,6" "1,7" "1,8":
if move == "5,3":
move = input("""
1 2 3 4 5 6 7 8
__ __ __ D_ __ __ __ __
1 |_ |_ |_ |_ |_ |_ |_ |_C|
2 |_ |_ |_ |_ |_ |_ |_ |_ |
3 |_ |?_|_ |_ |_ |_ |_ |_ |
4 |_ |_ |_ |_ |_ |_ |_ |_ |
5 |_ |_ |_x|_ |_ |_ |_ |_ |
D""")
答案 0 :(得分:5)
这会有所帮助,但你应该阅读一个教程:
while move in ("5,1", "5,2", "5,3", "5,4", ... etc):
# body
答案 1 :(得分:1)
正如其他人所指出的那样,
while move == "5,1" "5,2" "5,3" "5,4" "5,5" "5,6" "5,7" "5,8" "4,1" "4,2" "4,3" "4,4" "4,5" "4,6" "4,7" "4,8" "3,1" "3,2" "3,3" "3,4" "3,5" "3,6" "3,7" "3,8" "2,1" "2,2" "2,3" "2,4" "2,5" "2,6" "2,7" "2,8" "1,1" "1,2" "1,3" "1,4" "1,5" "1,6" "1,7" "1,8":
连接(粉碎在一起)所有的字符串。你想要的是:
while move in ("5,1", "5,2", "5,3", "5,4", "5,5", "5,6", "5,7", "5,8", "4,1", "4,2", "4,3", "4,4", "4,5", "4,6", "4,7", "4,8", "3,1", "3,2", "3,3", "3,4", "3,5", "3,6", "3,7", "3,8", "2,1", "2,2", "2,3", "2,4", "2,5", "2,6", "2,7", "2,8", "1,1", "1,2", "1,3", "1,4", "1,5", "1,6", "1,7", "1,8"):
但这也不是那么好。相反,我会使用更好的字符串匹配:
import re
while re.match(r'\d,\d', move):
答案 2 :(得分:0)
对于良好的编码实践,您应该将任何长的声明移动到代码的单独部分。一些简单的重新分解:
allowed_moves =["5,1", "5,2", "5,3", "5,4",...]
while move in allowed_moves:
etc...
另外,如果你正在寻找一个很好的资源来学习Python,这将帮助你解决这个以及无数其他的事情,这会影响你的编程生涯我建议如下:
http://www.greenteapress.com/thinkpython/
免费在线,由我的教授撰写。