Pygame中的旋转蒙版碰撞

时间:2018-05-30 17:12:43

标签: python pygame

所以我试图制作一个有2个物体的游戏,并试图让它让我的程序知道2个物体何时发生碰撞。

if players[0].mask.overlap(players[1].mask, offset):
    print("collided")`

我的物体顺时针和逆时针旋转,并且头部和身体都有面罩。 (我有2个图像,它们都是相同的大小但是一个只包含头部而另一个只包含正文,以便使用mask.from_surface()。)

def turn(self, event):
    self.surface = self.ojSurface
    if event == 0:
        self.deg -= self.turnSpd
    elif event == 1:
        self.deg += self.turnSpd
    self.surface = pygame.transform.rotate(self.surface, self.deg)
    self.newPos = (self.pos[0] - self.surface.get_width() / 2, self.pos[1] - self.surface.get_height() / 2)
    self.mask = pygame.mask.from_surface(self.surface)

然而,我发现当我在旋转时尝试检查碰撞时,面具与图像位于同一位置。它应该通过每个循环调用的移动函数每帧更新掩码

def move(self):
    self.pos[0] = pygame.mouse.get_pos()[0]
    self.pos[1] = pygame.mouse.get_pos()[1]
    self.newPos = (self.pos[0] - self.surface.get_width() / 2, self.pos[1] - self.surface.get_height() / 2)
    self.mask = pygame.mask.from_surface(self.surface)

Faulty Hit Detection

如果你想仔细阅读我到目前为止的内容,那就是:

import pygame

class Player:

    def __init__(self, pos,surface,screen):
        self.pos = [pos[0],pos[1]]
        self.newPos = (pos[0] - surface.get_width()/2, pos[1] - surface.get_height()/2)
        self.deg = 0
        self.surface = surface
        self.ojSurface = surface
        self.screen = screen
        self.mask = pygame.mask.from_surface(self.surface)

    def turn(self, event):
        self.surface = self.ojSurface

        #clockwise
        if event == 0:
            self.deg -= 0.1

        #counter clockwise
        elif event == 1:
            self.deg += 0.1
        self.surface = pygame.transform.rotate(self.surface, self.deg)

        #resetting pos and mask
        self.newPos = (self.pos[0] - self.surface.get_width() / 2, self.pos[1] - self.surface.get_height() / 2)
        self.mask = pygame.mask.from_surface(self.surface)

    def move(self):
        self.pos[0] = pygame.mouse.get_pos()[0]
        self.pos[1] = pygame.mouse.get_pos()[1]
        self.newPos = (self.pos[0] - self.surface.get_width() / 2, self.pos[1] - self.surface.get_height() / 2)
        self.mask = pygame.mask.from_surface(self.surface)

    def draw(self):
        self.screen.blit(self.surface, self.newPos)

screenRes = (640,480)
screen = pygame.display.set_mode(screenRes)
closed = False
players = [Player((320,240),pygame.image.load("body.png"), screen),Player((480,240),pygame.image.load("body.png"), screen)]

controls = [[pygame.K_a,pygame.K_s],[pygame.K_k,pygame.K_l]]
while not closed:
    screen.fill((0, 0, 0))
    keys = pygame.key.get_pressed()
    offset = (int(players[0].newPos[0] - players[1].newPos[0]), int(players[0].newPos[1] - players[1].newPos[1]))

    #collision
    if players[0].mask.overlap(players[1].mask, offset):
        print("collided")

    #controls
    for i in range(len(players)):
        if keys[controls[i][0]]:
            players[i].turn(0)
        if keys[controls[i][1]]:
            players[i].turn(1)
        players[i].draw()

    players[0].move()
    pygame.display.update()
    for event in pygame.event.get():
        # standard quit
        if event.type == pygame.QUIT:
            pygame.display.quit()
            closed = True
            pygame.quit()

2 个答案:

答案 0 :(得分:0)

所以我已经回顾了你的GitHub代码,虽然我不完全确定(我浏览了你的代码,寻找某些被调用的方法),但我相信你并不是每一帧都更新你的面具。在代码中的某处,您必须每帧/循环更新掩码,以使冲突正常工作。尝试更新所有蒙版,头部,正文和Player类中的面具。希望这有帮助!

答案 1 :(得分:0)

您正在以错误的顺序计算offset。从players[0]的位置减去players[1]的位置:

offset = (
    int(players[1].newPos[0] - players[0].newPos[0]),
    int(players[1].newPos[1] - players[0].newPos[1]),
)