0

I've been writing a game for a hobby/submitting it as a final project, cool stuff. But I've recently run into a mind-boggling issue that I have no idea how or why it happens.

This method is to draw a box for the game, but whenever it runs the console output seems to add more and more characters to the script. I tried to debug it, but so far it turns out I'm not a very good debugger.

def drawModule(selections, selected):
'''Draws a text box based on the inputs given'''
str1 = ""
str2 = ""
str3 = "  "
str4 = ""
str5 = ""
modList = selections
print id(modList)
print id(selections)
for i in range(len(modList)):
    if i == selected:
        modList[i] = "| " + modList[i] + " |  "
    else:
        modList[i] = "# " + modList[i] + " #  "
    str3 += modList[i]
print selections
print modList
for i in range(len(modList)):
    str1 += "  "
    str2 += "  "
    str4 += "  "
    str5 += "  "
    for letter in range(len(modList[i]) - 2):
        if letter == 0 or letter == (len(modList[i]) - 3):
            if i == selected:
                str1 += "|"
                str2 += "|"
                str4 += "|"
                str5 += "|"
            else:
                str1 += "#"
                str2 += "#"
                str4 += "#"
                str5 += "#"
        else:
            if i == selected:
                str1 += "-"
                str2 += " "
                str4 += " "
                str5 += "-"
            else:
                str1 += "#"
                str2 += " "
                str4 += " "
                str5 += "#"
sys.stdout.write(str1 + "\n" + str2 + "\n" + str3 + "\n" + str4 + "\n" + str5)

The script calling this method is for the beginnings of the combat system in my game. It's a rogue-like isaac-like whateverthehell made out of ASCII text, not that it matters.

UNFINISHED
def combat():
    '''Runs combat seperately from the selection menu'''
    inCombat = True
    print "You've started combat!"
    time.sleep(1)
    options = ["attack", "guard", "inventory", "flee"]
    position = 0
    clear()
    while inCombat:
        if msvcrt.kbhit():
            clear()
            key = msvcrt.getch()
            print options
            if ord(key) == 100:
                position +=1
                drawModule(options, position)
            elif ord(key) == 97:
                position -= 1
                drawModule(options, position)
            elif ord(key) == 27:
                break

I'm still losing my mind over this, any solution would be amazing. Also, I'm not great at this, so if you find something I can do better, I'd love to know more!

Console Output when run multiple times:

138650760
138650760
['# attack #  ', '| guard |  ', '# items #  ', '# flee #  ']
['# attack #  ', '| guard |  ', '# items #  ', '# flee #  ']
  ##########  |-------|  #########  ########
  #        #  |       |  #       #  #      #
  # attack #  | guard |  # items #  # flee #  
  #        #  |       |  #       #  #      #
  ##########  |-------|  #########  ########
-------------------------Second Time----------------------
138650760
138650760
['# # attack #   #  ', '| | guard |   |  ', '# # items #   #  ', '# # flee #   #  ']
['# # attack #   #  ', '| | guard |   |  ', '# # items #   #  ', '# # flee #   #  ']
  ################  |-------------|  ###############  ##############
  #              #  |             |  #             #  #            #
  # # attack #   #  | | guard |   |  # # items #   #  # # flee #   #  
  #              #  |             |  #             #  #            #
  ################  |-------------|  ###############  ##############

This trend continues some insubordinate amount of time.

  • 1
    Possible duplicate of [How to make a copy of a list of objects not change when modifying the original list?](http://stackoverflow.com/questions/6612775/how-to-make-a-copy-of-a-list-of-objects-not-change-when-modifying-the-original-l) – SiHa Jan 09 '17 at 19:46

2 Answers2

0

At the line:

modList = selections

You have equated two lists. So later:

    modList[i] = "| " + modList[i] + " |  "

is actually modifying selections and modList.

Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
0

It was the fact that I was equating the lists to each other earlier in the code that caused the issue. I used the deepcopy function from the copy module to solve my problem.

  • Please have a look at [Can I answer my own question?](http://stackoverflow.com/help/self-answer) and come back two days later and check as answered. – help-info.de May 03 '17 at 12:20