2

I am trying to check a collision between one static, and one moving image. The static image is a balloon and the moving image is a gun. I've tried to create a function that gets the rect of the gun (using get_rect), checks if one of the (7) balloons (stored in a list) has the same x coordinate (x coordinate is randomly generated), and prints out which balloon it collided with. But it doesn't always seem to work, and only works in a certain position. Also, it prints out something like collision with [<Surface(444x250x32 SW)> when it should print the name (variable name) of which one of the balloons it hit.

Edit: Using Rabbid's suggestions, I've made a mask around both the balloon and the gun and tried checking those for the collision. They still don't work, but I feel like I am getting closer. I also made a repl, so you can run the code yourself. Here it is: Repl

enter image description here (When the gun is in the same position as the balloon, it should print out which balloon it hit.)

How can I check the collisions between these images properly, and print out which balloon was hit?

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
krmogi
  • 2,588
  • 1
  • 10
  • 26
  • I've given you a nice answer. Why have you delete it? [Collisions between two objects not working correctly](https://stackoverflow.com/questions/69239164/pygame-collisions-of-two-images-not-consistent) – Rabbid76 Sep 19 '21 at 05:41

1 Answers1

1

pygame.Surface.get_rect.get_rect() returns a rectangle with the size of the Surface object, that always starts at (0, 0) since a Surface object has no position. A Surface is blit at a position on the screen. The position of the rectangle can be specified by a keyword argument. For example, the top left of the rectangle can be specified with the keyword argument topleft. e.g.:

gun_rect = gun.get_rect(topleft = (x, y))

Read How do I detect collision in pygame? and use pygame.Rect.collidepoint to detect the collision of the gun tip (gun_rect.topleft) with the enclosing rectangle of a balloon (ballon_rect):

def check_collisions(x, y):
    for i in range(num_balloons):
        gun_rect = gun.get_rect(topleft = (x,y))
        ballon_rect = colors[i].get_rect(topleft = (balloon_list[i] - 100, y-90))
        if ballon_rect.collidepoint(gun_rect.topleft):
            print(f'collision with {colors[i]}')
while running: # Game loop #
    # [...]

    check_collisions(x, y)

    # [...]

Note, that the position of the balloon is (balloon_list[i] - 100, y-90), since you are drawing it at that position:

def draw_balloons(y):
   for i in range(num_balloons):
       screen.blit(colors[i], (balloon_list[i] - 100, y-90))   
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • This is working a little better, however, even when the gun is not on the balloon, it seems that the balloon is being hit. I wonder if this is happening because the rect is too big? – krmogi Sep 19 '21 at 18:52
  • I think the balloons are bigger than the picture of the gun as you can see in my code. The balloon is 444 by 250 pixels and the gun is 150 by 150 pixels – krmogi Sep 19 '21 at 19:08
  • @krishayras I bet the balloon is just a small dot on a big image. See [How to get the correct dimensions for a pygame rectangle created from an image](https://stackoverflow.com/questions/65361582/how-to-get-the-correct-dimensions-for-a-pygame-rectangle-created-from-an-image/65361896#65361896) to understand what I mean. Actually this is a new problem. You can't keep updating the question and adding new issues once the original issue is resolved. – Rabbid76 Sep 19 '21 at 19:27
  • @krishayras A collision test between the tip of the gun and the enclosing rectangle of the balloon may not be what you want. Read about [PyGame collision with masks](https://stackoverflow.com/questions/57455811/pygame-collision-with-masks/57499484#57499484). – Rabbid76 Sep 19 '21 at 19:50
  • I read your great answer on Pygame collision with masks, and I came up with this. [REPL](https://replit.com/@krmogi/repl-pygame-project#main.py). It still doesn't work, but I feel like I'm getting closer. It only happens to work in the center couple of balloons. If you have time, could you look at this and possibly tell me how to fix it. I now added all the images, so you can really run it. – krmogi Sep 19 '21 at 23:27
  • 1
    I looked at the Images, and turns out you were right! The pictures had a lot of empty room. I cropped out all of the pictures and I decreased the size of the balloons a bit, and now they work very well. Thank you so much! – krmogi Sep 21 '21 at 02:00