Pygame精灵触摸

时间:2014-05-14 11:26:22

标签: python pygame collision

我正在尝试用两个精灵建立一个简单的游戏。我无法弄清楚如何添加代码来检测精灵是否正在接触另一个精灵。我也无法理解关于这个主题的无数教程。如果您可以尽可能简单地向我解释这一点,这将是惊人的!

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()

1 个答案:

答案 0 :(得分:2)

首先是一些问题:

  • pygame.sprite.collide_mask永远不会返回True。它返回一个点或None。因此,您的支票collide == True永远不会评估为True
  • pygame.sprite.collide_mask排除了两个Sprite个实例,但您将class个对象称为参数(collide_mask(player1, player2)
  • 当您想要进行像素完美碰撞检测时,您只需要pygame.sprite.collide_mask
  • 您实际上并未在代码的其余部分中使用类player1player2

如果您正在使用Sprite类,则只需使用Group类即可进行碰撞检测。但由于你只有两个精灵,你可以使用colliderect简单地检查Rects的交叉点。

我已更新您的代码以使用Sprite类:

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()

碰撞检测是一个广泛的主题,但这应该让你开始。