Programming Forums
User Name Password Register
 

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

Reply
 
Thread Tools Display Modes
Old Dec 8th, 2007, 11:40 AM   #11
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Posts: 1,799
Rep Power: 5 Sane will become famous soon enough
Re: name error

Wow. Clearly the OP does not understand indentation.

If you indent the_ship (line 13), you are trying to assign an instance of the class before it's even defined.

Furthermore, indenting those last 12 lines is just silly. Now why would you want to do that?

I'm assuming you're just copying someone else's code and trying to modify it? Or following some advanced game making tutorial? These are the problems that stem from being unable to code in a language you want to use for more advanced purposes...


Edit:

I don't like where this thread's heading, so I'll just give you the code how it should look (assuming your lines are merely incorrectly placed ... the rest is up to you):

python Syntax (Toggle Plain Text)
  1. import math, random
  2.  
  3. class Ship(Collider):
  4. """The player's ship."""
  5. image = games.load_image("ship.bmp")
  6. sound = games.load_sound("thrust.wav")
  7.  
  8. ROTATION_STEP = 3
  9. VELOCITY_STEP = .03
  10.  
  11. def update(self):
  12. """Rotates based on keys pressed."""
  13. if games.keyboard.is_pressed(games.K_LEFT):
  14. self.angle -= Ship.ROTATION_STEP
  15. if games.keyboard.is_pressed(games.K_RIGHT):
  16. self.angle += Ship.ROTATION_STEP
  17. super(Ship, self).die()
  18.  
  19. # apply thrust based on up arrow key
  20. if games.keyboard.is_pressed(games.K_UP):
  21. Ship.sound.play()
  22.  
  23. # change velocity based on ship's angle
  24. angle = self.angle * math.pi / 180 #convert to radian
  25. self.dx += Ship.VELOCITY_STEP * math.sin(angle)
  26. self.dy += Ship.VELOCITY_STEP * -math.cos(angle)
  27.  
  28.  
  29. the_ship = Ship(image = Ship.image,
  30. x = games.screen.width/2,
  31. y = games.screen.height/2)
  32.  
  33. games.screen.add(the_ship)

Last edited by Sane; Dec 8th, 2007 at 11:51 AM.
Sane is offline   Reply With Quote
Old Dec 9th, 2007, 7:53 AM   #12
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 4 Arevos is on a distinguished road
Re: name error

To give a little more detail, when you indent something in Python, you're saying it belongs to a block.

python Syntax (Toggle Plain Text)
  1. class Foobar:
  2. n = 10 # This variable belongs to the class Foobar
  3.  
  4. def f(self, x): # And so does this method
  5. self.n = x # But these statements belongs to f
  6. return x # because they have extra indentation

What you can't do is to alternate indentation:
python Syntax (Toggle Plain Text)
  1. class Foobar:
  2. def f(self, n):
  3. self.n = x
  4.  
  5. n = 10 # syntax error
  6.  
  7. return x

But you can do this:
python Syntax (Toggle Plain Text)
  1. class Foobar:
  2. n = 12 # before is okay
  3.  
  4. def f(self, n):
  5. self.n = x
  6. return x
  7.  
  8. n = 10 # and so is after!
Arevos 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:28 PM.

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