User Tools

Site Tools


pygame_framework

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
pygame_framework [2024/09/22 05:35] appledogpygame_framework [2024/09/22 05:38] (current) appledog
Line 250: Line 250:
         else:         else:
             print(f"There is no {sound} sound!")             print(f"There is no {sound} sound!")
 +</Code>
 +
 +== Sprite Objects
 +If you need it, you can try this:
 +
 +=== A basic square
 +<Code:Python>
 +import pygame
 +import time
 +from Color import *
 +from Screen import *
 +
 +# Define the Player Sprite class
 +class Player(pygame.sprite.Sprite):
 +    SPEED = 5
 +    SIZE = 20
 +    COLOR = Color.WHITE
 +
 +    def __init__(self):
 +        super().__init__()
 +
 +        # Create a surface for the Player
 +        self.image = pygame.Surface((self.SIZE, self.SIZE))
 +
 +        # Fill the surface with a color
 +        self.image.fill(self.COLOR)
 +
 +        # Get the rectangle for positioning
 +        self.rect = self.image.get_rect()
 +
 +        # Set the initial position
 +        self.rect.center = (Screen.WIDTH // 2, Screen.HEIGHT // 2)
 +
 +    def update(self):
 +        # Restrict positioning to screen.
 +        if self.rect.x < 0:
 +            self.rect.x = 0
 +        if self.rect.x > Screen.WIDTH - self.SIZE:
 +            self.rect.x = Screen.WIDTH - self.SIZE
 +        if self.rect.y < 0:
 +            self.rect.y = 0
 +        if self.rect.y > Screen.HEIGHT - self.SIZE:
 +            self.rect.y = Screen.HEIGHT - self.SIZE
 +
 +        # Easier to read, but, slower.
 +        #self.rect.x = max(0, min(self.rect.x, Screen.WIDTH - self.SIZE))
 +        #self.rect.y = max(0, min(self.rect.y, Screen.HEIGHT - self.SIZE))
 </Code> </Code>
pygame_framework.1726983351.txt.gz · Last modified: 2024/09/22 05:35 by appledog

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki