Thread: name error
View Single Post
Old Dec 8th, 2007, 11:40 AM   #11
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 1,835
Rep Power: 5 Sane will become famous soon enough
Send a message via MSN to Sane
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