Moving an object with control keys in a sprite

Thread Solved

Join Date: Nov 2007
Posts: 3
Reputation: blueness25 is an unknown quantity at this point 
Solved Threads: 0
blueness25 blueness25 is offline Offline
Newbie Poster

Moving an object with control keys in a sprite

 
0
  #1
Sep 21st, 2009
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:

  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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 308
Reputation: JasonHippy is a jewel in the rough JasonHippy is a jewel in the rough JasonHippy is a jewel in the rough JasonHippy is a jewel in the rough 
Solved Threads: 52
JasonHippy's Avatar
JasonHippy JasonHippy is offline Offline
Posting Whiz

Re: Moving an object with control keys in a sprite

 
0
  #2
Sep 21st, 2009
This block of code looks like it's presenting the problem:
  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):
  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.
There are 10 types of people in this world.....
Those who understand binary .....
And those who don't!
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,273
Reputation: sneekula has a spectacular aura about sneekula has a spectacular aura about 
Solved Threads: 175
sneekula's Avatar
sneekula sneekula is offline Offline
Nearly a Posting Maven

Re: Moving an object with control keys in a sprite

 
0
  #3
Sep 21st, 2009
And don't forget to use self to turn those functions into methods for the instance, like
def moveup(self):
No one died when Clinton lied.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 3
Reputation: blueness25 is an unknown quantity at this point 
Solved Threads: 0
blueness25 blueness25 is offline Offline
Newbie Poster

Re: Moving an object with control keys in a sprite

 
0
  #4
Sep 23rd, 2009
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:

  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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 3
Reputation: blueness25 is an unknown quantity at this point 
Solved Threads: 0
blueness25 blueness25 is offline Offline
Newbie Poster

Re: Moving an object with control keys in a sprite

 
0
  #5
Sep 23rd, 2009
Oh, sorry, I forgot to add that the Elderly class should be modified as seen below:

  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
Reply With Quote Quick reply to this message  
Reply


This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC