【发布时间】:2023-01-13 05:11:11
【问题描述】:
我的代码复制在下面
class Bullet(pygame.sprite.Sprite):
def __init__(self ,x, y, direction):
pygame.sprite.Sprite.__init__(self)
self.speed = 10
self.image = bullet_img
self.hitbox = self.image.get_rect()
self.hitbox.center = (x,y)
self.direction = direction
def update(self):
#move bullet
self.hitbox.x +=(self.direction*self.speed)
#check if bullet off screen
if self.hitbox.right <0 or self.hitbox.left>SCREEN_WIDTH:
self.kill()
追溯:
文件“C:\Users\bobby\AppData\Local\Programs\Python\Python311\Lib\site-packages\pygame\sprite.py”,第 551 行,在绘图中
zip(sprites, surface.blits((spr.image, spr.rect) for sprite in sprites))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^
文件“C:\Users\bobby\AppData\Local\Programs\Python\Python311\Lib\site-packages\pygame\sprite.py”,第 551 行,在
zip(sprites, surface.blits((spr.image, spr.rect) for sprite in sprites))
^^^^^^^^
AttributeError: 'Bullet' 对象没有属性 'rect'
谢谢你
试图在子弹中生成,但是 pygame 中用于生成其命中框的 get_rect 函数无法正常工作
【问题讨论】: