0

I'm trying to create a snake game where you can choose which character you want to be. I have the default character in my game set to be goomba. There is a "Characters" screen and from there, I want to be able to choose which character I want to be with buttons. I have tried to do

elif currentchar == "mario":
            charU = pygame.image.load('mario up')
            charD = pygame.image.load('mario down')
            charL = pygame.image.load('mario left')
            charR = pygame.image.load('mario right')
            gameLoop()

but it comes up with "NameError: name 'currentchar' is not defined". I don't know what to do if I define it. The full code can be found here: https://pastebin.com/7gtvdf8E Any help is appreciated. Thank you in advance.

EDIT: oops I was tinkering a bit before pasting and forgot to change it. Here is the real pastebin https://pastebin.com/sYNHqPyD EDIT: When i press "mario", nothing happens

  • Line 194, I believe you meant `currentchar = "mario"`, with a single `=` and not `==`. Also, the `elif` *after* that doesn't make sense. Are you sure the if block is correctly placed? – shad0w_wa1k3r Apr 23 '17 at 10:49
  • Yeah i meant to use one "=". It still doesnt work though. I don't know what to do. – PikachuPopcorn Apr 23 '17 at 11:05
  • In that case, your error will / should be different. If not, you should paste the full error traceback. – shad0w_wa1k3r Apr 23 '17 at 11:08
  • When I press Mario, nothing happens – PikachuPopcorn Apr 23 '17 at 11:10
  • Is there something unclear in my answer? If you work on your code and made the variables global you should in between see the change in what becomes displayed after you press Mario ... – Claudio Apr 23 '17 at 12:21

1 Answers1

0

To make the new character pictures visible the function displaying them must know about the change happened in another function. That is where global variables comes in. If you make your variables global you will see the change and the code will work as you want.

Check out what the command global means and does here:

Using global variables in a function other than the one that created them

Than put into in each function in which you have charU, charD, ... variables directly in the first line after def functionName(): :

global charU, charD, charL, charR
Community
  • 1
  • 1
Claudio
  • 7,474
  • 3
  • 18
  • 48