• Home
  • Blog
  • C# / Unity
  • C++ / Unreal Engine
  • Python strategy games
  • Python arcade games
  • Python other
  • Machine Learning
  • C
  • Scratch
  • Coding and electronics
  • How I learnt to code
  • Project Euler
  • Feedback
  James L.'s coding

James's blog

what I've been doing.

pygame.sprite.spritecollideany

13/10/2017

0 Comments

 
I've finally found out how to do collision detection for my new game Flappy Bird. 

How to do it (the full code is on the arcade games page)

​CODE FOR MAKING A GROUP OF CLASS  MEMBERS AND UPDATING THE GROUP
  1. class Pipe (pygame.sprite.Sprite):
  2.   def __init__(self, image,[...]):
  3.     pygame.sprite.Sprite.__init__(self)
  4.     self.image = image
  5.     self.rect = self.image.get_rect()​
  6.    self.x = 5
  7.   self.y = 5
  8.    [...]
  9.  
  10.  def update(self):
  11.   screen.blit(self.image, (self.x,self.y)
  12.  self.rect = Rect(self.x, self.y, 50,50)
  13. ​
  14. def main():
  15.    pipe_image = image.load('pipe.png')
  16.   pipeGroup = pygame.sprite.group()
  17.   for x in range(3):
  18.   pipeGroup.add(Flappy_class(flappy_image, [...]))
  19. while True:
  20.   pipeGroup.update()
  21. ​  pygame.display.update()
Picture
KEY
line 1. we create the class, inheriting from pygame.
line 2. we "__init__" like usual.  
 Note: "__init__" has 2 underscores
line 3. we call the pygame "__init__"
​line 4. we load the image
  Note: I don't load it in the class because if we had 40 pipes it would be too slow
line 5. we make a rect according to the image
lines 6&7. we create the x and y
​line 10. I make a method called update
  Note: its name is important because it it one of the only methods that you can call for all the sprites in a group.
line 11. we display the image
line 12. we create the rect(x pos, y pos, width, height)
   Note: using self.rect = Rect (...) for the updated image is important for collision detection 
line 15. we load the pipe image.
line 16: we create a sprite group
line 18. we add members to the sprite group
line 20. we call the update method for all the sprites in the group
We would do a similar thing for flappy bird (including updating using self.rect = Rect (...) and store it in the bird object and then call these lines
  1. if pygame.sprite.spritecollideany(bird, pipeGroup):
  2.   print ('collision')
0 Comments



Leave a Reply.

    Advert:

    Author

    I am doing lots of projects every projects all the time and I can't put them all on the website so I've made a blog.

    Archives

    August 2022
    April 2022
    March 2022
    February 2022
    January 2022
    November 2021
    October 2021
    August 2021
    July 2021
    June 2021
    April 2021
    March 2021
    June 2020
    May 2020
    April 2020
    March 2020
    September 2019
    June 2019
    May 2019
    April 2019
    March 2019
    February 2019
    January 2019
    November 2018
    August 2018
    July 2018
    June 2018
    May 2018
    April 2018
    March 2018
    February 2018
    January 2018
    December 2017
    November 2017
    October 2017

    Categories

    All

Proudly powered by Weebly
  • Home
  • Blog
  • C# / Unity
  • C++ / Unreal Engine
  • Python strategy games
  • Python arcade games
  • Python other
  • Machine Learning
  • C
  • Scratch
  • Coding and electronics
  • How I learnt to code
  • Project Euler
  • Feedback