Collision with images

Viewed 48

I have to do a game in 2d using pygame and I don't know how to detect collisions with images

I have tested the collisions with rectangles and it works, but when I try to collide a rectangle with an image, the application crash

import pygame,sys
from pygame.locals import *
from random import randint

pygame.init()
ventana=pygame.display.set_mode((1200,600))
pygame.display.set_caption("Jueguiño")

imageVida= pygame.image.load("vida.jpg")
posX= randint(0,1200)
posY= randint(0,0)

rectangulo=pygame.Rect(0,0,100,50)


velocidad=0.5

Blanco=(255,255,255)

while True:
    ventana.fill(Blanco)
    ventana.blit(imageVida,(posX,posY))
    pygame.draw.rect(ventana,(180,70,70),rectangulo)

    rectangulo.left,rectangulo.top=pygame.mouse.get_pos()
    if rectangulo.colliderect(imageVida):
        velocidad=0
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()

    if posY<1200:
        posY+=velocidad
    pygame.display.update()
2 Answers

PyGame is does not collide-test images with rectangles, it collides rectangles with rectangles. So the code needs to track the position of the image as-if it is a rectangle.

This is quite easy. Initially get the rectangular-shape of the image with .get_rect(), this gives the code a PyGame rect object. Then the code needs only move and collide the rectangle, but also must ensure the image is always painted at the rectangle's co-ordinates to keep everything synchronised.

import pygame,sys
from pygame.locals import *
from random import randint

pygame.init()
ventana=pygame.display.set_mode((1200,600))
pygame.display.set_caption("Jueguiño")

imageVida= pygame.image.load("vida.jpg")
vidaRect = imageVida.get_rect()            # <-- Get the Image's Rectangle
posX= randint(0,1200)
posY= randint(0,0)
vidaRect.center = ( posX, posY )           # <-- Position the Rectangle

rectangulo=pygame.Rect(0,0,100,50)

velocidad=0.5

Blanco=(255,255,255)

while True:
    ventana.fill(Blanco)
    ventana.blit( imageVida, ( vidaRect.x, vidaRect.y ) )  # <-- Draw at the Rectangle
    pygame.draw.rect(ventana,(180,70,70),rectangulo)

    rectangulo.left,rectangulo.top=pygame.mouse.get_pos()

    if rectangulo.colliderect( vidaRect ):  # <-- Collide-test the Rectangle
        velocidad=0
        print("Collision!")

    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()

    if posY<1200:
        posY+=velocidad
    pygame.display.update()

It's probably valuable to read the documentation on the PyGame Sprite Class. It is a complete set of functions for handling exactly this sort of work. It is a little more complicated at first, but then simplifies a great deal of the later work - like colliding groups of sprites, for example. There are numerous examples of using sprites in the PyGame answers on SO.

Don't try to collide with the image directly. Instead, use the .get_rect() function to get a rectangle for the image that you can check for collisions with.

Pygame docs for .get_rect().

Related