lissajous_figures/main.py

92 lines
3.2 KiB
Python
Raw Normal View History

2024-03-22 19:01:18 +01:00
#!/usr/bin/python3
import pygame
from math import radians,cos,sin
pygame.init()
## funktionen
def get_debug_text():
def text(text,line_counter):
2024-03-24 14:42:04 +01:00
debug_text = debug_font.render(str(text), True, (255,255,255))
screen.blit(debug_text,(0,line_counter*debug_font.get_height()))
2024-03-22 19:01:18 +01:00
line_counter = 1
globalvars = globals()
for a in globalvars:
text(f"{str(a)} = {str(globalvars[a])}",line_counter)
line_counter += 1
def get_best_size():
if screensize[0] <= screensize[1]:
best = screensize[0]
else:
best = screensize[1]
return best
## klassen
## variablen
screen = pygame.display.set_mode((300,300),pygame.RESIZABLE)
pygame.display.set_caption("Lissajous Figures")
screensize = pygame.display.get_window_size()
clock = pygame.time.Clock()
2024-03-24 14:42:04 +01:00
debug_font = pygame.font.SysFont("sans", 14)
default_font = pygame.font.SysFont(pygame.font.get_default_font(), 25)
2024-03-22 19:01:18 +01:00
mitte_pos = screensize[0]//2,screensize[1]//2
frequ1 = 1
2024-03-22 20:02:08 +01:00
frequ2 = 1
2024-03-22 19:01:18 +01:00
phasenverschiebung = 90
2024-03-24 14:42:04 +01:00
phasenverschiebungspeed = 1
2024-03-22 19:01:18 +01:00
scale = (get_best_size()-10)//2
preview_pos = 0,0
show_debug = False
running = True
if __name__ == "__main__":
while running:
## key managment
pressed_keys = pygame.key.get_pressed()
if pressed_keys[pygame.K_ESCAPE]:
exit()
if pressed_keys[pygame.K_d] and not preview_pressed_keys[pygame.K_d]:
show_debug = not show_debug
2024-03-22 20:02:08 +01:00
if pressed_keys[pygame.K_UP] and not preview_pressed_keys[pygame.K_UP]:
frequ1 += 1
2024-03-24 14:42:04 +01:00
if pressed_keys[pygame.K_DOWN] and not preview_pressed_keys[pygame.K_DOWN] and frequ1 >=1:
2024-03-22 20:02:08 +01:00
frequ1 -= 1
if pressed_keys[pygame.K_RIGHT] and not preview_pressed_keys[pygame.K_RIGHT]:
frequ2 += 1
2024-03-24 14:42:04 +01:00
if pressed_keys[pygame.K_LEFT] and not preview_pressed_keys[pygame.K_LEFT] and frequ2>=1:
2024-03-22 20:02:08 +01:00
frequ2 -= 1
2024-03-24 14:42:04 +01:00
if pressed_keys[pygame.K_PLUS] and not preview_pressed_keys[pygame.K_PLUS]:
phasenverschiebungspeed += 0.05
if pressed_keys[pygame.K_MINUS] and not preview_pressed_keys[pygame.K_MINUS] and phasenverschiebungspeed>=0:
phasenverschiebungspeed -= 0.05
2024-03-22 19:01:18 +01:00
preview_pressed_keys = pressed_keys
## event managment
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
if event.type == pygame.WINDOWRESIZED:
screensize = pygame.display.get_window_size()
mitte_pos = screensize[0]//2,screensize[1]//2
scale = (get_best_size()-10)//2
if show_debug:
get_debug_text()
2024-03-24 14:42:04 +01:00
phasenverschiebung += phasenverschiebungspeed
2024-03-22 19:01:18 +01:00
if phasenverschiebung >= 360:
phasenverschiebung = 0
for r in range(360):
pos = (sin(radians(frequ1*(r+phasenverschiebung)))*scale)+mitte_pos[0],(cos(radians(frequ2*r))*scale)+mitte_pos[1]
pygame.draw.line(screen,(0,255,0),preview_pos,pos,3)
preview_pos = pos
2024-03-22 20:02:08 +01:00
2024-03-24 14:42:04 +01:00
frequs_txt = default_font.render(f"Speed = {phasenverschiebungspeed} X = {frequ1} Y = {frequ2}", True, (255,255,255))
2024-03-22 20:02:08 +01:00
screen.blit(frequs_txt,(0,0))
2024-03-22 19:01:18 +01:00
## bildschirm aktuallisierung
pygame.display.flip()
clock.tick(60)
screen.fill((0,0,0))