'Trying to make images as sprites in pygame

Why isn't this code working? I'm trying to create a class to quickly create players for my game in pygame. I was trying to create a sprite based on what he did in this video, https://www.youtube.com/watch?v=hDu8mcAlY4E, and his seemed to work fine.

class Player(Sprite):
    
    def __init__(self,x,y,picture_path,width,height):
      super().__init__()
      self.image = pygame.image.load(picture_path)
      self.rect = self.image.get_rect()
    
    def move(self):
        self.x += self.dx
        self.y += self.dy
        self.dy += GRAVITY
        
    def jump(self):
        self.dy -= 15
        
    def left(self):
        self.dx -= 6
        if self.dx < -12:
          self.dx = -12
        
    def right(self):
        self.dx = 6
        if self.dx > 12:
            self.dx = 12



player = Player(600,0,'nin.png',20,20)
player2 = Player(600,40,'nin.png',20,20)

If you want the error for this here it is;

Traceback (most recent call last):
  File "main.py", line 107, in <module>
    player = Player(600,0,'nin.png',20,20)
  File "main.py", line 77, in __init__
    super().__init__()
TypeError: __init__() missing 4 required positional arguments: 'x', 'y', 'width', and 'height'
 Traceback (most recent call last):
  File "main.py", line 107, in <module>
    player = Player(600,0,'nin.png',20,20)
  File "main.py", line 77, in __init__
    super().__init__()
TypeError: __init__() missing 4 required positional arguments: 'x', 'y', 'width', and 'height'
 


Solution 1:[1]

The base class must be pygame.sprite.Sprite instead of your own Sprite class.

Pygame uses pygame.sprite.Sprite objects and pygame.sprite.Group objects to manage Sprites. pygame.sprite.Group.draw() and pygame.sprite.Group.update() are methods which are provided by pygame.sprite.Group.

The former delegates the to the update method of the contained pygame.sprite.Sprites - you have to implement the method. See pygame.sprite.Group.update():

Calls the update() method on all Sprites in the Group [...]

The later uses the image and rect attributes of the contained pygame.sprite.Sprites to draw the objects - you have to ensure that the pygame.sprite.Sprites have the required attributes. See pygame.sprite.Group.draw():

Draws the contained Sprites to the Surface argument. This uses the Sprite.image attribute for the source surface, and Sprite.rect. [...]

See the very basic example:

repl.it/@Rabbid76/PyGame-Sprite

import pygame

pygame.init()
window = pygame.display.set_mode((400, 400))
clock = pygame.time.Clock()

class Player(pygame.sprite.Sprite):
    
    def __init__(self, center_pos, image):
        super().__init__() 
        self.image = image
        self.rect = self.image.get_rect(center = center_pos)
    
    def update(self, surf):
        keys = pygame.key.get_pressed()
        self.rect.x += (keys[pygame.K_d]-keys[pygame.K_a]) * 5
        self.rect.y += (keys[pygame.K_s]-keys[pygame.K_w]) * 5
        self.rect.clamp_ip(surf.get_rect())

player_surf = pygame.image.load('Bird64.png').convert_alpha()
player = Player(window.get_rect().center, player_surf)
all_sprites = pygame.sprite.Group([player])

run = True
while run:
    clock.tick(60)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    all_sprites.update(window)

    window.fill(0)
    all_sprites.draw(window)
    pygame.display.flip()

pygame.quit()
exit()

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1