[英]Pygame: Showing sprite without a group
I have two sprites in a class for easier control (specifically: tank turret and suspension). 我在一个班上有两个精灵,以便于控制(特别是:坦克炮塔和悬架)。 If I try to launch program it works without any errors, but it doesn't show anything.
如果我尝试启动程序,它将正常运行,但不会显示任何错误。 I also tried to put both of sprites in group in class, but it threw error
我也试图将两个精灵放在班级里,但是抛出了错误
TypeError: draw() missing 1 required positional argument: 'surface'
The code is: 代码是:
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
For adding class I added 'self.grup(self.bokstelis,self.pagrindas)' in __init__
, and changed self.bokstelis.update()
and self.pagrindas.update()
with 为了添加类,我在
__init__
添加了“ self.grup(self.bokstelis,self.pagrindas)”,并使用以下命令更改了self.bokstelis.update()
和self.pagrindas.update()
self.grup.clear()
self.grup.update()
self.grup.draw()
Full eror mesage: 完整的错误讯息:
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'
What should I do for the displaying of my tank without disabling of my class? 在不停用班级的情况下,我该怎么做才能展示我的战车?
pygame.sprite.Group.draw()
requires a non optional argument 'surface'. pygame.sprite.Group.draw()
需要一个非可选参数'surface'。 If you're bliting straight to the screen ( screen = pygame.display.set_mode()
) you do: self.grup.draw(screen)
Your other alternative is to make a surface and blit that to the screen: 如果您是直接向屏幕上出血(
screen = pygame.display.set_mode()
),则可以执行以下操作: self.grup.draw(screen)
您的另一种选择是制作一个表面并将其上光到屏幕上:
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()
but this is the more complicated of the two. 但这是两者中比较复杂的。 If you want it straight from the docs it can be found here: https://www.pygame.org/docs/ref/sprite.html#pygame.sprite.Group.draw
如果您想直接从文档中获取它,可以在以下位置找到: https : //www.pygame.org/docs/ref/sprite.html#pygame.sprite.Group.draw
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.