Untitled

 avatar
unknown
plain_text
9 months ago
1.0 kB
1
Indexable
import pygame

class Player(pygame.sprite.Sprite):
    def __init__(self, image, x, y):
        super().__init__()  # Call the parent class (Sprite) constructor
        self.image = pygame.image.load(image)  # Load the image
        self.rect = self.image.get_rect()  # Get the rectangular area of the image
        self.rect.topleft = (x, y)  # Set the position of the sprite

# Initialize Pygame
pygame.init()
screen = pygame.display.set_mode((800, 600))

# Create a player instance and add it to a sprite group
player = Player('player_img.png', 100, 100)
all_sprites = pygame.sprite.Group()  # Create a group to hold all sprites
all_sprites.add(player)  # Add the player sprite to the group

# Game loop
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    screen.fill((255, 255, 255))  # Fill the screen with white
    all_sprites.draw(screen)  # Draw all sprites in the group
    pygame.display.flip()  # Update the display

pygame.quit()
Editor is loading...
Leave a Comment