Python Forum
Pygame global problem
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pygame global problem
#1
Greetings, I'm new here as I only now started developing in python due to a college class, and for this one as a end semester group work I need to develop a game, however simple it is. And for that I download a pygame called PyCatch-master and am using it's code as base since it is very close to what I need. But to simplify it's code even further (so that I don't have to risk explaining things that I do not entirely understand, or my colleague who understands far less than I) I've deleted a few lines of code to try and make the game as simple as possible. No menu, no levels, just open and play straight away. And I might even make it simpler without high-scores or lives (pointless game but would work for what is asked.

But enough background, I'm getting a problem like this.
Quote:C:\Users\user\Desktop\PyCatch-master> PyCatch.py
C:\Users\user\Desktop\PyCatch-master\PyCatch.py:263: Syntaxwarning: name ‘lives’ is assigned to before global declaration global lives
C:\Users\user\Desktop\PyCatch-master\PyCatch.py:266: Syntaxwarning: name ‘InGame’ is assigned to before global declaration global InGame
C:\Users\user\Oesktop\PyCatch-master>
To try and fix this I tried changing the line 31 code to global lives = 5 but then this happens
Quote:C:\Users\user\Desktop>cd PyCatch-master
C:\Users\user\Desktop\PyCatch-master> PyCatch.py
File "C:\Useps\usGp\Desktop\PyCatch-mastGp\PyCatch.py"
global lives = 5
>\
SyntaxEppop: invalid syntax C:\UsGPs\usGp\DGsktop\PyCatch-mastGP>
line 31
The files I am working with can be retrieved here.

Many thanks in advance to whoever tries to help me.
Reply
#2
The syntax global {variablename} is used to hint that a variable exists in the global scope, not within a block's scope.  You can't add other things to that, that's the only thing it can do.  So you can't assign a value to it at the same time that you're hinting that it exists somewhere else.

Try this instead:
global lives
lives = 5
Reply
#3
Thanks fore the reply, I tried what you said, sadly however it still gives me the first error about the global lives and ingame. e.e god damn python :c
Reply
#4
What's the error?  Please copy/paste the entire traceback.
Reply
#5
Quote:C:\Users\user\Desktop\PyCatch-master> PyCatch.py
C:\Users\user\Desktop\PyCatch-master\PyCatch.py:263: Syntaxwarning: name ‘lives’ is assigned to before global declaration global lives
C:\Users\user\Desktop\PyCatch-master\PyCatch.py:266: Syntaxwarning: name ‘InGame’ is assigned to before global declaration global InGame
C:\Users\user\Oesktop\PyCatch-master>

This is the error but since I changed the
Quote:lives = 5
to
Quote:global lives
Lives = 5
the lines are 264 and 267 (1 more)
Reply
#6
Whole program please. Also, you shouldn't be using global for non-constants in the first place.
Reply
#7
(Dec-29-2017, 06:13 PM)Hyperimus Wrote: global lives
Lives = 5

lives is not the same as Lives.  Capitalization matters.

But that's unrelated.  Just set it to whatever the starting value is at the top of the file, right under your imports.
Reply
#8
(Dec-29-2017, 06:39 PM)Mekire Wrote: Whole program please. Also, you shouldn't be using global for non-constants in the first place.

It's on my first reply, on a link for download.
Reply
#9
Quote:

Ok, so you're trying to use the variable, before you let python know where that variable is.
            for i in list:
                if i is not None:
                    i.explode()
                    lives -= 1

            global lives
            if lives == 0:
Move the global declaration to the top of the function.

And then, when you start writing real code, never use globals again lol.  It's one of the worst things to do, right next to "goto" abuse.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020