Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/hadoop/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我的第一个项目(Python)中奇怪的意外操作_Python - Fatal编程技术网

我的第一个项目(Python)中奇怪的意外操作

我的第一个项目(Python)中奇怪的意外操作,python,Python,不久前我开始学习Python,我决定用(几乎)纯Python创建一个老式ascii游戏,代码如下: import keyboard top=['╔','═','═','═','═','═','═','═','═','╗'] map1=['║','.','.','.','.','.','.','.','.','║'] map2=['║','@','.','.','.','.','.','.','.','║'] map3=['║','.','.','.','.','.','.','.','.','║

不久前我开始学习Python,我决定用(几乎)纯Python创建一个老式ascii游戏,代码如下:

import keyboard
top=['╔','═','═','═','═','═','═','═','═','╗']
map1=['║','.','.','.','.','.','.','.','.','║']
map2=['║','@','.','.','.','.','.','.','.','║']
map3=['║','.','.','.','.','.','.','.','.','║']
bot=['╚','═','═','═','═','═','═','═','═','╝']
place=1
was_pressed = False
print(*top, sep='')
print(*map1, sep='')
print(*map2, sep='')
print(*map3, sep='')
print(*bot, sep='')
while True:
    try:
        if keyboard.is_pressed('D') and place<=7:
            if not was_pressed:
                if '@' in map1:
                    map1[place], map1[place+1]=map1[place+1],map1[place]
                    place=place+1
                    print(*top, sep='')
                    print(*map1, sep='')
                    print(*map2, sep='')
                    print(*map3, sep='')
                    print(*bot, sep='')
                    was_pressed=True
                if '@' in map2:
                    map2[place], map2[place+1]=map2[place+1],map2[place]
                    place=place+1
                    print(*top, sep='')
                    print(*map1, sep='')
                    print(*map2, sep='')
                    print(*map3, sep='')
                    print(*bot, sep='')
                    was_pressed=True
                if '@' in map3:
                    map3[place], map3[place+1]=map3[place+1],map3[place]
                    place=place+1
                    print(*top, sep='')
                    print(*map1, sep='')
                    print(*map2, sep='')
                    print(*map3, sep='')
                    print(*bot, sep='')
                    was_pressed=True

        elif keyboard.is_pressed('A') and place >=2:
            if not was_pressed:
                if '@' in map1:
                    map1[place], map1[place - 1] = map1[place - 1], map1[place]
                    place = place - 1
                    print(*top, sep='')
                    print(*map1, sep='')
                    print(*map2, sep='')
                    print(*map3, sep='')
                    print(*bot, sep='')
                    was_pressed = True
                if '@' in map2:
                    map2[place], map2[place -1] = map2[place - 1], map2[place]
                    place = place - 1
                    print(*top, sep='')
                    print(*map1, sep='')
                    print(*map2, sep='')
                    print(*map3, sep='')
                    print(*bot, sep='')
                    was_pressed = True
                if '@' in map3:
                    map3[place], map3[place -1] = map3[place - 1], map3[place]
                    place = place - 1
                    print(*top, sep='')
                    print(*map1, sep='')
                    print(*map2, sep='')
                    print(*map3, sep='')
                    print(*bot, sep='')
                    was_pressed = True

        elif keyboard.is_pressed('W'):
            if not was_pressed:
                if '@' in map2:
                    map2[place], map1[place]=map1[place],map2[place]
                    print(*top, sep='')
                    print(*map1, sep='')
                    print(*map2, sep='')
                    print(*map3, sep='')
                    print(*bot, sep='')
                    was_pressed=True
                if '@' in map3:
                    map3[place], map2[place] = map2[place], map3[place]
                    print(*top, sep='')
                    print(*map1, sep='')
                    print(*map2, sep='')
                    print(*map3, sep='')
                    print(*bot, sep='')
                    was_pressed = True

        elif keyboard.is_pressed('S'):
            if not was_pressed:
                if '@' in map1:
                    map1[place], map2[place] = map2[place], map1[place]
                    print(*top, sep='')
                    print(*map1, sep='')
                    print(*map2, sep='')
                    print(*map3, sep='')
                    print(*bot, sep='')
                    was_pressed = True
                if '@' in map2:
                    map2[place], map3[place] = map3[place], map2[place]
                    print(*top, sep='')
                    print(*map1, sep='')
                    print(*map2, sep='')
                    print(*map3, sep='')
                    print(*bot, sep='')
                    was_pressed = True

        else:
            was_pressed=False

    except:
        break
我按下S键,输出如下:

╔════════╗
║........║
║@.......║
║........║
╚════════╝
但结果是:

╔════════╗
║........║
║@.......║
║........║
╚════════╝
╔════════╗
║........║
║........║
║@.......║
╚════════╝

我不确定还有什么其他方法可以解释它,因为我不确定如何简化代码,但我会尝试将文件中的最后一个if语句更改为elif。当前,对按下的键的检查正在运行两次。因此,来自:

。。。
elif键盘。按下('S'):
如果没有按下按钮:
如果map1中的“@”:
map1[place],map2[place]=map2[place],map1[place]
打印(f“将{map1[place]}、{map2[place]}移动到{map2[place]}{map1[place]}”)
打印(*顶部,九月=“”)
打印(*map1,sep='')
打印(*map2,sep='')
打印(*map3,sep='')
打印(*bot,sep='')
按下按钮=正确
如果map2中的“@”,则为“坏”,因为它运行了两次
map2[place],map3[place]=map3[place],map2[place]
打印(f“将{map2[place]}、{map3[place]}移动到{map3[place]}{map2[place]}”)
打印(*顶部,九月=“”)
打印(*map1,sep='')
打印(*map2,sep='')
打印(*map3,sep='')
打印(*bot,sep='')
按下按钮=正确
...

。。。
elif键盘。按下('S'):
如果没有按下按钮:
如果map1中的“@”:
map1[place],map2[place]=map2[place],map1[place]
打印(f“将{map1[place]}、{map2[place]}移动到{map2[place]}{map1[place]}”)
打印(*顶部,九月=“”)
打印(*map1,sep='')
打印(*map2,sep='')
打印(*map3,sep='')
打印(*bot,sep='')
按下按钮=正确
地图2中的elif'@:#现在只运行一次
map2[place],map3[place]=map3[place],map2[place]
打印(f“将{map2[place]}、{map3[place]}移动到{map3[place]}{map2[place]}”)
打印(*顶部,九月=“”)
打印(*map1,sep='')
打印(*map2,sep='')
打印(*map3,sep='')
打印(*bot,sep='')
按下按钮=正确
...

问题源于没有测试代码的一小部分,以及没有隔离逻辑分支。查看简单移动的逻辑轨迹:

            if '@' in map1:
                # Switch rows 1 & 2
                # Print the board
                was_pressed = True
            if '@' in map2:
                # Switch rows 2 & 3
                # Print the board
                was_pressed = True
问题是,即使您从第1行移动到第2行,您也会立即再次检查是否在第2行。。。如果你从第一行开始,你就是。由于您的支票从最上面一行向下移动,因此您将传播任何向下移动,
X
从播放器开始的位置,直接传播到最下面一行


要进行简单的修复,请将下面的检查更改为
elif
。对于一般修复,请学习跟踪您的位置,只需将
@
插入游戏板中的适当位置。

之间,如果未按下:
,代码将运行两次,因为一旦角色下移,它将检查是否位于移动到的位置。您需要将第二个
if
语句更改为
elif
,这样它就不会在不应该检查的时候进行检查。我以前也做过类似的事情,这对我来说很有效。

请提供预期结果。显示中间结果与预期结果的偏差。我们应该能够将单个代码块粘贴到文件中,运行它,并重现您的问题。您没有显示任何输出、否、跟踪,并且几乎所有发布的代码都处理无关的游戏显示,而不是定位。如果向下移动时@位于
map1
中,则将其移动到
map2
。然后检查@是否在
map2
中,并再次找到它-这次将其移动到
map3
。您需要以相反的顺序执行向下案例中的两个检查,或者将第二个
if
elif改为
elif
。啊,将第二个
if
改为
elif
非常有效,非常感谢!我很感激!将其更改为
elif
肯定有效,非常感谢,如果可以,我一定会设法用简单的坐标系跟踪我的
@
,谢谢!更改为
elif
效果很好,现在终于如愿了,非常感谢
            if '@' in map1:
                # Switch rows 1 & 2
                # Print the board
                was_pressed = True
            if '@' in map2:
                # Switch rows 2 & 3
                # Print the board
                was_pressed = True