2

虽然当我使用pygame.sprite.collide_rector时检测到碰撞pygame.sprite.collide_circle,但当我尝试为精灵分配位掩码并运行它时,没有检测到碰撞。(这wall.collision_action()使得第一个圆圈以与另一个圆圈相同的速度和方向移动)虽然这现在不是问题,
因为我正在使用圆圈的图像,这使得 pygame.sprite.collide_circle 工作正常,将来当我将使用更详细和非圆形的精灵。

代码:

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

class ball_class(pygame.sprite.Sprite):
    def __init__(self, surface):
        self.surface = surface
        self.x = 250
        self.y = 250
        self.vy = 0
        self.vx = 0
        self.sprite = pygame.sprite.Sprite()
        self.sprite.image = pygame.image.load("ball.png").convert()
        self.sprite.rect = self.sprite.image.get_rect(center = (self.x, self.y))
        self.sprite.rect.topleft = [int(self.x), int(self.y)]
        self.sprite.mask = pygame.mask.from_surface(self.sprite.image)

    def event(self, event):
        if event.key == K_UP:
            self.vy = -1
            self.vx = 0
        elif event.key == K_DOWN:
            self.vy = 1
            self.vx = 0
        elif event.key == K_LEFT:
            self.vx = -1
            self.vy = 0
        elif event.key == K_RIGHT:
            self.vx = 1
            self.vy = 0

    def move(self):
        self.y += self.vy
        self.x += self.vx
        self.sprite.rect.topleft = [int(self.x), int(self.y)]
        self.sprite.mask = pygame.mask.from_surface(self.sprite.image)

    def draw(self, surface):
        surface.blit(self.sprite.image, self.sprite.rect)

    def position(self):
        return self.sprite.rect()

class wall_class(pygame.sprite.Sprite):
    def __init__(self, surface):
        self.surface = surface
        self.x = 250
        self.y = 100
        self.vy = 0
        self.sprite = pygame.sprite.Sprite()
        self.sprite.image = pygame.image.load("ball.png").convert()
        self.sprite.rect = self.sprite.image.get_rect(center = (self.x, self.y))
        self.sprite.rect.topleft = [int(self.x), int(self.y)]
        self.sprite.mask = pygame.mask.from_surface(self.sprite.image)

    def draw(self, surface):
        surface.blit(self.sprite.image, self.sprite.rect)

    def collision_action(self):
        self.vy = ball.vy
        self.vx = ball.vx
        self.x += self.vx
        self.y += self.vy
        self.sprite.rect.topleft = [int(self.x), int(self.y)]

def gameQuit():
    pygame.quit()
    sys.exit()

screen = pygame.display.set_mode((500, 500), 0, 32)
ball = ball_class(screen)
wall = wall_class(screen)
clock = pygame.time.Clock()
while True:
    screen.fill((0, 0, 0))
    ball.move()
    ball.draw(screen)
    wall.draw(screen)
    is_a_collision = pygame.sprite.collide_mask(wall.sprite, ball.sprite)
    if is_a_collision:

        wall.collision_action()

    for event in pygame.event.get():
        if event.type == QUIT:
            gameQuit()
        elif event.type == KEYDOWN:
            ball.event(event)
    clock.tick(100)
    pygame.display.update()
4

1 回答 1

0

位掩码冲突在您的代码起作用,但我认为代码使其变得更加困难,因为精灵对象ball_classwall_class中定义了单独的子精灵对象。

class ball_class(pygame.sprite.Sprite):
    def __init__(self, surface):
        self.surface = surface
        self.x = 250
        self.y = 250
        self.vy = 0
        self.vx = 0
        self.sprite = pygame.sprite.Sprite()    # <-- HERE
        ...

通常,当代码“子类化”现有的类/对象时,它会变成相同的类/对象之一。所以没有必要在类中包含次要精灵。这也使得代码和逻辑更加复杂、棘手和混乱。

请考虑重新加工您的ball_class

class ball_class(pygame.sprite.Sprite):
    def __init__(self, surface):
        pygame.sprite.Sprite.__init__(self)    # <-- HERE Must initialise sprites
        self.x     = 250
        self.y     = 250
        self.vy    = 0
        self.vx    = 0
        self.image = pygame.image.load("ball_64.png").convert_alpha()
        self.rect  = self.image.get_rect(center = (self.x, self.y))
        self.mask  = pygame.mask.from_surface(self.image)

注意image,rectmask成员变量。以前,代码是在包含的精灵中使用这些。但是 PyGame 库被编写为期望这些作为类的成员,如果没有定义它们就不能正常工作。您的两个精灵类也没有调用基本精灵初始化函数。

这是您的代码的重新工作,它 - 当球撞到墙角时,会正确碰撞(使用位掩码)。

ball_64.png ball_64.png

墙.png wall.png

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

class ball_class(pygame.sprite.Sprite):
    def __init__(self, surface):
        pygame.sprite.Sprite.__init__(self)
        self.surface = surface
        self.x = 250
        self.y = 250
        self.vy = 0
        self.vx = 0
        self.image = pygame.image.load("ball_64.png").convert_alpha()
        self.rect  = self.image.get_rect(center = (self.x, self.y))
#        self.sprite.rect.topleft = [int(self.x), int(self.y)]
        self.mask = pygame.mask.from_surface(self.image)

    def event(self, event):
        if event.key == K_UP:
            self.vy = -1
            self.vx = 0
        elif event.key == K_DOWN:
            self.vy = 1
            self.vx = 0
        elif event.key == K_LEFT:
            self.vx = -1
            self.vy = 0
        elif event.key == K_RIGHT:
            self.vx = 1
            self.vy = 0

    def move(self):
        self.y += self.vy
        self.x += self.vx
        self.rect.topleft = [int(self.x), int(self.y)]
        #self.mask = pygame.mask.from_surface(self.sprite.image)

    def draw(self, surface):
        surface.blit(self.image, self.rect)

    def position(self):
        return self.rect()

class wall_class(pygame.sprite.Sprite):
    def __init__(self, surface):
        pygame.sprite.Sprite.__init__(self)
        self.x = 250
        self.y = 100
        self.vy = 0
        self.image = pygame.image.load("wall.png").convert_alpha()
        self.rect  = self.image.get_rect(center = (self.x, self.y))
#        self.sprite.rect.topleft = [int(self.x), int(self.y)]
        self.mask = pygame.mask.from_surface( self.image )

    def draw(self, surface):
        surface.blit(self.image, self.rect)

    def collision_action(self, by_sprite_at ):
        print("wall_class.collision_action( by_sprite_at=%s )" % ( str( by_sprite_at ) ) )
#        self.vy = ball.vy
#        self.vx = ball.vx
#        self.x += self.vx
#        self.y += self.vy
#        self.sprite.rect.topleft = [int(self.x), int(self.y)]

def gameQuit():
    pygame.quit()
    sys.exit()

screen = pygame.display.set_mode((500, 500), 0, 32)
ball = ball_class(screen)
wall = wall_class(screen)
clock = pygame.time.Clock()
while True:
    screen.fill((128,128,128))
    ball.move()
    ball.draw(screen)
    wall.draw(screen)
    is_a_collision = pygame.sprite.collide_mask(wall, ball)
    if is_a_collision:
        wall.collision_action( ball.rect.center )

    for event in pygame.event.get():
        if event.type == QUIT:
            gameQuit()
        elif event.type == KEYDOWN:
            ball.event(event)
    clock.tick(100)
    pygame.display.update()
于 2020-02-27T22:06:31.883 回答