pygame-dvd/dvd_bounce/surfaces/dvd_screen.py

77 lines
2.5 KiB
Python
Raw Normal View History

2023-02-28 09:50:34 -05:00
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)