screensaver/main.py

113 lines
3.6 KiB
Python
Raw Normal View History

2024-01-18 21:34:58 +01:00
import pygame
from random import randint as ri
def scale(vaktor):
2024-01-19 16:03:11 +01:00
scaledx = dvdsize[0]*vaktor
scaledy = dvdsize[1]*vaktor
2024-01-18 21:34:58 +01:00
return (scaledx,scaledy)
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-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-18 21:34:58 +01:00
screen = pygame.display.set_mode((0,0),pygame.FULLSCREEN)
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()
dvd = pygame.image.load("dvd-logo.png")
2024-01-19 16:03:11 +01:00
dvdsize = dvd.get_size()
2024-01-18 21:34:58 +01:00
dvdscale = 1
dvdscalesize = scale(dvdscale)
dvdscaled = pygame.transform.scale(dvd,dvdscalesize)
2024-01-19 16:03:11 +01:00
dvdpos = ri(0,screensize[0]-dvdsize[0]),ri(0,screensize[1]-dvdsize[1])
dvdspeed = 3
2024-01-18 21:34:58 +01:00
richtung = ["r","u"]
def randcolor():
return (ri(0,255),ri(0,255),ri(0,255))
2024-01-19 16:03:11 +01:00
def moving(richtung,dvdpos):
dvdx,dvdy = dvdpos
2024-01-18 21:34:58 +01:00
if richtung[0] == "r":
2024-01-19 16:03:11 +01:00
dvdx += dvdspeed
2024-01-18 21:34:58 +01:00
else:
2024-01-19 16:03:11 +01:00
dvdx -= dvdspeed
2024-01-18 21:34:58 +01:00
if richtung[1] =="u":
2024-01-19 16:03:11 +01:00
dvdy += dvdspeed
2024-01-18 21:34:58 +01:00
else:
2024-01-19 16:03:11 +01:00
dvdy -= dvdspeed
return dvdx,dvdy
2024-01-18 21:34:58 +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
def get_debug_text():
debug_text = ""
for index,a in enumerate(globals()):
debug_text = default_font.render(f"{str(a)} = {globals()[a]}", True, (240, 240, 240))
screen.blit(debug_text,(0,index*default_font.get_height()))
2024-01-18 21:34:58 +01:00
running = True
if __name__ == "__main__":
color = randcolor()
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-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)
if pressed_keys[pygame.K_UP] and dvdspeed>=0:
dvdspeed += 1
if pressed_keys[pygame.K_DOWN] and dvdspeed>=1:
dvdspeed -= 1
2024-01-18 21:34:58 +01:00
if pressed_keys[pygame.K_RIGHT] and dvdscale>0.1:
dvdscale += 0.1
dvdscalesize = scale(dvdscale)
if pressed_keys[pygame.K_LEFT] and dvdscale>=0.2:
dvdscale -= 0.1
dvdscalesize = scale(dvdscale)
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 16:03:11 +01:00
if dvdpos[0]+dvdscalesize[0] >= screensize[0]:
2024-01-18 21:34:58 +01:00
richtung[0] = "l"
color = randcolor()
2024-01-19 16:03:11 +01:00
if dvdpos[0] <= 0:
2024-01-18 21:34:58 +01:00
richtung[0] = "r"
color = randcolor()
2024-01-19 16:03:11 +01:00
if dvdpos[1]+dvdscalesize[1] >= screensize[1]:
2024-01-18 21:34:58 +01:00
richtung[1] = "o"
color = randcolor()
2024-01-19 16:03:11 +01:00
if dvdpos[1] <= 0:
2024-01-18 21:34:58 +01:00
richtung[1] = "u"
color = randcolor()
2024-01-19 16:03:11 +01:00
dvdpos = moving(richtung,dvdpos)
pygame.draw.rect(screen,color,(dvdpos[0],dvdpos[1],dvdscalesize[0],dvdscalesize[1]))
2024-01-18 21:34:58 +01:00
dvdscaled = pygame.transform.scale(dvd,dvdscalesize)
2024-01-19 16:03:11 +01:00
screen.blit(dvdscaled,dvdpos)
if show_debug:
get_debug_text()
pygame.display.flip()
2024-01-18 21:34:58 +01:00
clock.tick(60)