Jump to content
  • Advertisement
Sign in to follow this  
ErikNavarro

Python Help !

This topic is 2982 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts


from livewires import games
games.init(screen_width = 640, screen_height = 480, fps = 50 )


class LeftWalk(games.Animation):
""" A Animation Of Samus Walking Left """

images1 = ["1.png",
"2.png",
"3.png",
"4.png",
"5.png",
"6.png",
"7.png"]
images2 = ["b1.png",
"b2.png",
"b3.png",
"b4.png",
"b5.png",
"b6.png",
"b7.png"]
count = 0


def __init__(self):
""" Initialize animation object """
super(LeftWalk, self).__init__(images = LeftWalk.images1,
repeat_interval = 10)
def draw (self,x_cord, y_cord):
""" Draw Hpaddle to the screen """
self.x = int(x_cord)
self.y = int(y_cord)

def update(self):
global count
if games.keyboard.is_pressed(games.K_UP):

if count == 0:

ex = self.x
why = self.y
self.destroy
super(LeftWalk, self).__init__(images = LeftWalk.images2,
repeat_interval = 4)
self.draw(ex,why)
count = 1
self.x = (self.x + 5 )

if games.keyboard.is_pressed(games.K_DOWN):
if count == 0:
ex = self.x
why = self.y
self.destroy
super(LeftWalk, self).__init__(images = LeftWalk.images1,
repeat_interval = 4)
self.draw(ex,why)
count = 1
self.x = (self.x - 5 )
if games.keyboard.is_pressed(games.K_UP) == False:
if games.keyboard.is_pressed(games.K_DOWN) == False:
if count == 1:
ex = self.x
why = self.y
self.destroy
super(LeftWalk, self).__init__(images = LeftWalk.images3,
repeat_interval = 4)
self.draw(ex,why)




count = 0
#self.destroy()


def main():
countz = 0
wall_image = games.load_image("Background_1.png", transparent = False)
games.screen.background = wall_image

samus_1 = LeftWalk()
samus_1.draw(200,300)

games.screen.add(samus_1)
games.screen.mainloop()

main()





My problem occurs in the move() method, specifically in the third series of "if "statements. I receive the error message " global variable count is not defined "
The variable is clearly defined, and there is a global statement at the beginning of the method. I would also like to add that the count variable is accessed by the two first two blocks of "if" statements without a problem.

The error occurs at this line :

if games.keyboard.is_pressed(games.K_UP) == False:
if games.keyboard.is_pressed(games.K_DOWN) == False:
if count == 1:





Someone please help me, this is driving me insane.

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by ErikNavarro
I receive the error message " global variable count is not defined "
The variable is clearly defined, and there is a global statement at the beginning of the method.
It is clearly defined, but it isn't a global per se. Is there any reason why you are not accessing it as LeftWalk.count, or more simply, self.count?

Share this post


Link to post
Share on other sites
I tried that as well.. it generates a similar error. Also, the code works if I define count before I define the class.. after the imports.

Share this post


Link to post
Share on other sites
In your update method, try removing the line: global count
Then everywhere you reference count, change it to: LeftWalk.count

Hopefully that works - I'm new to Python as well.

Share this post


Link to post
Share on other sites
What swiftcoder said should work. This works in python 2.6.2. Note that you want to use global only if the variable is global, not part of an object.

x=2
class C:
x=1
def hi1(self):
print self.x
def hi2(self):
print C.x
def hi3(self):
global x
print x
c=C()
c.hi1()
c.hi2()
c.hi3()
==== outputs ====
1
1
2

Share this post


Link to post
Share on other sites
1
1
2

The first two output 1, the third outputs 2.

Don't think that self.x and C.x are equivalent though, they point to the same variable in this example, but x resides on the class, not on the instance.

Share this post


Link to post
Share on other sites
Well judging from the example you just gave me.. my code should work as well.. right ?

I did the same exact thing as the third output.

Why won't my code work ?

I hate little problems like these lol. Super frustrating.

Share this post


Link to post
Share on other sites
Quote:
Original post by ErikNavarro
I tried that as well.. it generates a similar error. Also, the code works if I define count before I define the class.. after the imports.


Quote:
Original post by ErikNavarro
Well judging from the example you just gave me.. my code should work as well.. right ?

I did the same exact thing as the third output.

Why won't my code work ?

I hate little problems like these lol. Super frustrating.


The difference is that in your case you're saying "global count". This is telling Python to look OUTSIDE of the class for "count", which is not defined unless you put the "count" outside of the class - it won't create a new global variable for you. For example, you defined a global "count" when you put "count" before the class definition.

Did you try getting rid of the "global count", then showing Python where "count" is (it is part of the class) by using "LeftWalk.count" instead?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!