dreieck_test/main.py

82 lines
2.7 KiB
Python
Raw Permalink Normal View History

2024-01-21 17:57:58 +01:00
#!/usr/bin/python3
2024-01-23 09:26:42 +01:00
from math import sin,cos,radians
from time import sleep
import pygame
2024-01-21 17:57:58 +01:00
def berechne_dreieck(alpha,c):
2024-01-23 09:26:42 +01:00
alpha = radians(alpha)
2024-01-21 17:57:58 +01:00
a = cos(alpha)*c# x
2024-01-23 09:26:42 +01:00
b = -sin(alpha)*c# y
2024-01-21 17:57:58 +01:00
return a,b
2024-01-23 09:26:42 +01:00
def get_debug_text():
def text(text,line_counter):
debug_text = default_font.render(str(text), True, (0,0,0))
screen.blit(debug_text,(0,line_counter*default_font.get_height()))
line_counter = 1
2024-01-23 15:21:30 +01:00
globalvars = globals()
2024-01-23 09:26:42 +01:00
for a in globalvars:
text(f"{str(a)} = {str(globalvars[a])}",line_counter)
line_counter += 1
2024-01-23 15:21:30 +01:00
def centered_text(text=str,pos=tuple,color=tuple):
text = default_font.render(str(text), True, color)
screen.blit(text,(pos[0]-text.get_width()/2,pos[1]))
2024-01-23 09:26:42 +01:00
def get_best_size():
if screensize[0] <= screensize[1]:
best = screensize[0]
2024-01-23 15:21:30 +01:00
else:
2024-01-23 15:26:56 +01:00
best = screensize[1]
2024-01-23 09:26:42 +01:00
return best
2024-01-23 15:21:30 +01:00
2024-01-23 09:26:42 +01:00
pygame.init()
clock = pygame.time.Clock()
screen = pygame.display.set_mode([500,500],pygame.RESIZABLE)
screensize = pygame.display.get_window_size()
default_font = pygame.font.SysFont("sans", 14)
start_pos = screensize[0]/2,screensize[1]/2#(10,screensize[1]-10)
winkel = 0
2024-01-23 15:21:30 +01:00
best_radios = get_best_size()/2-20
2024-01-23 09:26:42 +01:00
running = True
if __name__ == "__main__":
while running:
screen.fill((255,255,255))
pressed_keys = pygame.key.get_pressed()
if pressed_keys[pygame.K_ESCAPE]:
exit()
if pressed_keys[pygame.K_UP]:
winkel += 1
if pressed_keys[pygame.K_DOWN]:
winkel -= 1
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
if event.type == pygame.WINDOWRESIZED:
screensize = pygame.display.get_window_size()
start_pos = screensize[0]/2,screensize[1]/2#(10,screensize[1]-10)
screen.fill((0,0,0))
2024-01-23 15:21:30 +01:00
best_radios = get_best_size()/2-20
2024-01-23 09:26:42 +01:00
if winkel >= 360 or winkel <= -1:
winkel = 0
2024-01-23 15:26:56 +01:00
a,b = berechne_dreieck(winkel,best_radios)#screensize[0]*screensize[1])
2024-01-23 09:26:42 +01:00
end_pos = start_pos[0]+a,start_pos[1]+b
# draw c schwarz
pygame.draw.line(screen,(0,0,0),start_pos,end_pos,3)
2024-01-23 15:21:30 +01:00
centered_text("c",(end_pos[0]+10,end_pos[1]+10),(0,0,0))
2024-01-23 09:26:42 +01:00
# draw a rot
pygame.draw.line(screen,(255,0,0),start_pos,(start_pos[0]+a,start_pos[1]),3)
2024-01-23 15:21:30 +01:00
centered_text("a",(start_pos[0]+10,start_pos[1]+10),(255,0,0))
2024-01-23 09:26:42 +01:00
# draw b blau
pygame.draw.line(screen,(0,0,255),(start_pos[0]+a,start_pos[1]),(start_pos[0]+a,start_pos[1]+b),3)
2024-01-23 15:21:30 +01:00
centered_text("b",(start_pos[0]+a+10,start_pos[1]+10),(0,0,255))
2024-01-23 09:26:42 +01:00
get_debug_text()
pygame.display.flip()
clock.tick(30)
#sleep(1)
winkel += 1