![]() |
name error
below is my code and whenever i run it it says: "NameError: name 'Ship' is not defined"
class Ship(games.Sprite): """The player's ship.""" image = games.load_image("ship.bmp") ROTATION_STEP = 3 def update(self): """Rotates based on keys pressed.""" if games.keyboard.is_pressed(games.K_LEFT): self.angle -= Ship.ROTATION_STEP if games.keyboard.is_pressed(games.K_RIGHT): self.angle += Ship.ROTATION_STEP the_ship = Ship(image = Ship.image, x = games.screen.width/2, y = games.screen.height/2) games.screen.add(the_ship) |
Re: name error
Wrap your code in [code][/code] tags.
What line are you getting the error on? You might need to show more source too, as I don't think the error belongs to this class. Everything looks fine, assuming it's indented how I think. |
Re: name error
class Ship(games.Sprite):
"""The player's ship.""" image = games.load_image("ship.bmp") ROTATION_STEP = 3 def update(self): """Rotates based on keys pressed.""" if games.keyboard.is_pressed(games.K_LEFT): self.angle -= Ship.ROTATION_STEP if games.keyboard.is_pressed(games.K_RIGHT): self.angle += Ship.ROTATION_STEP the_ship = Ship(image = Ship.image, x = games.screen.width/2, y = games.screen.height/2) games.screen.add(the_ship) and i am getting the error in the 13th line |
Re: name error
I will repeat Sane and ask that you wrap your code in the appropriate tags to allow for indentation and highlighting. Doing so will help us to help you.
|
Re: name error
Clearly the OP didn't read the reply to his original post or he would have known to use code tags in his second post. Why should we waste our time trying to answer a question knowing our responses will be ignored?
|
Re: name error
maybe if i knew how to wrap it then i would
|
Re: name error
[ CODE ] your code [ / CODE ] without the spaces in the tags.
|
Re: name error
ty sir
:
class Ship(games.Sprite)::
now can someone plz help me i am getting a name error on the line that is printed in bold... the error reads as follows: "NameError: name 'Ship' is not defined" |
Re: name error
Er, you've still got the code tags wrong. Don't encase every line, just the whole block.
I've taken the liberty of doing this for you: :
class Ship(games.Sprite):And now it's formatted like this, the answer is obvious: you have the indentation wrong. You're trying to instantiate a class before it has been fully defined. You want something more like this: :
class Ship(games.Sprite):Additionally, I'm not sure how the Sprite class works, but I suspect you could do: :
class Ship(games.Sprite): |
Re: name error
but i have a problem i have more code that falls under the Ship class and if i unindent the parts u had stated then the code will look like this:
:
class Ship(Collider):instead of: :
class Ship(Collider):will the code still work??? |
| All times are GMT -5. The time now is 3:44 AM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC