Ignas Kiela
Ignas Kiela

Reputation: 180

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

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?

Upvotes: 1

Views: 172

Answers (1)

user5273198
user5273198

Reputation:

pygame.sprite.Group.draw() requires a non optional argument '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((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

Upvotes: 1

Related Questions