0
#!/usr/bin/env python

import random
import time
import os

class vars:
    running = 1

def win ():
    print("You escaped!")
    vars.running = 0
    time.sleep(4)
    return 0

def main ():
    char_loc = 11 #The characters current co-ordinates in XY format
    pos_char_loc = 11
    ex_y = random.randint(1, 5)
    ex_x = random.randint(1, 5) * 10
    ex_loc = ex_x + ex_y

    while vars.running == 1:
        os.system('CLS')
        x0 = ["#"] * 5
        x1 = ["#"] * 5
        x2 = ["#"] * 5
        x3 = ["#"] * 5
        x4 = ["#"] * 5
        if (char_loc >= 11  and char_loc <= 55):
            if (char_loc >= 11 and char_loc <= 15):
                i = 0; k = 11
                for x in range(0, 4):          
                    if char_loc == k:
                        x0.insert(i, '@')
                    else:
                        i += 1
                        k += 1 
            if (char_loc >= 21 and char_loc <= 25):
                i =0; k = 21
                for loop1 in range(0, 4):
                    if char_loc == k:
                        x1.insert(i, '@')
                    else:
                        i += 1
                        k += 1
            if (char_loc >= 31 and char_loc <= 35):
                i =0; k = 31
                for loop2 in range(0, 4):
                    if char_loc == k:
                        x2.insert(i, '@')
                    else:
                        i += 1
                        k += 1                                
            if (char_loc >= 41 and char_loc <= 45):
                i =0; k = 41
                for loop3 in range(0, 4):
                    if char_loc == k:
                        x3.insert(i, '@')
                    else:
                        i += 1
                        k += 1
            if (char_loc >= 51 and char_loc <= 55):
                i =0; k = 51
                for loop5 in range(0, 4):
                    if char_loc == k:
                        x4.insert(i, '@')
                    else:
                        i += 1
                        k += 1
            else:
                print("fail")

        print( x0[4],x1[4],x2[4],x3[4],x4[4])
        print( x0[3],x1[3],x2[3],x3[3],x4[3])
        print( x0[2],x1[2],x2[2],x3[2],x4[2])
        print( x0[1],x1[1],x2[1],x3[1],x4[1])
        print( x0[0],x1[0],x2[0],x3[0],x4[0])
        print(char_loc, ex_loc)
        if char_loc == ex_loc:
            win()         
        move = input()
        if move == "w" and (char_loc != 15 and char_loc != 25 and char_loc != 35 and char_loc != 45 and char_loc !=55):
            char_loc += 1
            print("up")
        elif move == "s" and (char_loc != 11 and char_loc != 21 and char_loc != 31 and char_loc != 41 and char_loc != 51):
            char_loc -= 1
            print("down")
        elif move == "a" and (char_loc != 11 and char_loc != 12 and char_loc != 13 and char_loc != 14 and char_loc != 15):
            char_loc -= 10
            print("left")
        elif move == "d" and (char_loc != 51 and char_loc != 52 and char_loc != 53 and char_loc != 54 and char_loc != 55):
            char_loc += 10
            print("right")
        else: print("You can't move there!")

if __name__ == '__main__': main()

我正在尝试制作一个简单的基于文本的游戏,您可以在其中围绕“#”网格移动“@”并尝试找到出口。我已经更改了代码,使我更容易在不添加或删除大量代码的情况下使网格更大或更小,并且它不断给我这个输出:

fail
# # # # #
@ # # # #
@ # # # #
@ # # # #
@ # # # #
11 52

我无法弄清楚它有什么问题!只有一个'@'应该出现:(我只是python的新手,所以如果你有任何改进的建议,请不要犹豫,发布它们!提前致谢,

4

1 回答 1

0

我认为“失败”发生是因为每次 char_loc 不在 51 和 55 之间时都会发生。

if (char_loc >= 11 and char_loc <= 15):
if (char_loc >= 21 and char_loc <= 25):
if (char_loc >= 31 and char_loc <= 35):
if (char_loc >= 41 and char_loc <= 45):
if (char_loc >= 51 and char_loc <= 55):
else:

我认为你在这里想要做的是使用 elif,它只会在之前的检查没有触发的情况下触发。

if (char_loc >= 11 and char_loc <= 15):
elif (char_loc >= 21 and char_loc <= 25):
elif (char_loc >= 31 and char_loc <= 35):
elif (char_loc >= 41 and char_loc <= 45):
elif (char_loc >= 51 and char_loc <= 55):
else:

关于多个 @ 符号,我认为这可能会起作用。目前您有:

if char_loc == k:
    x0.insert(i, '@')
else:
    i += 1
    k += 1

我认为你想要做的是:

if char_loc == k:
    x0.insert(i, '@')

i += 1
k += 1

由于您希望 k 在每次循环迭代时都更改。

我建议的最后一件事是,因为你有:

i =0; k = 21
i =0; k = 31
i =0; k = 41
i =0; k = 51

您可能想要添加

i =0; k = 11

给第一个。

希望有帮助。

于 2013-07-25T16:15:18.670 回答