Frage

#!/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()

The problem with this code is that it will only display the '@' and '+' at the same time, if they are in the same column

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

and it will only display the left most one if they are not in the same column

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

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

The game is supposed to display the '@' and '+' at all times, not what it is doing at the moment. Any help, tips, suggestions are greatly appreciated. Thanks in advance.

War es hilfreich?

Lösung

Without getting into coding style, it's all the elifs. The first one you hit, you go into and ignore the others. So the first time you find a column that contains a symbol you compute that column and put the value in it. Then the following columns you ignore. If you change them all to regular if's your code looks like it should work.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top