#!/usr/bin/python3 import pygame from random import randint as ri pygame.init() default_font = pygame.font.SysFont("sans", 14) is_fullscreen = False fill_screen = True show_debug = False screen = pygame.display.set_mode((0,0),pygame.FULLSCREEN) pygame.display.set_caption("Screensaver") screensize = pygame.display.get_window_size() clock = pygame.time.Clock() scales = 1 speeds = 1 class DVD(): def __init__(self,scale,speed): self.newcolor() self.dvd = pygame.image.load("dvd-logo.png") 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() self.richtung = ["r","u"] self.render() def moving(self): if self.pos[0]+self.scalesize[0] >= screensize[0]: self.richtung[0] = "l" self.newcolor() if self.pos[0] <= 0: self.richtung[0] = "r" self.newcolor() if self.pos[1]+self.scalesize[1] >= screensize[1]: self.richtung[1] = "o" self.newcolor() if self.pos[1] <= 0: self.richtung[1] = "u" self.newcolor() dvdx,dvdy = self.pos 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 return dvdx,dvdy def newcolor(self): self.color = (ri(0,255),ri(0,255),ri(0,255)) def get_dvd(self): scaledx = self.size[0]*self.scale scaledy = self.size[1]*self.scale self.scalesize = (scaledx,scaledy) self.rendered_dvd = pygame.surface.Surface(self.scalesize, flags=pygame.SRCALPHA) self.rendered_dvd = pygame.transform.scale(self.dvd,self.scalesize)#(self.scalesize[0],self.scalesize[1]+10)) self.rendered_dvd2 = pygame.surface.Surface(self.scalesize, flags=pygame.SRCALPHA) self.rendered_dvd2.fill(self.color) pygame.transform.threshold(self.rendered_dvd, self.rendered_dvd2, (0, 0, 0), (255, 0, 0, 128), self.color, inverse_set=False) #screen.blit(self.rendered_dvd2,self.pos) screen.blit(self.rendered_dvd,self.pos) def render(self): self.get_dvd() #pygame.draw.rect(screen,self.color,(self.pos[0],self.pos[1],self.scalesize[0],self.scalesize[1])) #screen.blit(self.rendered_dvd,self.pos) dvds = [] dvds.append(DVD(1,1))#ri(1,10))) 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() def get_debug_text(): 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 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 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(): is_fullscreen,screensize = toggle_fullscreen(is_fullscreen) 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)) #ri(1,30))) if pressed_keys[pygame.K_MINUS] and not preview_pressed_keys[pygame.K_MINUS] and not len(dvds) == 0: dvds.pop(0) preview_pressed_keys = pressed_keys for event in pygame.event.get(): if event.type == pygame.QUIT: exit() if event.type == pygame.WINDOWRESIZED: screensize = pygame.display.get_window_size() for a in dvds: a.speed = speeds a.scale = scales a.moving() a.render() if show_debug: get_debug_text() pygame.display.flip() clock.tick(60)