Spaces:
Runtime error
Runtime error
from random import randint | |
import pygame | |
import numpy as np | |
import streamlit as st | |
import os | |
import time | |
os.system("pip install -r requirements.txt") | |
os.system("pip install --upgrade pip") | |
BG_COLOR = (0, 0, 0) | |
fullscreen = True | |
logo = pygame.image.load('dvd_logo_0002.png') | |
logo = pygame.transform.scale(logo, (50, 100)) | |
logo = pygame.surfarray.array3d(logo) | |
logo = np.flip(logo, axis=1) | |
logo = pygame.surfarray.make_surface(logo) | |
img_size = logo.get_rect().size | |
screen_width = 850 | |
screen_height = 540 | |
screen = pygame.Surface((screen_height, screen_width)) | |
screen.fill((0, 0, 0)) | |
x = randint(0, screen_width - img_size[0]) | |
y = randint(0, screen_height - img_size[1]) | |
x_speed = 2 | |
y_speed = 3 | |
np_logo = pygame.surfarray.array3d(logo) | |
# Inverse the colors so that the background of the DVD logo is black and the letters are white | |
np_logo = np.where(np_logo >= 200, 0, np_logo+200) | |
dvd_letters_color = [255, 255, 255] | |
dvd_background_color = [0, 0, 0] | |
logo = pygame.surfarray.make_surface(np_logo) | |
def random_color_change(surface, color_to_be_changed): | |
np_surface = pygame.surfarray.array3d(surface) | |
for channel in range(np_surface.shape[-1]): | |
random_channel_color = randint(30, 240) | |
np_surface[:, :, channel] = np.where( | |
np.logical_and(np_surface[:, :, channel] >= color_to_be_changed[channel]-20, | |
np_surface[:, :, channel] <= color_to_be_changed[channel]), | |
random_channel_color, np_surface[:, :, channel]) | |
color_to_be_changed[channel] = random_channel_color | |
return pygame.surfarray.make_surface(np_surface), color_to_be_changed | |
st_display_img = st.empty() | |
while True: | |
screen.fill(BG_COLOR) | |
if ((x + img_size[1] + x_speed >= screen_width) or (x + x_speed <= 0)) and \ | |
((y + y_speed + img_size[0] >= screen_height) or (y + y_speed <= 0)): | |
x -= int(4.5 * x_speed + 7) | |
y -= int(3.5 * y_speed + 7) | |
if (x + img_size[0] >= screen_height) or (x <= 0): | |
logo, dvd_letters_color = random_color_change(logo, dvd_letters_color) | |
x_speed = -x_speed | |
if (y + img_size[1] >= screen_width) or (y <= 0): | |
logo, dvd_letters_color = random_color_change(logo, dvd_letters_color) | |
y_speed = -y_speed | |
x += x_speed | |
x = np.clip(x, 0, screen_height - img_size[0]) | |
y += y_speed | |
y = np.clip(y, 0, screen_width - img_size[1]) | |
screen.blit(logo, (x, y)) | |
screen_np = pygame.surfarray.array3d(screen) | |
st_display_img.image(screen_np, caption='Idle screen bouncing DVD logo') | |
time.sleep(0.03) | |