screensaver/main.py

165 lines
5.1 KiB
Python
Raw Normal View History

2024-01-19 22:09:41 +01:00
#!/usr/bin/python3
2024-01-18 21:34:58 +01:00
import pygame
from random import choice
2024-01-18 21:34:58 +01:00
from random import randint as ri
import os
2024-01-19 21:59:16 +01:00
2024-01-18 21:34:58 +01:00
pygame.init()
2024-01-19 16:03:11 +01:00
default_font = pygame.font.SysFont("sans", 14)
2024-01-18 21:34:58 +01:00
2024-01-20 22:39:12 +01:00
def toggle_fullscreen(is_fullscreen):
if is_fullscreen:
pygame.display.set_mode((600,500),pygame.RESIZABLE)
else:
pygame.display.set_mode((0,0),pygame.FULLSCREEN)
is_fullscreen = not is_fullscreen
return is_fullscreen,pygame.display.get_window_size()
2024-01-19 16:03:11 +01:00
is_fullscreen = False
2024-01-18 21:34:58 +01:00
fill_screen = True
2024-01-19 16:03:11 +01:00
show_debug = False
2024-01-21 15:47:15 +01:00
inverse = False
2024-01-20 22:39:12 +01:00
screen = pygame.display.set_mode((600,500))#(0,0),pygame.FULLSCREEN)
is_fullscreen,screensize = toggle_fullscreen(is_fullscreen)
2024-01-18 21:34:58 +01:00
pygame.display.set_caption("Screensaver")
2024-01-19 16:03:11 +01:00
screensize = pygame.display.get_window_size()
2024-01-18 21:34:58 +01:00
clock = pygame.time.Clock()
working_directory = os.path.dirname(os.path.realpath(__file__))
images = []
for image in os.listdir(f"{working_directory}/dvds"):
images.append(pygame.image.load(f"{working_directory}/dvds/{image}"))
2024-01-19 21:59:16 +01:00
scales = 1
speeds = 1
2024-01-18 21:34:58 +01:00
2024-01-19 21:59:16 +01:00
class DVD():
def __init__(self,scale,speed):
2024-01-20 09:48:03 +01:00
self.newcolor()
self.dvd = choice(images)
2024-01-19 21:59:16 +01:00
self.size = self.dvd.get_size()
self.scale = scale
self.speed = speed
self.pos = ri(0,screensize[0]-self.size[0]),ri(0,screensize[1]-self.size[1])
self.get_dvd(inverse)
2024-01-19 21:59:16 +01:00
self.richtung = ["r","u"]
2024-01-18 21:34:58 +01:00
2024-01-19 21:59:16 +01:00
def moving(self):
2024-01-21 15:47:15 +01:00
dvdx,dvdy = self.pos
if dvdx+self.scalesize[0] >= screensize[0]:
2024-01-19 21:59:16 +01:00
self.richtung[0] = "l"
self.newcolor()
2024-01-21 15:47:15 +01:00
if dvdx <= 0:
2024-01-19 21:59:16 +01:00
self.richtung[0] = "r"
self.newcolor()
2024-01-21 15:47:15 +01:00
if dvdy+self.scalesize[1] >= screensize[1]:
2024-01-19 21:59:16 +01:00
self.richtung[1] = "o"
self.newcolor()
2024-01-21 15:47:15 +01:00
if dvdy <= 0:
2024-01-19 21:59:16 +01:00
self.richtung[1] = "u"
self.newcolor()
2024-01-18 21:34:58 +01:00
2024-01-19 21:59:16 +01:00
if self.richtung[0] == "r":
dvdx += self.speed
else:
dvdx -= self.speed
if self.richtung[1] =="u":
dvdy += self.speed
else:
dvdy -= self.speed
self.pos = dvdx,dvdy
2024-01-21 15:47:15 +01:00
2024-01-19 21:59:16 +01:00
def newcolor(self):
self.color = (ri(0,255),ri(0,255),ri(0,255))
2024-01-21 15:47:15 +01:00
def get_dvd(self,inverse):
scaledx = self.size[0] * self.scale
scaledy = self.size[1] * self.scale
self.scalesize = (scaledx, scaledy)
self.sdvd = pygame.Surface(self.scalesize, flags=pygame.SRCALPHA)
2024-01-20 09:48:03 +01:00
2024-01-21 15:47:15 +01:00
pygame.draw.rect(self.sdvd, self.color, (0, 0, self.scalesize[0], self.scalesize[1]))
self.sdvd.blit(pygame.transform.scale(self.dvd, self.scalesize), (0, 0))
tempdvd = pygame.PixelArray(self.sdvd)
if inverse :
tempdvd.replace(self.color, (0,0,0,0))
tempdvd.replace((0,0,0), self.color)
else:
tempdvd.replace((0,0,0), (0,0,0,0))
del tempdvd
screen.blit(self.sdvd, self.pos)
2024-01-19 21:59:16 +01:00
dvds = []
dvds.append(DVD(1,1,"dvds/dvd-logo.png"))#ri(1,10)))
2024-01-18 21:34:58 +01:00
2024-01-19 16:03:11 +01:00
def get_debug_text():
2024-01-20 09:48:03 +01:00
def text(text,line_counter):
debug_text = default_font.render(str(text), True, (240, 240, 240))
screen.blit(debug_text,(0,line_counter*default_font.get_height()))
line_counter = 1
globalvars =globals()
for a in globalvars:
text(f"{str(a)} = {str(globalvars[a])}",line_counter)
line_counter += 1
for a in dvds:
text(vars(a),line_counter)
line_counter += 1
2024-01-19 16:03:11 +01:00
2024-01-18 21:34:58 +01:00
running = True
if __name__ == "__main__":
while running:
if fill_screen:
screen.fill((0,0,0))
pressed_keys = pygame.key.get_pressed()
if pressed_keys[pygame.K_ESCAPE]:
exit()
if pressed_keys[pygame.K_SPACE] and not preview_pressed_keys[pygame.K_SPACE]:
fill_screen = not fill_screen
2024-01-19 16:03:11 +01:00
if pressed_keys[pygame.K_d] and not preview_pressed_keys[pygame.K_d]:
show_debug = not show_debug
2024-01-21 15:47:15 +01:00
if pressed_keys[pygame.K_i] and not preview_pressed_keys[pygame.K_i]:
inverse = not inverse
2024-01-18 21:34:58 +01:00
if pressed_keys[pygame.K_f] and screen.get_size():
2024-01-19 16:03:11 +01:00
is_fullscreen,screensize = toggle_fullscreen(is_fullscreen)
2024-01-19 21:59:16 +01:00
if pressed_keys[pygame.K_UP] and speeds>=0:
speeds += 1
if pressed_keys[pygame.K_DOWN] and speeds>=1:
speeds -= 1
if pressed_keys[pygame.K_RIGHT] and scales>0.1:
scales += 0.1
if pressed_keys[pygame.K_LEFT] and scales>=0.2:
scales -= 0.1
if pressed_keys[pygame.K_PLUS] and not preview_pressed_keys[pygame.K_PLUS]:
dvds.append(DVD(1,1,"dvds/dvd-logo.png")) #ri(1,30)))
2024-01-19 21:59:16 +01:00
if pressed_keys[pygame.K_MINUS] and not preview_pressed_keys[pygame.K_MINUS] and not len(dvds) == 0:
dvds.pop(0)
2024-01-18 21:34:58 +01:00
preview_pressed_keys = pressed_keys
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
if event.type == pygame.WINDOWRESIZED:
2024-01-19 16:03:11 +01:00
screensize = pygame.display.get_window_size()
2024-01-18 21:34:58 +01:00
2024-01-19 21:59:16 +01:00
for a in dvds:
a.speed = speeds
a.scale = scales
a.moving()
2024-01-21 15:47:15 +01:00
a.get_dvd(inverse)
2024-01-19 21:59:16 +01:00
2024-01-19 16:03:11 +01:00
if show_debug:
get_debug_text()
pygame.display.flip()
2024-01-19 22:09:41 +01:00
clock.tick(60)