dimanche 29 avril 2018

Issues with re-assigning text variables in IF statements? (Python)

My newest Python program is supposed to detect for a key press of WASD, and then move a five character sprite (-\o/-) over one in a large ASCII art board. Just for clarification, here are some code snippets (and then the full code at the end).

This is the model of the board as of right now. I will implement a new way of showing the board (right now it just prints it over and over, but I do want to have it clear every time it has to be loaded):

https://pastebin.com/z1fGLMYd (sorry, but formatting couldn't work out on here).

Then, pygame is initialized to connect to the keyboard:

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
        pygame.quit(); #sys.exit() if sys is imported
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_s:
                doSkey()
                print(m)
            if event.key == pygame.K_w:
                doWkey()
                print(m)
            if event.key == pygame.K_d:
                doDkey()
                print(m)
            if event.key == pygame.K_a:
                doAkey()
                print(m)

Finally, we use if statements to connect the keyboard to the "v-board" (variable board, i.e everything stored in variable m):

def doAkey():
    if ab == "-\o/-":
        ab = "     "
        aa = "-\o/-"
    else:
        print(m)

def doDkey():
    if aa == "-\o/-":
        aa = "     "
        ab = "-\o/-"
    else:
        print(m)

What happens at this point is the program launches, and everything loads fine. But when the 'd' or 'a' keys are pressed, it just re-prints the board again, without making any change.

QUESTION:

Why doesn't the variable change in these 'if' statements? I have tried every possible solution I can find on the internet, but nothing works. I have also tried putting the code directly in the loop, but still nothing.

Any help would be greatly appreciated. Thanks!

Here is the full code, for anyone who wants it: https://pastebin.com/ay8MA0cm

Aucun commentaire:

Enregistrer un commentaire