Pygame: 显示没有分组的精灵

Pygame: Showing sprite without a group

我在 class 中有两个精灵,以便于控制(特别是:坦克炮塔和悬架)。如果我尝试启动程序,它可以正常运行,但不会显示任何内容。我还尝试将两个精灵放在 class 中,但它抛出了错误

TypeError: draw() missing 1 required positional argument: 'surface'

密码是:

class Bokstelis(pygame.sprite.Sprite):
    def __init__(self,atvaizdas,centerx,centery,sukgreitis):
        pygame.sprite.Sprite.__init__(self)
        self.nuotr=pygame.image.load(atvaizdas)
        self.image=self.nuotr
        self.rect=self.image.get_rect()
        self.rect.centerx=centerx
        self.rect.centery=centery
        self.sukgreitis=sukgreitis
        self.kryptis=0
    def update(self):
        mouseX, mouseY=pygame.mouse.get_pos()
        self.angle = math.degrees(math.atan2(mouseY - self.rect.centery, mouseX - self.rect.centerx))
        orig_rect=self.rect
        if self.angle - self.kryptis <self.sukgreitis:
            self.image=pygame.transform.rotate(self.nuotr,-(self.sukgreitis+self.kryptis))
        elif self.angle - self.kryptis >self.sukgreitis:
            self.image=pygame.transform.rotate(self.nuotr,self.sukgreitis+self.kryptis)
        else:
            self.image=pygame.transform.rotate(self.nuotr,-angle)
        self.rect=self.image.get_rect()
        self.rect.center=orig_rect.center
        self.kryptis=self.angle
class Pagrindas(pygame.sprite.Sprite):
    def __init__(self,atvaizdas,centerx,centery,sukgreitis):
        pygame.sprite.Sprite.__init__(self)
        self.nuotr=pygame.image.load(atvaizdas)
        self.image=self.nuotr
        self.rect=self.image.get_rect()
        self.rect.centerx=centerx
        self.rect.centery=centery
        self.sukgreitis=-sukgreitis
        self.kryptis=0
    def suktis(self):
        orig_rect=self.rect
        self.image=pygame.transform.rotate(self.nuotr,-sukgreitis)
        self.rect=self.image.get_rect()
        self.rect.center=orig_rect.center
        self.kryptis-=kryptis
class Tankas:
    def __init__(self,centerx,centery,bokstelis,pagrindas,maxjudgreit,galingumas,svoris):
        self.bokstelis=bokstelis
        self.pagrindas=pagrindas
        self.centerx=centerx
        self.centery=centery
        self.bokstelis.rect.center=(self.centerx,self.centery)
        self.pagrindas.rect.center=(self.centerx,self.centery)
        self.grup=pygame.sprite.Group(self.bokstelis,self.pagrindas)
        self.maxjudgreit=maxjudgreit
        self.galing=galingumas
        self.svoris=svoris
        self.judejimas=0
        self.kryptis=self.pagrindas.kryptis
        self.greit=False
        self.maxdabgreit=72
    def update(self):
        self.centerx,selfcentery=self.judejimas * math.cos(math.radians(self.kryptis)), self.judejimas * math.sin(math.radians(self.kryptis))
        self.bokstelis.rect.center=(self.centerx,self.centery)
        self.pagrindas.rect.center=(self.centerx,self.centery)
        self.bokstelis.update()
        self.pagrindas.update()
        if self.maxdabgreit < self.judejimas:
            selfjudėjimas-=self.galing/self.svoris
        elif self.greit:
            self.judejimas=self.judejimasself.galing/self.svoris

为了添加 class,我在 __init__ 中添加了 'self.grup(self.bokstelis,self.pagrindas)',并将 self.bokstelis.update()self.pagrindas.update() 更改为

self.grup.clear()
self.grup.update()
self.grup.draw()

完整的错误信息:

Traceback (most recent call last):
  File "C:\Users\Kiela\Dropbox\IK8\IK3\World of Tankz.py", line 76, in <module>
    tankas.grup.draw()
TypeError: draw() missing 1 required positional argument: 'surface'

在不禁用 class 的情况下如何显示我的坦克?

pygame.sprite.Group.draw() 需要一个非可选参数 'surface'。 如果你直接对着屏幕 (screen = pygame.display.set_mode()) 你会这样做:self.grup.draw(screen) 您的另一种选择是制作一个表面并将其 blit 到屏幕:

screen = pygame.display.set_mode((0, 0)) # Create the main window
#Create the sprites and groups etc.
...

surface = pygame.Surface(screen.get_size)
self.grup.draw(surface)
screen.blit(surface)
pygame.display.flip()

但这是两者中较复杂的一个。 如果你想直接从文档中找到它,可以在这里找到:https://www.pygame.org/docs/ref/sprite.html#pygame.sprite.Group.draw