Traceback (most recent call last): File "C:/Users/25604/PycharmProjects/飞机大战/game.py", line 48, in <module> main() File "C:/Users/25604/PycharmProjects/飞机大战/game.py", line 43, in main a.show() File "C:/Users/25604/PycharmProjects/飞机大战/game.py", line 33, in show bg.draw() File "C:/Users/25604/PycharmProjects/飞机大战/game.py", line 16, in draw self.screen.blit(self.image, (self.x, self.y)) TypeError: argument 1 must be pygame.surface.Surface, not str
时间: 2023-06-26 22:07:09 浏览: 176
这个错误提示是因为在bg.draw()方法中,self.image的类型应该是pygame.surface.Surface,但是实际上传入的参数是str类型。你需要检查一下代码,找出导致这个问题的原因。可能是在定义bg对象时,没有正确地将图片加载到self.image中,或者是在调用draw()时,传入了错误的参数。你可以参考以下代码,确保正确加载图片并传入正确的参数:
```
class Background(pygame.sprite.Sprite):
def __init__(self, image_path, location):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load(image_path)
self.rect = self.image.get_rect()
self.rect.left, self.rect.top = location
self.speed = 1
def move(self):
self.rect.top += self.speed
def draw(self, screen):
screen.blit(self.image, self.rect)
```
在这个代码中,我们通过pygame.image.load()方法来加载图片,并将其赋值给self.image。然后在draw()方法中,我们通过screen.blit()方法将图片绘制到屏幕上。确保你的代码中也是按照这个方式加载图片并绘制的。
阅读全文