Henry C
Henry C

Reputation: 11

Pygame sprite touching

I am attempting to build a simple game with two sprites. I can't figure out how to add code that detects if a sprite is touching the other. I am also having trouble understanding the countless tutorials on this topic. If you could try and explain this to me as simply as possible that would be amazing!

import pygame
import sys
from pygame.locals import *
pygame.init()

black = (0, 0, 0)
white = (255, 255, 255)
bg = black
square = pygame.image.load('square.png')
square1 = pygame.image.load('square1.png')
screen = pygame.display.set_mode((640, 400))

UP = 'UP'
DOWN = 'DOWN'
LEFT = 'LEFT'
RIGHT = 'RIGHT'
direction = RIGHT
movex, movey, movex1, movey1 = 100, 100, 200, 200

class player1(pygame.sprite.Sprite):
    """Player 1"""

    def __init__(self, xy):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load('square.png')
        self.rect = self.image.get_rect()

class player2(pygame.sprite.Sprite):
    """Player 2"""

    def __init__(self, xy):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load('square1.png')
        self.rect = self.image.get_rect()

while True:
    screen.fill(black)
    collide = pygame.sprite.collide_mask(player1, player2)
    if collide == True:
        print 'collision!'
    for event in pygame.event.get():
        if event.type == KEYDOWN:
            if event.key == K_w:
                movey -= 10 
            elif event.key == K_s:
                movey += 10
            elif event.key == K_a:
                movex -= 10
            elif event.key == K_d:
                movex += 10
            if event.key == K_UP:
                movey1 -= 10 
            elif event.key == K_DOWN:
                movey1 += 10
            elif event.key == K_LEFT:
                movex1 -= 10
            elif event.key == K_RIGHT:
                movex1 += 10
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
    screen.blit(square, (movex, movey))
    screen.blit(square1, (movex1, movey1))
    pygame.display.update()

Upvotes: 1

Views: 5341

Answers (1)

sloth
sloth

Reputation: 101162

Some issues first:

  • pygame.sprite.collide_mask never returns True. It returns a point or None. So your check collide == True will never be evaluate to True
  • pygame.sprite.collide_mask excepts two Sprite instances, but you call it with class objects as arguments (collide_mask(player1, player2))
  • You only need pygame.sprite.collide_mask when you want to do pixel perfect collision detection
  • You actually don't use the classes player1 and player2 in the rest of your code

If you're using the Sprite class, a simply way for collision detection is to use the Group class. But since you only have two Sprites, you can simple check for an intersection of their Rects using colliderect.

I've updated your code to make use of the Sprite class:

import pygame
import sys
from pygame.locals import *
pygame.init()

black = (0, 0, 0)
white = (255, 255, 255)

screen = pygame.display.set_mode((640, 400))

class Player(pygame.sprite.Sprite):

    def __init__(self, image, x, y):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load(image)
        self.rect = self.image.get_rect(x=x, y=y)

player1, player2 = Player('square.png', 100, 100), Player('square1.png', 200, 200)
players = pygame.sprite.Group(player1, player2)

while True:
    screen.fill(black)

    if player1.rect.colliderect(player2.rect):
        print 'collision!'

    for event in pygame.event.get():
        if event.type == KEYDOWN:
            if event.key == K_w:
                player1.rect.move_ip(0, -10) 
            elif event.key == K_s:
                player1.rect.move_ip(0, 10)
            elif event.key == K_a:
                player1.rect.move_ip(-10, 0)
            elif event.key == K_d:
                player1.rect.move_ip(10, 0)
            if event.key == K_UP:
                player2.rect.move_ip(0, -10)
            elif event.key == K_DOWN:
                player2.rect.move_ip(0, 10)
            elif event.key == K_LEFT:
                player2.rect.move_ip(-10, 0)
            elif event.key == K_RIGHT:
                player2.rect.move_ip(10, 0)
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
    players.draw(screen)
    pygame.display.update()

Collision detection is a broad topic, but this should get you started.

Upvotes: 3

Related Questions