![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Dec 2007
Posts: 5
Rep Power: 0
![]() |
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) |
|
|
|
|
|
#2 |
|
Programming Guru
![]() |
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. |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Dec 2007
Posts: 5
Rep Power: 0
![]() |
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 |
|
|
|
|
|
#4 |
|
The Oblivious One
Join Date: May 2005
Location: Ontario, Canada
Posts: 641
Rep Power: 4
![]() |
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.
__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS! |
|
|
|
|
|
#5 |
|
Expert Programmer
|
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?
|
|
|
|
|
|
#6 |
|
Newbie
Join Date: Dec 2007
Posts: 5
Rep Power: 0
![]() |
Re: name error
maybe if i knew how to wrap it then i would
|
|
|
|
|
|
#7 |
|
Programming Guru
![]() ![]() ![]() |
Re: name error
[ CODE ] your code [ / CODE ] without the spaces in the tags.
__________________
http://jasonpowers.net "There are a thousand hacking at the branches of evil to one who is striking at the root." |
|
|
|
|
|
#8 |
|
Newbie
Join Date: Dec 2007
Posts: 5
Rep Power: 0
![]() |
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" Last edited by dognayr15; Dec 8th, 2007 at 8:47 AM. |
|
|
|
|
|
#9 |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
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):
"""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 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):
"""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)Additionally, I'm not sure how the Sprite class works, but I suspect you could do: class Ship(games.Sprite):
"""The player's ship."""
ROTATION_STEP = 3
def __init__(self, x, y):
image = games.load_image("ship.bmp")
games.Sprite.__init__(self, image, x, y)
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(x = games.screen.width / 2,
y = games.screen.height / 2)
games.screen.add(the_ship) |
|
|
|
|
|
#10 |
|
Newbie
Join Date: Dec 2007
Posts: 5
Rep Power: 0
![]() |
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):
"""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
super(Ship, self).die()
the_ship = Ship(image = Ship.image,
x = games.screen.width/2,
y = games.screen.height/2)
games.screen.add(the_ship)
import math, random
VELOCITY_STEP = .03
sound = games.load_sound("thrust.wav")
# apply thrust based on up arrow key
if games.keyboard.is_pressed(games.K_UP):
Ship.sound.play()
# change velocity based on ship's angle
angle = self.angle * math.pi / 180 #convert to radian
self.dx += Ship.VELOCITY_STEP * math.sin(angle)
self.dy += Ship.VELOCITY_STEP * -math.cos(angle)instead of: class Ship(Collider):
"""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
super(Ship, self).die()
the_ship = Ship(image = Ship.image,
x = games.screen.width/2,
y = games.screen.height/2)
games.screen.add(the_ship)
# Astrocrash 03
# Engages ship's engine
import math, random
VELOCITY_STEP = .03
sound = games.load_sound("thrust.wav")
# apply thrust based on up arrow key
if games.keyboard.is_pressed(games.K_UP):
Ship.sound.play()
# change velocity based on ship's angle
angle = self.angle * math.pi / 180 #convert to radian
self.dx += Ship.VELOCITY_STEP * math.sin(angle)
self.dy += Ship.VELOCITY_STEP * -math.cos(angle)will the code still work??? |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Basic Socket Programming Error in Linux.. Pls help | boraciner | C++ | 18 | Sep 12th, 2007 1:17 AM |
| C# corruption!!! | Kilo | C++ | 32 | May 21st, 2006 8:44 PM |
| Masm | rsnd | Assembly | 4 | May 20th, 2006 9:05 PM |
| libraries | matko | C | 1 | Jan 22nd, 2006 2:12 PM |
| String error in if statement | Blighttdm | C | 12 | Nov 18th, 2005 6:34 PM |