gpt4 book ai didi

python - 属性错误 : 'Enemy' object has no attribute 'blit'

转载 作者:行者123 更新时间:2023-12-04 07:55:00 24 4
gpt4 key购买 nike

我正在制作一个飞船游戏,你可以控制飞船并发射子弹来击败敌人。但是,当我运行我的游戏时,我得到了这个:

Traceback (most recent call last):
File "C:/eqodqfe/t2.py", line 98, in enemies.draw(enemy)
File "C:\Users\linpang2018\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pygame\sprite.py", line 474, in drawsurface_blit = surface.blit
AttributeError: 'Enemy' object has no attribute 'blit'


为什么会发生这种情况?我如何解决它?这是我当前的代码:(有些部分省略或替换为--snip--)
class Spaceship(pygame.sprite.Sprite):
def __init__(self, s, x, y):
pygame.sprite.Sprite.__init__(self)

self.screen = s
self.x, self.y = x, y
self.image = pygame.image.load("C:/eqodqfe/spaceship.png")
self.image = pygame.transform.scale(self.image, (175, 175))
self.rect = self.image.get_rect()
self.rect.center = (self.x, self.y)

def update(self):
self.rect.center = (self.x, self.y)


class Bullet(pygame.sprite.Sprite):
def __init__(self, s, x, y):
pygame.sprite.Sprite.__init__(self)

--snip--

def update(self):
self.y -= 5
self.rect.center = (self.x, self.y)
if self.y < 0:
self.kill()


class Enemy(pygame.sprite.Sprite):
def __init__(self, s, x, y):
pygame.sprite.Sprite.__init__(self)

self.screen, self.x, self.y = s, x ,y
self.image = pygame.image.load("C:/eqodqfe/enemy.png")
self.image = pygame.transform.scale(self.image, (240, 210))
self.rect = self.image.get_rect()
self.rect = self.image.get_rect()
self.rect.center = (self.x, self.y)

def update(self):
self.rect.center = (self.x, self.y)


spaceship = Spaceship(screen, 400, 400)

bullets = pygame.sprite.Group()
enemies = pygame.sprite.Group()

clock = pygame.time.Clock()

running = True
while running:
--snip-- # (event loop and spawning bullets)

bullets.update()

key = pygame.key.get_pressed()
amount = 5

if key[pygame.K_a]:
spaceship.x -= amount
--snip--

spaceship.update()

screen.fill((255, 255, 255))

screen.blit(spaceship.image, spaceship.rect)

enemy = Enemy(screen, randint(-200, 800), -100)
enemies.add(enemy)

bullets.draw(screen)
enemies.draw(enemy)

pygame.display.update()
clock.tick(60)

最佳答案

pygame.sprite.Group() 像这样工作:

Draws the contained Sprites to the Surface argument.


所以线
enemies.draw(enemy) 
应该
enemies.draw(screen)
因为参数必须是 Surface ,不是 Sprite .

关于python - 属性错误 : 'Enemy' object has no attribute 'blit' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66748004/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com