AttributeError:'pygame.Rect'对象在执行冲突时没有属性'Rect'错误

2024-09-28 21:50:57 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

Tags: rectselfrandomwidthpygameclassplayerheight
3条回答

正如模块名“sprite”所提示的那样,函数pygame.sprite.collide_rect和所有其他冲突检测函数期望Sprite实例作为参数

请看documentation

pygame.sprite.collide_rect()
Collision detection between two sprites, using rects.
collide_rect(left, right) -> bool

Tests for collision between two sprites. Uses the pygame rect colliderect function to calculate the collision. Intended to be passed as a collided callback function to the *collide functions. Sprites must have a "rect" attributes.

(另外,通常不直接使用此函数,而是与^{}一起使用)

因此,要么开始使用Sprite类(这是您应该做的),要么使用Rect类提供的函数,如^{}

colliderect()
test if two rectangles overlap
colliderect(Rect) -> bool

Returns true if any portion of either rectangle overlap (except the top+bottom or left+right edges).

首先,当为球创建rect属性时,它应该是self.rect = pygame.Rect(x, y, width, height)(假设您使用import pygame导入Pygame)。这应该可以处理你的AttributeError。你的ballRectballRect = pygame.Rect(Ball.coordinateX, Ball.coordinateY, 10, 10)也是如此

接下来,您使用了错误的rect碰撞检测功能pygame.sprite指的是Pygame中完全不同的东西(它的Sprite对象),并且您的BallPaddle类都不是您编写的代码中的Pygame精灵。因此,在这里调用pygame.sprite.collide_rect()没有任何意义

相反,您应该简单地使用rect.colliderect,如下所示:

if rect.colliderect(other_rect):
    # Colliding!
    print("Stop touching me!")

或者在您的情况下:

if player.rect.colliderect(ballRect):
    bounce()

此外,您不需要将rect作为参数传递给Paddle初始化,因为它从未被使用过。对象的rect完全由其他参数生成

最后,您的Ball类和实例化代码似乎有点不对劲。您应该像对Paddle一样生成并初始化它

编辑最后一件事。在pygame中,当您创建一个具有rect属性的对象时,您不再需要为该对象分配xy值,因为无论何时需要它们,例如在blit中,您都可以直接传递rect(或者在某些边缘情况下,rect.xrect.y作为位置参数,pygame将很好地处理其余的问题

下面是您的代码应该是什么样子(还有一些其他问题需要解决):

class Paddle():
    def __init__(self, x, y, width, height):
        self.width = width
        self.height = height
        self.rect = pygame.Rect(x, y, width, height)

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

player = Paddle(1100, 300, 10, 30)
enemy = Paddle(100, 300, 10, 30)
ball = Ball(600, 300, 10, 10)

# Other stuff

# Collision checking
if player.rect.colliderect(ball.rect):
    bounce()

根据documentationpygame.sprite.collide_rect()用于检测两个精灵之间的碰撞,但是您正在通过一个pygame.rect.Rect()对象和一个拨片对象

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

相关问题 更多 >