Screensaver/screensaver.py

150 lines
4.4 KiB
Python
Raw Normal View History

2023-07-19 14:41:48 +02:00
#!/usr/bin/python3
import os
import math
import random
import pygame
pygame.init()
screen = pygame.display.set_mode((1000, 600), pygame.RESIZABLE)
pygame.display.set_caption("Screensaver")
gray = (20, 20, 20)
red = (255, 20, 20)
2023-07-19 15:41:22 +02:00
working_directory = os.path.dirname(os.path.realpath(__file__))
file_path = os.path.abspath(__file__)
2023-07-19 14:41:48 +02:00
wobblers = []
2023-07-19 16:19:11 +02:00
max_wobbler_size = 100
2023-07-19 14:41:48 +02:00
2023-07-19 15:41:22 +02:00
images = []
for image in os.listdir(f"{working_directory}/textures"):
images.append(pygame.image.load(f"{working_directory}/textures/{image}"))
2023-07-19 14:41:48 +02:00
clock = pygame.time.Clock()
running = True
class Wobbler:
def __init__(self, position: tuple=(100, 100), rotation: int=0):
self.size = 1
self.x, self.y = position
self.color = red
self.rotation = rotation
self.speed = 4
self.age = 0
2023-07-19 16:19:11 +02:00
self.image_original = random.choice(images)
2023-07-19 17:24:45 +02:00
self.divider = 1
self.max_width, self.max_height = self.image_original.get_size()
2023-07-19 17:24:45 +02:00
while self.max_width > max_wobbler_size * 2 or self.max_height > max_wobbler_size * 2:
self.divider += 0.01
self.max_width = self.image_original.get_width() / self.divider
self.max_height = self.image_original.get_height() / self.divider
2023-07-19 17:24:45 +02:00
self.width = self.max_width * self.size / 100 / self.divider
self.height = self.max_height * self.size / 100 / self.divider
2023-07-19 16:19:11 +02:00
self.image = self.image_original
self.image = pygame.transform.scale(self.image, (self.width, self.height))
self.image_no_rot = self.image
# self.image = pygame.transform.rotate(self.image, self.rotation)
2023-07-19 16:19:11 +02:00
self.image_rect = self.image.get_rect(center=self.image_no_rot.get_rect(center=(self.x, self.y)).center)
2023-07-19 14:41:48 +02:00
def draw(self):
2023-07-19 16:19:11 +02:00
screen.blit(self.image, self.image_rect)
2023-07-19 14:41:48 +02:00
def update(self):
2023-07-19 17:24:45 +02:00
self.width = self.max_width * self.size / 100 / self.divider
self.height = self.max_height * self.size / 100 / self.divider
2023-07-19 14:41:48 +02:00
2023-07-19 16:19:11 +02:00
self.image = self.image_original
self.image = pygame.transform.scale(self.image, (self.width, self.height))
self.image_no_rot = self.image
# self.image = pygame.transform.rotate(self.image, self.rotation)
2023-07-19 16:19:11 +02:00
self.image_rect = self.image.get_rect(center=self.image_no_rot.get_rect(center=(self.x, self.y)).center)
2023-07-19 14:41:48 +02:00
def move(self):
x_offset, y_offset = deg_to_offset(self.rotation, self.speed)
self.x += x_offset
self.y += y_offset
def deg_to_offset(degrees, offset):
radians = math.radians(degrees)
x = math.sin(radians) * offset
y = math.cos(radians) * offset
return x, y
2023-07-19 14:43:08 +02:00
2023-07-19 14:41:48 +02:00
def update():
screen.fill(gray)
for event in pygame.event.get():
if event.type == pygame.QUIT:
global running
pygame.quit()
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_w:
print(wobblers)
print(len(wobblers))
if running:
2023-07-19 16:36:58 +02:00
new_wobbler = random.randrange(1, 61) == 1
2023-07-19 14:41:48 +02:00
if new_wobbler:
x = random.randrange(0, screen.get_width() + 1)
y = random.randrange(0, screen.get_height() + 1)
rot = random.randrange(0, 360)
wobblers.append(Wobbler((x, y), rot))
for wobbler in wobblers:
if not wobbler.x < 0 or not wobbler.x > screen.get_width() or not wobbler.y < 0 or not wobbler.y > screen.get_height():
wobbler.move()
wobbler.update()
wobbler.draw()
if wobbler.speed > 1:
decrease_speed = random.randrange(1, 101 - wobbler.speed) == 1
if decrease_speed:
wobbler.speed -= 1
change_rot = random.randrange(1, 101 - wobbler.speed) == 1
if change_rot:
wobbler.rotation = random.randrange(0, 360)
wobbler.speed = 4
2023-07-19 14:41:48 +02:00
if wobbler.age < max_wobbler_size:
wobbler.size += 1
wobbler.age += 1
if wobbler.age >= 600:
2023-07-19 14:41:48 +02:00
wobbler.size -= 1
if wobbler.size == 0:
wobblers.remove(wobbler)
if wobbler.x < -5 or wobbler.x > screen.get_width() + 5 or wobbler.y < -5 or wobbler.y > screen.get_height() + 5:
wobbler.rotation -= 180
wobbler.speed = 4
2023-07-19 14:41:48 +02:00
pygame.display.update()
clock.tick(60)
while running:
update()