User Tools

Site Tools


pygame_framework

Differences

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

Link to this comparison view

Next revision
Previous revision
pygame_framework [2024/09/11 06:43] – created appledogpygame_framework [2024/09/22 05:38] (current) appledog
Line 96: Line 96:
 import pygame import pygame
 import time import time
 +from Screen import *
  
 class Game: class Game:
Line 223: Line 224:
         VGA_WHITE  # 15 (F)         VGA_WHITE  # 15 (F)
     ]     ]
 +</Code>
 +
 +== Sound.py
 +If you need it, and you have sound files, you can do something like this:
 +
 +<Code:Python>
 +import pygame as a
 +
 +class Sound:
 +    def __init__(self):
 +        a.mixer.init()
 +        dir = 'assets/sound/'
 +        self.sounds = {}
 +
 +        #self.sounds["laser"] = a.mixer.Sound(dir+'laser1.wav')
 +        #self.sounds["laser"].set_volume(0.15)
 +        #self.sounds["boom"] = a.mixer.Sound(dir+'boom6.wav')
 +        #self.sounds["boom7"] = a.mixer.Sound(dir+'boom7.wav')
 +        #self.sounds["start"] = a.mixer.Sound(dir+'start4.wav')
 +        #self.sounds["pellet"] = a.mixer.Sound(dir+'pellet.wav')
 +
 +    def play(self, sound):
 +        if sound in self.sounds:
 +            self.sounds[sound].play()
 +        else:
 +            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.1726037031.txt.gz · Last modified: 2024/09/11 06:43 by appledog

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki