pygame_framework
                Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| pygame_framework [2024/09/18 08:45] – appledog | pygame_framework [2024/09/22 05:38] (current) – appledog | ||
|---|---|---|---|
| Line 224: | Line 224: | ||
| VGA_WHITE | VGA_WHITE | ||
| ] | ] | ||
| + | </ | ||
| + | |||
| + | == Sound.py | ||
| + | If you need it, and you have sound files, you can do something like this: | ||
| + | |||
| + | < | ||
| + | import pygame as a | ||
| + | |||
| + | class Sound: | ||
| + | def __init__(self): | ||
| + | a.mixer.init() | ||
| + | dir = ' | ||
| + | self.sounds = {} | ||
| + | |||
| + | # | ||
| + | # | ||
| + | # | ||
| + | # | ||
| + | # | ||
| + | # | ||
| + | |||
| + | def play(self, sound): | ||
| + | if sound in self.sounds: | ||
| + | self.sounds[sound].play() | ||
| + | else: | ||
| + | print(f" | ||
| + | </ | ||
| + | |||
| + | == Sprite Objects | ||
| + | If you need it, you can try this: | ||
| + | |||
| + | === A basic square | ||
| + | < | ||
| + | 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, | ||
| + | |||
| + | # 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. | ||
| + | # | ||
| + | # | ||
| </ | </ | ||
pygame_framework.1726649134.txt.gz · Last modified: 2024/09/18 08:45 by appledog
                
                