made a bouncing DVD logo

This commit is contained in:
Nicholas Dyer 2023-02-28 09:50:34 -05:00
parent 64ec4adbe7
commit 01d2ceee9e
7 changed files with 138 additions and 0 deletions

3
dvd_bounce/__init__.py Normal file
View File

@ -0,0 +1,3 @@
__version__ = "0.0.1"
from . import main

14
dvd_bounce/__main__.py Normal file
View File

@ -0,0 +1,14 @@
import dvd_bounce
import sys
import os
SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
if __name__ == '__main__':
windowed_mode = False
if len(sys.argv) > 0:
for arg in sys.argv:
if arg == '--window':
windowed_mode = True
dvd_bounce.main.main(windowed_mode)

43
dvd_bounce/main.py Normal file
View File

@ -0,0 +1,43 @@
import pygame
import dvd_bounce.surfaces as surfaces
# Global variables
SCREEN_SIZE = (720, 720)
FPS = 60
VALID_SURFACES = [
'dvd_screen'
]
def main(windowed_mode=False):
"""
Main scene manager to display the scenes of the application
:param windowed_mode: boolean to start the game in windowed mode
"""
pygame.init()
clock = pygame.time.Clock()
if windowed_mode:
window = pygame.display.set_mode(SCREEN_SIZE)
else:
window = pygame.display.set_mode(SCREEN_SIZE, pygame.FULLSCREEN)
# Starts the program with the surface 'dial' as the default
surface = getattr(globals()['surfaces'], 'dvd_screen').Surface(SCREEN_SIZE)
running = True
while running:
clock.tick(FPS)
surface.update()
window.blit(surface, surface.get_rect())
if not surface.running:
if surface.quit:
break
# Switch the surface to a new surface
next_surface = surface.next_surface
if next_surface not in VALID_SURFACES:
raise Exception('Given surface is not a valid surface!')
surface = getattr(globals()['surfaces'], next_surface).Surface(SCREEN_SIZE)
pygame.display.flip()
pygame.quit()

View File

@ -0,0 +1 @@
from . import dvd_screen

View File

@ -0,0 +1,76 @@
import pygame
import os
import random
SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
LOGO_SCALING = 0.1
class DVDLogo(pygame.sprite.Sprite):
def __init__(self, window_size):
super().__init__()
self.base_image = pygame.image.load(SCRIPT_DIR + '/resources/dvd.png')
self.base_image.convert_alpha()
self.base_image = pygame.transform.scale(self.base_image, (self.base_image.get_width() * LOGO_SCALING,
self.base_image.get_height() * LOGO_SCALING))
self.image = self.base_image.copy()
color_surface = pygame.Surface(self.image.get_size(), pygame.SRCALPHA)
color_surface.fill((64, 64, 255))
self.image.blit(color_surface, (0, 0), special_flags=pygame.BLEND_RGBA_MULT)
self.rect = self.image.get_rect()
self.x_speed = 4
self.y_speed = 4
self.max_x = window_size[0] - self.rect.width
self.max_y = window_size[1] - self.rect.height
def random_color(self):
color_surface = pygame.Surface(self.image.get_size(), pygame.SRCALPHA)
new_color = (random.randint(0, 255),
random.randint(0, 255),
random.randint(0, 255))
color_surface.fill(new_color)
self.image = self.base_image.copy()
self.image.blit(color_surface, (0, 0), special_flags=pygame.BLEND_RGBA_MULT)
def update(self):
self.rect.x += self.x_speed
self.rect.y += self.y_speed
if self.rect.x < 0 or self.rect.x > self.max_x:
self.x_speed *= -1
self.random_color()
if self.rect.y < 0 or self.rect.y > self.max_y:
self.y_speed *= -1
self.random_color()
self.rect.x = max(0, min(self.rect.x, self.max_x))
self.rect.y = max(0, min(self.rect.y, self.max_y))
class Surface(pygame.Surface):
def __init__(self, window_size):
super().__init__(window_size, pygame.SRCALPHA)
self.running = True
self.quit = False
self.next_surface = ''
pygame.mouse.set_visible(False)
dvd_logo = DVDLogo(window_size)
self.all_sprites = pygame.sprite.Group(dvd_logo)
def update(self):
self.fill(pygame.colordict.THECOLORS.get('black'))
for event in pygame.event.get():
match event.type:
case pygame.MOUSEBUTTONDOWN:
self.running = False
self.quit = True
self.all_sprites.update()
self.all_sprites.draw(self)

Binary file not shown.

After

Width:  |  Height:  |  Size: 63 KiB

1
requirements.txt Normal file
View File

@ -0,0 +1 @@
pygame~=2.1.3