简体   繁体   中英

How do I detect the collision of two sprites using pygame?

This is my code so far, I can move and it places out a blip to pick up, I just need to know how to register that and move the blip to a new random spot! I am very new to pygame and not 100% fluent in python either, but I'm decent. If there are pdf:s good for intermediate coders when it comes to python that would be wonderful!

import sys, pygame, os, math
from random import randint

pygame.init()

size = width, height = 800, 600
speed = [2, 2]
black = 1, 1, 1
screen = pygame.display.set_mode(size)
pygame.display.set_caption('Pick up the squares!')
UP='up'
DOWN='down'
LEFT='left'
RIGHT='right'

ball = pygame.image.load("ball.png")
ballrect = ball.get_rect()
ballx = 400
bally = 300

blip = pygame.image.load("blip.png")
bliprect = blip.get_rect()
blipx = randint(1,800)
blipy = randint(1,600)

background = pygame.Surface(screen.get_size())
background = background.convert()

background.fill((250, 250, 250))


clock = pygame.time.Clock()

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


    keys = pygame.key.get_pressed()

    if keys[pygame.K_LEFT]:
        ballx  -= 5
    if keys[pygame.K_RIGHT]:
        ballx += 5
    if keys[pygame.K_UP]:
        bally -= 5
    if keys[pygame.K_DOWN]:
        bally +=5    

    screen.fill(black)
    screen.blit(ball, (ballx,bally))
    screen.blit(blip, (blipx, blipy))
    pygame.display.update()
    clock.tick(40)

Use the colliderect method of rectangles:

if ballrect.colliderect(bliprect):
    print 'Do something here'

A basic collision detection works like this (assuming you are working with two rectangles):

def does_collide(rect1, rect2):
    if rect1.x < rect2.x + rect2.width and rect1.x + rect1.width > rect2.x \ 
       and rect1.y < rect2.y + rect2.height and rect1.height + rect1.y > rect2.y:
        return True
    return False

Fortunately Pygame is packed with such methods, so you should go with @MalikBrahimi's answer - using colliderect function call, which will do the math for you.

You can also try using pygame.sprite.spritecollide() :

if pygame.sprite.spritecollide(a, b, False):
    pass
    #Do something

a here is your variable name for the class for one of the sprites in the collision. b here is the group that your second sprite will be. You can set the last one to True, which will remove the sprite from the group . Setting to False will keep it the way it is. Here is an example:

if pygame.sprite.spritecollide(my_ball, ballGroup, False):
    Ball.speed[0] = -Ball.speed[0]
    hit.play()
Ball.move()

The variable name for my sprite is my_ball . The group containing the other sprite(s) in this collision is ballGroup . I set the last one to False to keep all the sprites on the surface. If I set to True, the sprite from ballGroup will be removed from the group and screen. I hope this helps you!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM