943,634 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 1802
  • Python RSS
Sep 21st, 2009
0

Moving an object with control keys in a sprite

Expand Post »
Hello,

I know this is a very simple problem, but I am about to give up... Why couldn't I get the control keys do their job? Maybe someone whose eyes are more rested than mine can tell me. I will appreciate any help.

I am dealing with pygame, and I have two images loaded on the screen when running the program. However, what I want the first image in the first sprite to do is moving around with controlled keys by me, such as, move up, left, down, and right.

By the way, needless to say, I am new to pygame. It actually is so much fun, especially when things are smooth.

Here is my code:

Python Syntax (Toggle Plain Text)
  1.  
  2. #Coding starts here
  3.  
  4. """The elder moves when pressed the control keys & when collides
  5. with the scared guy, makes him disappear
  6. Modified on September 20th, 2009"""
  7.  
  8. import pygame
  9. pygame.init()
  10.  
  11. screen = pygame.display.set_mode((960, 720)) #Resolution has been changed by me
  12.  
  13. class Elderly(pygame.sprite.Sprite):
  14. def __init__(self):
  15. pygame.sprite.Sprite.__init__(self)
  16. self.image = pygame.image.load("ELDERLY1.png")
  17. self.rect = self.image.get_rect()
  18. self.rect.centerx = 100
  19. self.rect.centery = 150
  20.  
  21. def update(self):
  22. self.rect.center = pygame.image.load()
  23. #if self.rect.right > screen.get_width():
  24. # self.rect.left = 0
  25.  
  26. class Ohno(pygame.sprite.Sprite):
  27. def __init__(self):
  28. pygame.sprite.Sprite.__init__(self)
  29. self.image = pygame.image.load("OHNO2.png")
  30. self.rect = self.image.get_rect()
  31. self.rect.centerx = 800
  32. self.rect.centery = 450
  33.  
  34. def update(self):
  35. self.rect.center = pygame.image.load()
  36. #if self.rect.right > screen.get_width():
  37. # self.rect.left = 0
  38.  
  39. def main():
  40.  
  41. #Entities for the background
  42. background = pygame.Surface(screen.get_size())
  43. background = background.convert()
  44. background.fill((238, 232, 170)) #
  45. screen.blit(background, (0, 0))
  46.  
  47. elderly = Elderly()
  48. ohno = Ohno()
  49. allSprites = pygame.sprite.Group(elderly, ohno)
  50.  
  51. clock =pygame.time.Clock()
  52. keepGoing = True
  53.  
  54. while keepGoing:
  55. clock.tick(30)
  56.  
  57. for event in pygame.event.get():
  58. if event.type == pygame.QUIT:
  59. keepGoing = False
  60. #Starting from here, modifying the control keys
  61. elif event.type == pygame.KEYDOWN:
  62. if event.key == pygame.K_UP:
  63. elderly.moveup()
  64. if event.key == pygame.K_DOWN:
  65. elderly.movedown()
  66. if event.key == pygame.K_RIGHT:
  67. elderly.moveright()
  68. if event.key == pygame.K.LEFT:
  69. elderly.moveleft()
  70. elif event.type == pygame.KEYUP:
  71. if event.key == pygame.K_UP or event.key == pygame.K_DOWN or event.key == pygame.K_RIGHT or event.key == pygame.K_LEFT:
  72. elderly.movepos = [0,0]
  73. elderly.state = "still"
  74.  
  75. allSprites.clear(screen, background)
  76. allSprites.update()
  77. allSprites.draw(screen)
  78.  
  79. pygame.display.flip()
  80.  
  81. if __name__ == "__main__":
  82. main()
  83.  
  84. #Coding ends here

I have been up for hours and hours and tried some searching. I also modified the code a little bit. When I hit the control keys, it used to shut itself down. At least, it doesn't do it anymore, but I am getting some attribute errors.

Thank you very much in advance.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
blueness25 is offline Offline
3 posts
since Nov 2007
Sep 21st, 2009
0

Re: Moving an object with control keys in a sprite

This block of code looks like it's presenting the problem:
PYTHON Syntax (Toggle Plain Text)
  1. elif event.type == pygame.KEYDOWN:
  2. if event.key == pygame.K_UP:
  3. elderly.moveup()
  4. if event.key == pygame.K_DOWN:
  5. elderly.movedown()
  6. if event.key == pygame.K_RIGHT:
  7. elderly.moveright()
  8. if event.key == pygame.K.LEFT:
  9. elderly.moveleft()
  10. elif event.type == pygame.KEYUP:
  11. if event.key == pygame.K_UP or event.key == pygame.K_DOWN or event.key == pygame.K_RIGHT or event.key == pygame.K_LEFT:
  12. elderly.movepos = [0,0]
  13. elderly.state = "still"

If you take another look at your Elderly class, it doesn't have any methods defined called moveright(), moveleft(), moveup(), or movedown(), or properties called movepos or state for that matter either!

So you'll need to define them in your Elderly class.
e.g.
inside your definition of Elderly you need to define the moveup function (and the other missing functions like this):
PYTHON Syntax (Toggle Plain Text)
  1. def moveup():
  2. #TODO: add code to move your elder upwards
  3.  
  4. def movedown():
  5. #TODO: add code to move your elder downwards
  6.  
  7. # etc etc.....

You should also add and initialise the missing properties (movepos and state) in the __init__ function for the Elderly class!

Give that a go and let us know how you get on!

Cheers for now,
Jas.
Reputation Points: 590
Solved Threads: 123
Practically a Master Poster
JasonHippy is offline Offline
672 posts
since Jan 2009
Sep 21st, 2009
0

Re: Moving an object with control keys in a sprite

And don't forget to use self to turn those functions into methods for the instance, like
def moveup(self):
Reputation Points: 961
Solved Threads: 211
Nearly a Posting Maven
sneekula is offline Offline
2,413 posts
since Oct 2006
Sep 23rd, 2009
0

Re: Moving an object with control keys in a sprite

Thank you very much to all responders.

After working a while on this code, I got everything I needed smooth and working. Here is how I figured out the control keys:

Python Syntax (Toggle Plain Text)
  1. #Starting from here, modifying the control keys
  2. elif event.type == pygame.KEYDOWN:
  3. if event.key == pygame.K_UP:
  4. elderly.dy -= 20
  5. if event.key == pygame.K_DOWN:
  6. elderly.dy += 20
  7. if event.key == pygame.K_LEFT:
  8. elderly.dx -= 20
  9. if event.key == pygame.K_RIGHT:
  10. elderly.dx += 20
  11. #The part below is from http://www.pygame.org/docs/tut/tom/games5.html
  12. elif event.type == pygame.KEYUP: #Occurs when user releases the control keys
  13. if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
  14. elderly.dy = 0
  15. if event.key == pygame.K_RIGHT or event.key == pygame.K_LEFT:
  16. elderly.dx = 0

I hope this will be helpful for someone else too.

pygame is fun!

Best wishes to everyone.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
blueness25 is offline Offline
3 posts
since Nov 2007
Sep 23rd, 2009
0

Re: Moving an object with control keys in a sprite

Oh, sorry, I forgot to add that the Elderly class should be modified as seen below:

Python Syntax (Toggle Plain Text)
  1.  
  2. class Elderly(pygame.sprite.Sprite): #Creating the first Sprite within its class for the first image
  3. def __init__(self): #Built-in Sprite method & Called when the object is created
  4. pygame.sprite.Sprite.__init__(self) #Call the constructor for Sprite
  5. self.image = pygame.image.load("ELDERLY1.png") #Load the first image
  6. self.image = self.image.convert_alpha() #Create the Sprite image & New copy of the surface with the desired pixel format.
  7.  
  8. self.rect = self.image.get_rect() #Create the rect attribute for Sprite
  9. self.rect.centerx = 200 #Horizontal position of the image
  10. self.rect.centery = 200 #Vertical position of the image
  11. self.dx = 0 #Initialize x
  12. self.dy = 0
Reputation Points: 10
Solved Threads: 0
Newbie Poster
blueness25 is offline Offline
3 posts
since Nov 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Python Xlib . Looking for basis docs or tut.
Next Thread in Python Forum Timeline: python assignment help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC