基于文本的游戏错误地在'#'的网格上显示'@'和'+'

时间:2013-07-26 13:50:26

标签: python if-statement for-loop python-3.x text-based

#!/usr/bin/env python

import random
import time
import os

class vars:

    running = 1

class ex:
    placed = 0
    y = 0
    x = 0
    loc = 0

def win ():

    print("You escaped!")
    time.sleep(2)
    ex.placed = 0

def main ():

    char_loc = 11 #The characters current co-ordinates in XY format
##    ex.y = random.randint(1, 10)
##    ex.x = random.randint(1, 10) * 10
##    ex.loc = ex.x + ex.y


    while vars.running == 1:

        while ex.placed == 0:
            ex.y = random.randint(1, 10)
            ex.x = random.randint(1, 10) * 10
            ex.loc = ex.x + ex.y
            ex.placed = 1

        os.system('CLS')
        x1 = ["#"] * 10
        x2 = ["#"] * 10
        x3 = ["#"] * 10
        x4 = ["#"] * 10
        x5 = ["#"] * 10
        x6 = ["#"] * 10
        x7 = ["#"] * 10
        x8 = ["#"] * 10
        x9 = ["#"] * 10
        x10 = ["#"] * 10

        if (char_loc >= 11  and char_loc <= 105):
            if (char_loc >= 11 and char_loc <= 20) or (ex.loc >= 11 and ex.loc <= 20):
                i = b = 0; k = a = 11
                for loop0 in range(0, 10):          
                    if char_loc == k:
                        x1.insert(i, '@')
                    if ex.loc == a and char_loc != a:
                        x1.insert(b, '+')

                    i += 1; b += 1
                    k += 1; a += 1 
            elif (char_loc >= 21 and char_loc <= 30) or (ex.loc >= 21 and ex.loc <= 30):
                i = b = 0; k = a = 21
                for loop1 in range(0, 10):
                    if char_loc == k:
                        x2.insert(i, '@')
                    if ex.loc == a and char_loc != a:
                        x2.insert(b, '+')

                    i += 1; b += 1
                    k += 1; a += 1
            elif (char_loc >= 31 and char_loc <= 40) or (ex.loc >= 31 and ex.loc <= 40):
                i = b = 0; k = a = 31
                for loop2 in range(0, 10):
                    if char_loc == k:
                        x3.insert(i, '@')
                    if ex.loc == a and char_loc != a:
                        x3.insert(b, '+')

                    i += 1; b += 1
                    k += 1; a += 1                                
            elif (char_loc >= 41 and char_loc <= 50) or (ex.loc >= 41 and ex.loc <= 50):
                i = b = 0; k = a = 41
                for loop3 in range(0, 10):
                    if char_loc == k:
                        x4.insert(i, '@')
                    if ex.loc == a and char_loc != a:
                        x4.insert(b, '+')

                    i += 1; b += 1
                    k += 1; a += 1
            elif (char_loc >= 51 and char_loc <= 60) or (ex.loc >= 51 and ex.loc <= 60):
                i = b = 0; k = a = 51
                for loop4 in range(0, 10):
                    if char_loc == k:
                        x5.insert(i, '@')
                    if ex.loc == a and char_loc != a:
                        x5.insert(b, '+')

                    i += 1; b += 1
                    k += 1; a += 1
            elif (char_loc >= 61 and char_loc <= 70) or (ex.loc >= 61 and ex.loc <= 70):
                i = b = 0; k = a = 61
                for loop5 in range(0, 10):          
                    if char_loc == k:
                        x6.insert(i, '@')
                    if ex.loc == a and char_loc != a:
                        x6.insert(b, '+')

                    i += 1; b += 1
                    k += 1; a += 1 
            elif (char_loc >= 71 and char_loc <= 80) or (ex.loc >= 71 and ex.loc <= 80):
                i = b = 0; k = a = 71
                for loop6 in range(0, 10):
                    if char_loc == k:
                        x7.insert(i, '@')
                    if ex.loc == a and char_loc != a:
                        x7.insert(b, '+')

                    i += 1; b += 1
                    k += 1; a += 1
            elif (char_loc >= 81 and char_loc <= 90) or (ex.loc >= 81 and ex.loc <= 90):
                i = b = 0; k = a = 81
                for loop7 in range(0, 10):
                    if char_loc == k:
                        x8.insert(i, '@')
                    if ex.loc == a and char_loc != a:
                        x8.insert(b, '+')

                    i += 1; b += 1
                    k += 1; a += 1                        
            elif (char_loc >= 91 and char_loc <= 100) or (ex.loc >= 91 and ex.loc <= 100):
                i = b = 0; k = a = 91
                for loop8 in range(0, 10):
                    if char_loc == k:
                        x9.insert(i, '@')
                    if ex.loc == a and char_loc != a:
                        x9.insert(b, '+')

                    i += 1; b += 1
                    k += 1; a += 1
            elif (char_loc >= 101 and char_loc <= 110) or (ex.loc >= 101 and ex.loc <= 110):
                i = b = 0; k = a = 101
                for loop9 in range(0, 10):
                    if char_loc == k:
                        x10.insert(i, '@')
                    if ex.loc == a and char_loc != a:
                        x10.insert(b, '+')

                    i += 1; b += 1
                    k += 1; a += 1
            else:
                print("fail")

        print( x1[9],x2[9],x3[9],x4[9],x5[9],x6[9],x7[9],x8[9],x9[9],x10[9])
        print( x1[8],x2[8],x3[8],x4[8],x5[8],x6[8],x7[8],x8[8],x9[8],x10[8])
        print( x1[7],x2[7],x3[7],x4[7],x5[7],x6[7],x7[7],x8[7],x9[7],x10[7])
        print( x1[6],x2[6],x3[6],x4[6],x5[6],x6[6],x7[6],x8[6],x9[6],x10[6])
        print( x1[5],x2[5],x3[5],x4[5],x5[5],x6[5],x7[5],x8[5],x9[5],x10[5])                
        print( x1[4],x2[4],x3[4],x4[4],x5[4],x6[4],x7[4],x8[4],x9[4],x10[4])
        print( x1[3],x2[3],x3[3],x4[3],x5[3],x6[3],x7[3],x8[3],x9[3],x10[3])
        print( x1[2],x2[2],x3[2],x4[2],x5[2],x6[2],x7[2],x8[2],x9[2],x10[2])
        print( x1[1],x2[1],x3[1],x4[1],x5[1],x6[1],x7[1],x8[1],x9[1],x10[1])
        print( x1[0],x2[0],x3[0],x4[0],x5[0],x6[0],x7[0],x8[0],x9[0],x10[0])
        print(char_loc, ex.loc)

        if char_loc == ex.loc:
            win()

        move = input()

        if move == "w" and (char_loc != 20 and char_loc != 30 and char_loc != 40 and char_loc != 50 and char_loc != 60 and char_loc != 70 and char_loc != 80 and char_loc != 90 and char_loc != 100 and char_loc != 110):
            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 and char_loc != 61 and char_loc != 71 and char_loc != 81 and char_loc != 91 and char_loc != 101):
            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 and char_loc != 16 and char_loc != 17 and char_loc != 18 and char_loc != 19 and char_loc != 20):
            char_loc -= 10
            print("left")
        elif move == "d" and (char_loc != 101 and char_loc != 102 and char_loc != 103 and char_loc != 104 and char_loc != 105 and char_loc != 106 and char_loc != 107 and char_loc != 108 and char_loc != 109 and char_loc != 110):
            char_loc += 10
            print("right")
        else: print("You can't move there!")

if __name__ == '__main__': main()

此代码存在的问题是,如果它们位于同一列中,它只会同时显示“@”和“+”

# # # # # # # # # #
# # # # # # # # # #
# # # # # # # + # #
# # # # # # # # # #
# # # # # # # # # #
# # # # # # # # # #
# # # # # # # # # #
# # # # # # # @ # #
# # # # # # # # # #
# # # # # # # # # #
83 88

如果它们不在同一列中,它将只显示最左边的那个

# # # # # # # # # #
# # # # # # # # # #
# # # # # # # + # #
# # # # # # # # # #
# # # # # # # # # #
# # # # # # # # # #
# # # # # # # # # #
# # # # # # # # # #
# # # # # # # # # #
# # # # # # # # # #
93 88

# # # # # # # # # #
# # # # # # # # # #
# # # # # # # # # #
# # # # # # # # # #
# # # # # # # # # #
# # # # # # # # # #
# # # # # # # # # #
# # # # # # @ # # #
# # # # # # # # # #
# # # # # # # # # #
73 88

游戏应始终显示“@”和“+”,目前正在做什么。任何帮助,提示,建议都非常感谢。 提前谢谢。

1 个答案:

答案 0 :(得分:2)

没有进入编码风格,所有 elifs 。你击中的第一个,你进入并忽略其他人。因此,当您第一次找到包含符号的列时,您将计算该列并将值放入其中。然后忽略以下列。如果您将它们全部更改为常规,如果,您的代码看起来应该可以正常工作。