0

I'm making a simple text based game in python, which works fine until I activate "debug" mode (in game, with /debug), which is supposed to change this:

# # # # # # # # # # # #
# . . . . . . . . . . #
# . . . . . . . . . . #
# . . . . . . . . . . #
# . . . . . . . . . . #
# . . . . . . . . . . #
# . . . . . . . . . . #
# . . + . . . . . . . #
# . . . . . . . . . . #
# . . . . . . . . . . #
# @ . . . . . . . . . #
# # # # # # # # # # # #

Into this:

 # # # # # # # # # # # #
10 . . . . . . . . . . #
 9 . . . . . . . . . . #
 8 . . . . . . . . . . #
 7 . . . . . . . . . . #
 6 . . . . . . . . . . #
 5 . . . . . . . . . . #
 4 . . + . . . . . . . #
 3 . . . . . . . . . . #
 2 . . . . . . . . . . #
 1 @ . . . . . . . . . #
 # 1 2 3 4 5 6 7 8 9 0 #

But instead gives this error:

Traceback (most recent call last):
  File "Location\On\HDD\Game.py", line 336, in <module>
    if __name__ == '__main__': main()
  File "Location\On\HDD\Game.py", line 277, in main
    walls.btm = ['1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9','20']
UnboundLocalError: local variable 'walls' referenced before assignment

Which is odd because "walls" is a class not a variable!
How would I fix this? And what is wrong? All help is greatly apreciated.

Here is the code for the game:

#!/usr/bin/env python

import random
import time
import os

class vars:
    running = 1
    debug = 0 #Debug mode on/off  ~ default = off
    message = ("")
    n = 1

class ex:
    placed = 0
    y = 0
    x = 0
    last_x = 1
    last_y = 1

class char:
    x = 1
    y = 1

class map:
    size = 1
    max_x = 10
    max_y = 10
    x1 = ["."] * 20
    x2 = ["."] * 20
    x3 = ["."] * 20
    x4 = ["."] * 20
    x5 = ["."] * 20
    x6 = ["."] * 20
    x7 = ["."] * 20
    x8 = ["."] * 20
    x9 = ["."] * 20
    x10 = ["."] * 20
    x11 = ["."] * 20
    x12 = ["."] * 20
    x13 = ["."] * 20
    x14 = ["."] * 20
    x15 = ["."] * 20
    x16 = ["."] * 20
    x17 = ["."] * 20
    x18 = ["."] * 20
    x19 = ["."] * 20
    x20 = ["."] * 20
    x21 = ["."] * 20
    x22 = ["."] * 20
    x23 = ["."] * 20
    x24 = ["."] * 20
    x25 = ["."] * 20
    x26 = ["."] * 20
    x27 = ["."] * 20
    x28 = ["."] * 20
    x29 = ["."] * 20
    x30 = ["."] * 20

    def reset ():
        map.x1 = ["."] * 20
        map.x2 = ["."] * 20
        map.x3 = ["."] * 20
        map.x4 = ["."] * 20
        map.x5 = ["."] * 20
        map.x6 = ["."] * 20
        map.x7 = ["."] * 20
        map.x8 = ["."] * 20
        map.x9 = ["."] * 20
        map.x10 = ["."] * 20
        map.x11 = ["."] * 20
        map.x12 = ["."] * 20
        map.x13 = ["."] * 20
        map.x14 = ["."] * 20
        map.x15 = ["."] * 20
        map.x16 = ["."] * 20
        map.x17 = ["."] * 20
        map.x18 = ["."] * 20
        map.x19 = ["."] * 20
        map.x20 = ["."] * 20
        map.x21 = ["."] * 20
        map.x22 = ["."] * 20
        map.x23 = ["."] * 20
        map.x24 = ["."] * 20
        map.x25 = ["."] * 20
        map.x26 = ["."] * 20
        map.x27 = ["."] * 20
        map.x28 = ["."] * 20
        map.x29 = ["."] * 20
        map.x30 = ["."] * 20


def set_char_loc ():
    if (1 <= char.x <= map.max_x and 1 <= char.y <= map.max_y):
        if (char.x == ex.x and char.y > ex.y):
            vars.n = 2
        else:
            vars.n = 1
        if (char.x == 1):
            map.x1.insert((char.y - vars.n), '@') 
        elif (char.x == 2):
            map.x2.insert((char.y - vars.n), '@')
        elif (char.x == 3):
            map.x3.insert((char.y - vars.n), '@')
        elif (char.x == 4):
            map.x4.insert((char.y - vars.n), '@')
        elif (char.x == 5):
            map.x5.insert((char.y - vars.n), '@')
        elif (char.x == 6):
            map.x6.insert((char.y - vars.n), '@')
        elif (char.x == 7):
            map.x7.insert((char.y - vars.n), '@')
        elif (char.x == 8):
            map.x8.insert((char.y - vars.n), '@')
        elif (char.x == 9):
            map.x9.insert((char.y - vars.n), '@')
        elif (char.x == 10):
            map.x10.insert((char.y - vars.n), '@')
        elif (char.x == 11):
            map.x11.insert((char.y - vars.n), '@') 
        elif (char.x == 12):
            map.x12.insert((char.y - vars.n), '@')
        elif (char.x == 13):
            map.x13.insert((char.y - vars.n), '@')
        elif (char.x == 14):
            map.x14.insert((char.y - vars.n), '@')
        elif (char.x == 15):
            map.x15.insert((char.y - vars.n), '@')
        elif (char.x == 16):
            map.x16.insert((char.y - vars.n), '@')
        elif (char.x == 17):
            map.x17.insert((char.y - vars.n), '@')
        elif (char.x == 18):
            map.x18.insert((char.y - vars.n), '@')
        elif (char.x == 19):
            map.x19.insert((char.y - vars.n), '@')
        elif (char.x == 20):
            map.x20.insert((char.y - vars.n), '@')

def set_ex_loc ():
    if not (ex.x == char.x and ex.y == char.y):       
        if (ex.x == 1):
            map.x1.insert((ex.y  - 1), '+')
        elif (ex.x == 2 ):
            map.x2.insert((ex.y  - 1), '+')
        elif (ex.x == 3 ):
            map.x3.insert((ex.y  - 1), '+')
        elif (ex.x == 4 ):
            map.x4.insert((ex.y  - 1), '+')
        elif (ex.x == 5 ):
            map.x5.insert((ex.y  - 1), '+')
        elif (ex.x == 6 ):
            map.x6.insert((ex.y  - 1), '+')
        elif (ex.x == 7 ):
            map.x7.insert((ex.y  - 1), '+')
        elif (ex.x == 8 ):
            map.x8.insert((ex.y  - 1), '+')
        elif (ex.x == 9 ):
            map.x9.insert((ex.y  - 1), '+')
        elif (ex.x == 10 ):
            map.x10.insert((ex.y  - 1), '+')
        elif (ex.x == 11):
            map.x11.insert((ex.y  - 1), '+')
        elif (ex.x == 12 ):
            map.x12.insert((ex.y  - 1), '+')
        elif (ex.x == 13 ):
            map.x13.insert((ex.y  - 1), '+')
        elif (ex.x == 14 ):
            map.x14.insert((ex.y  - 1), '+')
        elif (ex.x == 15 ):
            map.x15.insert((ex.y  - 1), '+')
        elif (ex.x == 16 ):
            map.x16.insert((ex.y  - 1), '+')
        elif (ex.x == 17 ):
            map.x17.insert((ex.y  - 1), '+')
        elif (ex.x == 18 ):
            map.x18.insert((ex.y  - 1), '+')
        elif (ex.x == 19 ):
            map.x19.insert((ex.y  - 1), '+')
        elif (ex.x == 20 ):
            map.x20.insert((ex.y  - 1), '+')

class walls:
    top = ["#"] * 30
    right = ["#"] * 30
    corner = ["#"] * 8
    btm = ["#"] * 30
    left = ["#"] * 30
    def reset ():
        walls.top = ["#"] * 30
        walls.right = ["#"] * 30
        walls.corner = ["#"] * 8
        walls.btm = ["#"] * 30
        walls.left = ["#"] * 30

def draw_map (size):
    if size == 1:
        map.max_x = 10; map.max_y = 10
        print( walls.corner[0],walls.top[0],walls.top[1],walls.top[2],walls.top[3],walls.top[4],walls.top[5],walls.top[6],walls.top[7],walls.top[8],walls.top[9],walls.corner[1] )
        print( walls.left[9],map.x1[9],map.x2[9],map.x3[9],map.x4[9],map.x5[9],map.x6[9],map.x7[9],map.x8[9],map.x9[9],map.x10[9],walls.right[0])
        print( walls.left[8],map.x1[8],map.x2[8],map.x3[8],map.x4[8],map.x5[8],map.x6[8],map.x7[8],map.x8[8],map.x9[8],map.x10[8],walls.right[1])
        print( walls.left[7],map.x1[7],map.x2[7],map.x3[7],map.x4[7],map.x5[7],map.x6[7],map.x7[7],map.x8[7],map.x9[7],map.x10[7],walls.right[2])
        print( walls.left[6],map.x1[6],map.x2[6],map.x3[6],map.x4[6],map.x5[6],map.x6[6],map.x7[6],map.x8[6],map.x9[6],map.x10[6],walls.right[3])
        print( walls.left[5],map.x1[5],map.x2[5],map.x3[5],map.x4[5],map.x5[5],map.x6[5],map.x7[5],map.x8[5],map.x9[5],map.x10[5],walls.right[4])
        print( walls.left[4],map.x1[4],map.x2[4],map.x3[4],map.x4[4],map.x5[4],map.x6[4],map.x7[4],map.x8[4],map.x9[4],map.x10[4],walls.right[5])
        print( walls.left[3],map.x1[3],map.x2[3],map.x3[3],map.x4[3],map.x5[3],map.x6[3],map.x7[3],map.x8[3],map.x9[3],map.x10[3],walls.right[6])
        print( walls.left[2],map.x1[2],map.x2[2],map.x3[2],map.x4[2],map.x5[2],map.x6[2],map.x7[2],map.x8[2],map.x9[2],map.x10[2],walls.right[7])
        print( walls.left[1],map.x1[1],map.x2[1],map.x3[1],map.x4[1],map.x5[1],map.x6[1],map.x7[1],map.x8[1],map.x9[1],map.x10[1],walls.right[8])
        print( walls.left[0],map.x1[0],map.x2[0],map.x3[0],map.x4[0],map.x5[0],map.x6[0],map.x7[0],map.x8[0],map.x9[0],map.x10[0],walls.right[9])
        print( walls.corner[0],walls.btm[0],walls.btm[1],walls.btm[2],walls.btm[3],walls.btm[4],walls.btm[5],walls.btm[6],walls.btm[7],walls.btm[8],walls.btm[9],walls.corner[1])

    if size == 2:
        map.max_x = 20; map.max_y = 10
        print( walls.corner[0],walls.top[0],walls.top[1],walls.top[2],walls.top[3],walls.top[4],walls.top[5],walls.top[6],walls.top[7],walls.top[8],walls.top[9],walls.top[10],walls.top[11],walls.top[12],walls.top[13],walls.top[14],walls.top[15],walls.top[16],walls.top[17],walls.top[18],walls.top[19],walls.corner[1])
        print( walls.left[9],map.x1[9],map.x2[9],map.x3[9],map.x4[9],map.x5[9],map.x6[9],map.x7[9],map.x8[9],map.x9[9],map.x10[9],map.x11[9],map.x12[9],map.x13[9],map.x14[9],map.x15[9],map.x16[9],map.x17[9],map.x18[9],map.x19[9],map.x20[9],walls.right[0])
        print( walls.left[8],map.x1[8],map.x2[8],map.x3[8],map.x4[8],map.x5[8],map.x6[8],map.x7[8],map.x8[8],map.x9[8],map.x10[8],map.x11[8],map.x12[8],map.x13[8],map.x14[8],map.x15[8],map.x16[8],map.x17[8],map.x18[8],map.x19[8],map.x20[8],walls.right[0])
        print( walls.left[7],map.x1[7],map.x2[7],map.x3[7],map.x4[7],map.x5[7],map.x6[7],map.x7[7],map.x8[7],map.x9[7],map.x10[7],map.x11[7],map.x12[7],map.x13[7],map.x14[7],map.x15[7],map.x16[7],map.x17[7],map.x18[7],map.x19[7],map.x20[7],walls.right[0])
        print( walls.left[6],map.x1[6],map.x2[6],map.x3[6],map.x4[6],map.x5[6],map.x6[6],map.x7[6],map.x8[6],map.x9[6],map.x10[6],map.x11[6],map.x12[6],map.x13[6],map.x14[6],map.x15[6],map.x16[6],map.x17[6],map.x18[6],map.x19[6],map.x20[6],walls.right[0])
        print( walls.left[5],map.x1[5],map.x2[5],map.x3[5],map.x4[5],map.x5[5],map.x6[5],map.x7[5],map.x8[5],map.x9[5],map.x10[5],map.x11[5],map.x12[5],map.x13[5],map.x14[5],map.x15[5],map.x16[5],map.x17[5],map.x18[5],map.x19[5],map.x20[5],walls.right[0])
        print( walls.left[4],map.x1[4],map.x2[4],map.x3[4],map.x4[4],map.x5[4],map.x6[4],map.x7[4],map.x8[4],map.x9[4],map.x10[4],map.x11[4],map.x12[4],map.x13[4],map.x14[4],map.x15[4],map.x16[4],map.x17[4],map.x18[4],map.x19[4],map.x20[4],walls.right[0])
        print( walls.left[3],map.x1[3],map.x2[3],map.x3[3],map.x4[3],map.x5[3],map.x6[3],map.x7[3],map.x8[3],map.x9[3],map.x10[3],map.x11[3],map.x12[3],map.x13[3],map.x14[3],map.x15[3],map.x16[3],map.x17[3],map.x18[3],map.x19[3],map.x20[3],walls.right[0])
        print( walls.left[2],map.x1[2],map.x2[2],map.x3[2],map.x4[2],map.x5[2],map.x6[2],map.x7[2],map.x8[2],map.x9[2],map.x10[2],map.x11[2],map.x12[2],map.x13[2],map.x14[2],map.x15[2],map.x16[2],map.x17[2],map.x18[2],map.x19[2],map.x20[2],walls.right[0])
        print( walls.left[1],map.x1[1],map.x2[1],map.x3[1],map.x4[1],map.x5[1],map.x6[1],map.x7[1],map.x8[1],map.x9[1],map.x10[1],map.x11[1],map.x12[1],map.x13[1],map.x14[1],map.x15[1],map.x16[1],map.x17[1],map.x18[1],map.x19[1],map.x20[1],walls.right[0])
        print( walls.left[0],map.x1[0],map.x2[0],map.x3[0],map.x4[0],map.x5[0],map.x6[0],map.x7[0],map.x8[0],map.x9[0],map.x10[0],map.x11[0],map.x12[0],map.x13[0],map.x14[0],map.x15[0],map.x16[0],map.x17[0],map.x18[0],map.x19[0],map.x20[0],walls.right[0])
        print( walls.corner[0],walls.btm[0],walls.btm[1],walls.btm[2],walls.btm[3],walls.btm[4],walls.btm[5],walls.btm[6],walls.btm[7],walls.btm[8],walls.btm[9],walls.btm[10],walls.btm[11],walls.btm[12],walls.btm[13],walls.btm[14],walls.btm[15],walls.btm[16],walls.btm[17],walls.btm[18],walls.btm[19],walls.corner[4])

    if size == 3:
        map.max_x = 10; map.max_y = 20
        print( walls.corner[0],walls.top[0],walls.top[1],walls.top[2],walls.top[3],walls.top[4],walls.top[5],walls.top[6],walls.top[7],walls.top[8],walls.top[9],walls.corner[1] )
        print( walls.left[19],map.x1[19],map.x2[19],map.x3[19],map.x4[19],map.x5[19],map.x6[19],map.x7[19],map.x8[19],map.x9[19],map.x10[19],walls.right[19])
        print( walls.left[18],map.x1[18],map.x2[18],map.x3[18],map.x4[18],map.x5[18],map.x6[18],map.x7[18],map.x8[18],map.x9[18],map.x10[18],walls.right[18])
        print( walls.left[17],map.x1[17],map.x2[17],map.x3[17],map.x4[17],map.x5[17],map.x6[17],map.x7[17],map.x8[17],map.x9[17],map.x10[17],walls.right[17])
        print( walls.left[16],map.x1[16],map.x2[16],map.x3[16],map.x4[16],map.x5[16],map.x6[16],map.x7[16],map.x8[16],map.x9[16],map.x10[16],walls.right[16])
        print( walls.left[15],map.x1[15],map.x2[15],map.x3[15],map.x4[15],map.x5[15],map.x6[15],map.x7[15],map.x8[15],map.x9[15],map.x10[15],walls.right[15])
        print( walls.left[14],map.x1[14],map.x2[14],map.x3[14],map.x4[14],map.x5[14],map.x6[14],map.x7[14],map.x8[14],map.x9[14],map.x10[14],walls.right[14])
        print( walls.left[13],map.x1[13],map.x2[13],map.x3[13],map.x4[13],map.x5[13],map.x6[13],map.x7[13],map.x8[13],map.x9[13],map.x10[13],walls.right[13])
        print( walls.left[12],map.x1[12],map.x2[12],map.x3[12],map.x4[12],map.x5[12],map.x6[12],map.x7[12],map.x8[12],map.x9[12],map.x10[12],walls.right[12])
        print( walls.left[11],map.x1[11],map.x2[11],map.x3[11],map.x4[11],map.x5[11],map.x6[11],map.x7[11],map.x8[11],map.x9[11],map.x10[11],walls.right[11])
        print( walls.left[10],map.x1[10],map.x2[10],map.x3[10],map.x4[10],map.x5[10],map.x6[10],map.x7[10],map.x8[10],map.x9[10],map.x10[10],walls.right[10])
        print( walls.left[9],map.x1[9],map.x2[9],map.x3[9],map.x4[9],map.x5[9],map.x6[9],map.x7[9],map.x8[9],map.x9[9],map.x10[9],walls.right[9])
        print( walls.left[8],map.x1[8],map.x2[8],map.x3[8],map.x4[8],map.x5[8],map.x6[8],map.x7[8],map.x8[8],map.x9[8],map.x10[8],walls.right[8])
        print( walls.left[7],map.x1[7],map.x2[7],map.x3[7],map.x4[7],map.x5[7],map.x6[7],map.x7[7],map.x8[7],map.x9[7],map.x10[7],walls.right[7])
        print( walls.left[6],map.x1[6],map.x2[6],map.x3[6],map.x4[6],map.x5[6],map.x6[6],map.x7[6],map.x8[6],map.x9[6],map.x10[6],walls.right[6])
        print( walls.left[5],map.x1[5],map.x2[5],map.x3[5],map.x4[5],map.x5[5],map.x6[5],map.x7[5],map.x8[5],map.x9[5],map.x10[5],walls.right[5])
        print( walls.left[4],map.x1[4],map.x2[4],map.x3[4],map.x4[4],map.x5[4],map.x6[4],map.x7[4],map.x8[4],map.x9[4],map.x10[4],walls.right[4])
        print( walls.left[3],map.x1[3],map.x2[3],map.x3[3],map.x4[3],map.x5[3],map.x6[3],map.x7[3],map.x8[3],map.x9[3],map.x10[3],walls.right[3])
        print( walls.left[2],map.x1[2],map.x2[2],map.x3[2],map.x4[2],map.x5[2],map.x6[2],map.x7[2],map.x8[2],map.x9[2],map.x10[2],walls.right[2])
        print( walls.left[1],map.x1[1],map.x2[1],map.x3[1],map.x4[1],map.x5[1],map.x6[1],map.x7[1],map.x8[1],map.x9[1],map.x10[1],walls.right[1])
        print( walls.left[0],map.x1[0],map.x2[0],map.x3[0],map.x4[0],map.x5[0],map.x6[0],map.x7[0],map.x8[0],map.x9[0],map.x10[0],walls.right[0])
        print( walls.corner[0],walls.btm[0],walls.btm[1],walls.btm[2],walls.btm[3],walls.btm[4],walls.btm[5],walls.btm[6],walls.btm[7],walls.btm[8],walls.btm[9],walls.corner[1])



def main ():

    message = ("")

    while vars.running == 1:

        if char.x == ex.x and char.y == ex.y:
            message = (" You escaped!")
            ex.placed = 0

        while ex.placed == 0:
            ex.y = random.randint(1, map.max_y)
            ex.x = random.randint(1, map.max_x)
            char.x = ex.last_x
            char.y = ex.last_y
            ex.last_y = ex.y
            ex.last_x = ex.x
            ex.placed = 1

        os.system('CLS')
        map.reset()
        set_char_loc()
        set_ex_loc()
        if vars.debug == 1:
            walls.btm = ['1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9','20']
            walls.left = [' 1',' 2',' 3',' 4',' 5',' 6',' 7',' 8',' 9','10','11','12','13','14','15','16','17','18','19','20']
            walls.corner[0] = (' #');walls,corner[2] = (' #')

        draw_map(map.size)
        print(message);message = ("")
        if vars.debug == 1:
            print(char.x,':', char.y,',',ex.x,':',ex.y,'    mapsize:', map.size)

        move = input().lower()
        if move == "quit":
            vars.running = 0
        elif move == "help":
            message = (" 'w' and 's' to move up and down\n 'a' and 'd' to move left and right\n 'quit' to quit")
        elif move.startswith('/'):
            if "debug" in move:
                if vars.debug == 0:
                    vars.debug = 1
                elif vars.debug == 1:
                    vars.debug = 0
                    walls.reset()
            elif "char.x" in move:
                for c in range(1, 10):
                    c2 = ("%d" % (c))
                    repr(c2)
                    if c2 in move:
                        char.x = c
            elif "char.y" in move:
                for b in range(1, 10):
                    b2 = ("%d" % (b))
                    repr(b2)
                    if b2 in move:
                        char.y = b
            elif "mapsize" in move:
                for m in range(1, 5):
                    m2 = ("%d" % (m))
                    repr(m2)
                    if m2 in move:
                        map.size = m
            else: 
                message = (" Unrecognised command")
        elif move == "w" or move == "s" or move == "a" or move == "d":
            if move == "w" and (char.y != map.max_y):
                char.y += 1
                print("up")
            elif move == "s" and (char.y != 1):
                char.y -= 1
                print("down")
            elif move == "a" and (char.x != 1):
                char.x -= 1
                print("left")
            elif move == "d" and (char.x != map.max_x):
                char.x += 1
                print("right")
            else:
                message = ("You can't move there!")
        else: 
            message = (" Incorrect input\n Type 'help' for help")

if __name__ == '__main__': main()
4
Contributors
4
Replies
26
Views
3 Years
Discussion Span
Last Post by pyTony
Featured Replies
  • 1

    walls,corner[2] = (' #') You accidentally typed a comma instead of a period here. As a result the parser things you want to assign to a local variable called `walls` as well as to the second element of a variable named `corner`. Because of this Python now thinks that `walls` … Read More

  • 1

    Moral: "Don't assume if a program does not work, that it is fault of the computer or the language." Read More

  • 1

    ... or do not assume that the that program seems to work does not have bugs... (I would not know, as this program is quite unverifyable for me because of repeating code instead of loops) Read More

1
walls,corner[2] = (' #')

You accidentally typed a comma instead of a period here.

As a result the parser things you want to assign to a local variable called walls as well as to the second element of a variable named corner. Because of this Python now thinks that walls is a local variable and thus produces an error when you try to access it before its value has been set.

1

Moral:
"Don't assume if a program does not work, that it is fault of the computer or the language."

1

... or do not assume that the that program seems to work does not have bugs...
(I would not know, as this program is quite unverifyable for me because of repeating code instead of loops)

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.