[AttributeError:'pygame.Rect'对象在碰撞时没有属性'rect'错误

问题描述 投票:0回答:2

因此,我正在尝试使用实例对我的游戏进行碰撞检测。这是代码:

class Paddle:
def __init__(self, x, y, width, height, rect):
    self.x = x
    self.y = y
    self.width = width
    self.height = height
    self.rect = pygame.rect.Rect(x, y, width, height)
class Ball:
    coordinateX = 600
    coordinateY = 300
    velocity = [random.randint(0,1),random.randint(-1,1)]

player = (Paddle(1100, 300, 10, 30, (1100, 300, 10, 30)))
enemy = Paddle(100, 300, 10, 30, (100, 30, 10, 30))
ball = (Ball.coordinateX, Ball.coordinateY, 15)

以及以后:

ballRect = pygame.rect.Rect(Ball.coordinateX, Ball.coordinateY, 10, 10)
if pygame.sprite.collide_rect(player, ballRect):
    bounce()
python pygame
2个回答
0
投票

根据documentationpygame.sprite.collide_rect()用于检测两个sprites之间的碰撞,但是您正在传递pygame.rect.Rect()对象和Paddle对象。

因此,您需要做的是使用pygame.sprite.Sprite创建两个对象,然后如果要使用pygame.sprite.collide_rect(),则检测它们之间的冲突>


0
投票

作为模块名称'sprite'的提示,函数pygame.sprite.collide_rect和所有其他冲突检测函数期望将Sprite实例作为参数。

© www.soinside.com 2019 - 2024. All rights reserved.