debug mit key d

This commit is contained in:
Michael S. 2024-01-19 16:03:11 +01:00
parent d4d4d96a8e
commit e3b2947f31

67
main.py
View file

@ -1,42 +1,45 @@
import pygame import pygame
from random import randint as ri from random import randint as ri
def scale(vaktor): def scale(vaktor):
scaledx = dvdsizex*vaktor scaledx = dvdsize[0]*vaktor
scaledy = dvdsizey*vaktor scaledy = dvdsize[1]*vaktor
return (scaledx,scaledy) return (scaledx,scaledy)
pygame.init() pygame.init()
default_font = pygame.font.SysFont("sans", 14)
is_fullscreen = True is_fullscreen = False
fill_screen = True fill_screen = True
show_debug = False
screen = pygame.display.set_mode((0,0),pygame.FULLSCREEN) screen = pygame.display.set_mode((0,0),pygame.FULLSCREEN)
pygame.display.set_caption("Screensaver") pygame.display.set_caption("Screensaver")
x,y = pygame.display.get_window_size() screensize = pygame.display.get_window_size()
clock = pygame.time.Clock() clock = pygame.time.Clock()
dvd = pygame.image.load("dvd-logo.png") dvd = pygame.image.load("dvd-logo.png")
dvdsizex,dvdsizey = dvd.get_size() dvdsize = dvd.get_size()
dvdscale = 1 dvdscale = 1
dvdscalesize = scale(dvdscale) dvdscalesize = scale(dvdscale)
dvdscaled = pygame.transform.scale(dvd,dvdscalesize) dvdscaled = pygame.transform.scale(dvd,dvdscalesize)
dvdx,dvdy = ri(0,x-dvdsizex),ri(0,y-dvdsizey) dvdpos = ri(0,screensize[0]-dvdsize[0]),ri(0,screensize[1]-dvdsize[1])
wobspeed = 3 dvdspeed = 3
richtung = ["r","u"] richtung = ["r","u"]
def randcolor(): def randcolor():
return (ri(0,255),ri(0,255),ri(0,255)) return (ri(0,255),ri(0,255),ri(0,255))
def moving(richtung,wobx,woby): def moving(richtung,dvdpos):
dvdx,dvdy = dvdpos
if richtung[0] == "r": if richtung[0] == "r":
wobx += wobspeed dvdx += dvdspeed
else: else:
wobx -= wobspeed dvdx -= dvdspeed
if richtung[1] =="u": if richtung[1] =="u":
woby += wobspeed dvdy += dvdspeed
else: else:
woby -=wobspeed dvdy -= dvdspeed
return wobx,woby return dvdx,dvdy
def toggle_fullscreen(is_fullscreen): def toggle_fullscreen(is_fullscreen):
if is_fullscreen: if is_fullscreen:
@ -46,6 +49,12 @@ def toggle_fullscreen(is_fullscreen):
is_fullscreen = not is_fullscreen is_fullscreen = not is_fullscreen
return is_fullscreen,pygame.display.get_window_size() return is_fullscreen,pygame.display.get_window_size()
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()))
running = True running = True
if __name__ == "__main__": if __name__ == "__main__":
color = randcolor() color = randcolor()
@ -58,12 +67,14 @@ if __name__ == "__main__":
exit() exit()
if pressed_keys[pygame.K_SPACE] and not preview_pressed_keys[pygame.K_SPACE]: if pressed_keys[pygame.K_SPACE] and not preview_pressed_keys[pygame.K_SPACE]:
fill_screen = not fill_screen fill_screen = not fill_screen
if pressed_keys[pygame.K_d] and not preview_pressed_keys[pygame.K_d]:
show_debug = not show_debug
if pressed_keys[pygame.K_f] and screen.get_size(): if pressed_keys[pygame.K_f] and screen.get_size():
is_fullscreen,(x,y) = toggle_fullscreen(is_fullscreen) is_fullscreen,screensize = toggle_fullscreen(is_fullscreen)
if pressed_keys[pygame.K_UP] and wobspeed>=0: if pressed_keys[pygame.K_UP] and dvdspeed>=0:
wobspeed += 1 dvdspeed += 1
if pressed_keys[pygame.K_DOWN] and wobspeed>=1: if pressed_keys[pygame.K_DOWN] and dvdspeed>=1:
wobspeed -= 1 dvdspeed -= 1
if pressed_keys[pygame.K_RIGHT] and dvdscale>0.1: if pressed_keys[pygame.K_RIGHT] and dvdscale>0.1:
dvdscale += 0.1 dvdscale += 0.1
dvdscalesize = scale(dvdscale) dvdscalesize = scale(dvdscale)
@ -77,24 +88,26 @@ if __name__ == "__main__":
if event.type == pygame.QUIT: if event.type == pygame.QUIT:
exit() exit()
if event.type == pygame.WINDOWRESIZED: if event.type == pygame.WINDOWRESIZED:
x,y = pygame.display.get_window_size() screensize = pygame.display.get_window_size()
if dvdx+dvdscalesize[0] >= x: if dvdpos[0]+dvdscalesize[0] >= screensize[0]:
richtung[0] = "l" richtung[0] = "l"
color = randcolor() color = randcolor()
if dvdx <= 0: if dvdpos[0] <= 0:
richtung[0] = "r" richtung[0] = "r"
color = randcolor() color = randcolor()
if dvdy+dvdscalesize[1] >= y: if dvdpos[1]+dvdscalesize[1] >= screensize[1]:
richtung[1] = "o" richtung[1] = "o"
color = randcolor() color = randcolor()
if dvdy <= 0: if dvdpos[1] <= 0:
richtung[1] = "u" richtung[1] = "u"
color = randcolor() color = randcolor()
dvdx,dvdy = moving(richtung,dvdx,dvdy) dvdpos = moving(richtung,dvdpos)
pygame.draw.rect(screen,color,(dvdx,dvdy,dvdscalesize[0],dvdscalesize[1])) pygame.draw.rect(screen,color,(dvdpos[0],dvdpos[1],dvdscalesize[0],dvdscalesize[1]))
dvdscaled = pygame.transform.scale(dvd,dvdscalesize) dvdscaled = pygame.transform.scale(dvd,dvdscalesize)
screen.blit(dvdscaled,(dvdx,dvdy)) screen.blit(dvdscaled,dvdpos)
pygame.display.update() if show_debug:
get_debug_text()
pygame.display.flip()
clock.tick(60) clock.tick(60)