Python 参数1必须是pygame.Surface,而不是str
我有大量不相关的代码,所以在注释掉剩下的不重要的花絮之后,我将尝试跳到重要的部分:Python 参数1必须是pygame.Surface,而不是str,python,python-3.x,pygame,typeerror,pygame-surface,Python,Python 3.x,Pygame,Typeerror,Pygame Surface,我有大量不相关的代码,所以在注释掉剩下的不重要的花絮之后,我将尝试跳到重要的部分: import pygame from pygame.locals import * from characters import Character class Game(object): def __init__(self): self.__running = True self.__display = None self.size = self.width, self.heigh
import pygame
from pygame.locals import *
from characters import Character
class Game(object):
def __init__(self):
self.__running = True
self.__display = None
self.size = self.width, self.height = 640, 480
def on_init(self):
pygame.init()
self.clock = pygame.time.Clock()
self.__display = pygame.display.set_mode(self.size, pygame.HWSURFACE |
pygame.DOUBLEBUF)
pygame.mouse.set_visible(False)
self.hero = Character()
self.hrgrp = pygame.sprite.Group(self.hero)
self.__running = True
def on_render(self):
self.hrgrp.draw(self.__display)
pygame.display.update()
def on_cleanup(self):
pygame.quit()
def on_execute(self):
if self.on_init() == False:
self.__running = False
while(self.__running):
self.on_render()
self.on_cleanup()
if __name__ = "__main__":
game = Game()
game.on_execute()
如果您想知道:
self.hero = Character()
只包含
Class Character(pygame.sprite.Sprite):
def __init__(self):
pygame.image.load("path.png")
self.rect = self.sprite.get_rect()
返回的错误为:
File "/home/thegilberts/Documents/Python/game/game/game.py", line 70, in on_render
self.hrgrp.draw(self.__display)
File "/usr/local/lib/python3.5/dist-packages/pygame-1.9.4.dev0-py3.5-linux-x86_64.egg/pygame/sprite.py", line 475, in draw
self.spritedict[spr] = surface_blit(spr.image, spr.rect)
TypeError: argument 1 must be pygame.Surface, not str
我不知道为什么。它绝对是一个曲面,而不是一根弦。事实上,我使用self.\u在许多其他地方显示来创建菜单和东西,但它不允许我绘制这个精灵组。我找到了它。在Character()类中,self.image必须是pygame.image.load()文件,这一点非常重要 我发现我在发布的Character()类中缺少了一些代码,因为我认为它并不重要。 如果其他任何人遇到此错误,则错误代码为:
Class Character(pygame.sprite.Sprite)
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = "image.png"
self.sprite = pygame.image.load(self.image)
self.rect = self.sprite.get_rect()
为了纠正这个问题,我在所有情况下都将self.image与self.sprite进行了交换,效果很好。您在这里剪得太多了。返回错误的是什么?请显示完整的回溯。对不起,我已经更新了。我以为我复制了整个错误。基本上,模块>on_执行>on_渲染>self.hrgrp.draw(self.\u显示)