Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Python (http://www.programmingforums.org/forum43.html)
-   -   name error (http://www.programmingforums.org/showthread.php?t=14707)

dognayr15 Dec 7th, 2007 7:21 PM

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)

Sane Dec 7th, 2007 7:34 PM

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.

dognayr15 Dec 7th, 2007 8:52 PM

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

Jessehk Dec 7th, 2007 10:17 PM

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.

titaniumdecoy Dec 7th, 2007 11:46 PM

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?

dognayr15 Dec 8th, 2007 9:03 AM

Re: name error
 
maybe if i knew how to wrap it then i would

Infinite Recursion Dec 8th, 2007 9:16 AM

Re: name error
 
[ CODE ] your code [ / CODE ] without the spaces in the tags.

dognayr15 Dec 8th, 2007 9:36 AM

Re: name error
 
ty sir



:

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)






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"

Arevos Dec 8th, 2007 10:43 AM

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)


dognayr15 Dec 8th, 2007 11:02 AM

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???


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