1

Im new to python and im currently making a simple pygame but I can not grasp the concept of sprites. I want to replace the moving block with a player icon that I created.

Do I need to make a group just for one object or is there a way just to add the player without any groups?

from pygame import sprite
import pygame, sys
from pygame.locals import*
from pynput.keyboard import Key, Controller

class Player(pygame.sprite.Sprite):
    def __init__(self, picture_path):
        super().__init__()
        self.image = pygame.image.load(picture_path)
        self.rect = self.image.get_rect()
    

def main():
    pygame.init()
    DISPLAY=pygame.display.set_mode((1280,720),0,32)
    pygame.display.set_caption('Der Sammler')

    WHITE=(255,255,255)
    BLUE=(0,0,255)
    DISPLAY.fill(WHITE)

    x = 0
    y = 0
    velocity1 = 0
    acceleration = 0.1
    velocity = pygame.Vector2()
    velocity.xy = 3, 0

    player = Player("player.png")
    
    while True:
        pygame.draw.rect(DISPLAY, BLUE, (x,y,50,50))
        x += velocity.x
        if x+ 50 > DISPLAY.get_width():
            velocity.x = -3
        if x < 0:
            velocity.x = 3
        pressed_keys = pygame.key.get_pressed()
        for event in pygame.event.get():
            if event.type==QUIT:
                pygame.quit()
                sys.exit()
        DISPLAY.fill((255, 255, 255))
        pygame.draw.rect(DISPLAY,BLUE, (x,y, 50, 50))
        y += velocity1
        velocity1 += acceleration
        if pressed_keys[pygame.K_SPACE]:
            velocity1 = -3
        pygame.display.update()
        pygame.time.delay(10)
main()
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
mane004
  • 11
  • 2

1 Answers1

0

Add an update method to Player and put the movement and gravity algorithm in that method:

class Player(pygame.sprite.Sprite):
    def __init__(self, picture_path):
        super().__init__()
        self.image = pygame.image.load(picture_path)
        self.rect = self.image.get_rect()
        self.velocity = pygame.Vector2(3, 0)
        self.acceleration = 0.1

    def update(self, dispaly):
        pressed_keys = pygame.key.get_pressed()
        self.rect.x += self.velocity.x
        if self.rect.right >= dispaly.get_width():
            self.velocity.x = -3
        if self.rect.left < 0:
            self.velocity.x = 3
        self.rect.y += self.velocity.y
        self.velocity.y += self.acceleration
        if pressed_keys[pygame.K_SPACE]:
            self.velocity.y = -3

Add the player to a pygame.sprite.Group:

sprites = pygame.sprite.Group(player)

Call sprites.update() and sprites.draw() in the application loop to update the position of the player and to move the player:

while True:
    for event in pygame.event.get():
        if event.type==QUIT:
            pygame.quit()
            sys.exit()
    sprites.update(DISPLAY)
    DISPLAY.fill((255, 255, 255))
    sprites.draw(DISPLAY)
    pygame.display.update()

Also see How can I add objects to a "pygame.sprite.Group()"?.


Complete example:

import pygame, sys
from pygame.locals import*

class Player(pygame.sprite.Sprite):
    def __init__(self, picture_path):
        super().__init__()
        self.image = pygame.image.load(picture_path)
        self.rect = self.image.get_rect()
        self.velocity = pygame.Vector2(3, 0)
        self.acceleration = 0.1

    def update(self, dispaly):
        pressed_keys = pygame.key.get_pressed()
        self.rect.x += self.velocity.x
        if self.rect.right >= dispaly.get_width():
            self.velocity.x = -3
        if self.rect.left < 0:
            self.velocity.x = 3
        self.rect.y += self.velocity.y
        self.velocity.y += self.acceleration
        if pressed_keys[pygame.K_SPACE]:
            self.velocity.y = -3
        if self.rect.bottom > dispaly.get_height():
            self.rect.bottom = dispaly.get_height()
            self.velocity.y = 0
   
def main():
    pygame.init()
    DISPLAY=pygame.display.set_mode((1280,720),0,32)
    pygame.display.set_caption('Der Sammler')
    clock = pygame.time.Clock()

    WHITE=(255,255,255)
    BLUE=(0,0,255)
    DISPLAY.fill(WHITE)

    player = Player("player.png")
    sprites = pygame.sprite.Group(player)
    
    while True:
        for event in pygame.event.get():
            if event.type==QUIT:
                pygame.quit()
                sys.exit()
        sprites.update(DISPLAY)
        DISPLAY.fill((255, 255, 255))
        sprites.draw(DISPLAY)
        pygame.display.update()
        clock.tick(100)

main()
Rabbid76
  • 202,892
  • 27
  • 131
  • 174