0

I have been working with pygame recently. I started putting my sprites in a class now but I have encountered a problem. I put my character tank in the init module. Then added my class Player to a GroupSingle (called player). But when trying to draw the group i encounter this problem:

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

This is my code:

import pygame

pygame.init()

class Player:
    def __init__(self):
        super().__init__()
        #create sprite
        self.tank = pygame.image.load("Graphics/Tank.jpg").convert_alpha()
        self.rect = self.tank.get_rect(midbottom = (200, 300))

screen = pygame.display.set_mode((800, 400))

clock = pygame.time.Clock()

#make group
player = pygame.sprite.GroupSingle
player.add(Player())

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            exit()

    screen.fill("black")

    #draw group
    player.draw(screen)

    pygame.display.update()
    clock.tick(60)

I have no idea what I might be doing wrong. This might turn turn out to be a very stupid mistake but I am still a beginner.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • Typo: You have missed the parenthesisis: **`player = pygame.sprite.GroupSingle()`** instead of `player = pygame.sprite.GroupSingle` – Rabbid76 Aug 19 '23 at 14:12
  • The name of the image attribute must be `image` and must not be `tank` and `Player` must be derived from `pygame.sprite.Sprite`: **`class Player(pygame.sprite.Sprite):`**. See [What does pygame.sprite.Group() do](https://stackoverflow.com/questions/68765971/what-does-pygame-sprite-group-do) or [How can I add objects to a "pygame.sprite.Group()"?](https://stackoverflow.com/questions/73924256/how-can-i-add-objects-to-a-pygame-sprite-group) – Rabbid76 Aug 19 '23 at 14:16
  • Thank you so much. This worked. But why should image attribute be only name `image`? – Captain_Wiz08 Aug 19 '23 at 14:52
  • Why do you not read the answer to the linked question [What does pygame.sprite.Group() do](https://stackoverflow.com/questions/68765971/what-does-pygame-sprite-group-do) or at least the documentation https://www.pygame.org/docs/ref/sprite.html#pygame.sprite.Group.draw? – Rabbid76 Aug 19 '23 at 14:54

0 Answers0