Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Dec 7th, 2007, 6:21 PM   #1
dognayr15
Newbie
 
Join Date: Dec 2007
Posts: 5
Rep Power: 0 dognayr15 is on a distinguished road
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)
dognayr15 is offline   Reply With Quote
Old Dec 7th, 2007, 6:34 PM   #2
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 1,869
Rep Power: 5 Sane will become famous soon enough
Send a message via MSN to Sane
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.
Sane is offline   Reply With Quote
Old Dec 7th, 2007, 7:52 PM   #3
dognayr15
Newbie
 
Join Date: Dec 2007
Posts: 5
Rep Power: 0 dognayr15 is on a distinguished road
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
dognayr15 is offline   Reply With Quote
Old Dec 7th, 2007, 9:17 PM   #4
Jessehk
The Oblivious One
 
Jessehk's Avatar
 
Join Date: May 2005
Location: Ontario, Canada
Posts: 644
Rep Power: 4 Jessehk is on a distinguished road
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!
Jessehk is offline   Reply With Quote
Old Dec 7th, 2007, 10:46 PM   #5
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 855
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
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?
titaniumdecoy is offline   Reply With Quote
Old Dec 8th, 2007, 8:03 AM   #6
dognayr15
Newbie
 
Join Date: Dec 2007
Posts: 5
Rep Power: 0 dognayr15 is on a distinguished road
Re: name error

maybe if i knew how to wrap it then i would
dognayr15 is offline   Reply With Quote
Old Dec 8th, 2007, 8:16 AM   #7
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,467
Rep Power: 8 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
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."
Infinite Recursion is offline   Reply With Quote
Old Dec 8th, 2007, 8:36 AM   #8
dognayr15
Newbie
 
Join Date: Dec 2007
Posts: 5
Rep Power: 0 dognayr15 is on a distinguished road
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"

Last edited by dognayr15; Dec 8th, 2007 at 8:47 AM.
dognayr15 is offline   Reply With Quote
Old Dec 8th, 2007, 9:43 AM   #9
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
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)
Arevos is offline   Reply With Quote
Old Dec 8th, 2007, 10:02 AM   #10
dognayr15
Newbie
 
Join Date: Dec 2007
Posts: 5
Rep Power: 0 dognayr15 is on a distinguished road
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???
dognayr15 is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 5:14 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC