import pgzrun
from time import time
from math import pi, sin, asin, cos

WIDTH, HEIGHT = 500, 500

rechteck = Rect((0, 0), (50, 50))

nachricht = ""

def skaliere_nach(wert, von_min, von_max, nach_min, nach_max):
    bereich_von = von_max - von_min
    bereich_nach = nach_max - nach_min
    return (((wert - von_min) * bereich_nach) / bereich_von) + nach_min

def sinus_oszillator(frequenz, wert_min, wert_max, zeit_versatz=0):
    t = time() + zeit_versatz
    t = t * pi * 2 * frequenz
    wert = sin(t)
    return skaliere_nach(wert, -1, 1, wert_min, wert_max)

def textnachricht(text):
    screen.draw.text(text,
                    center=(WIDTH/2, HEIGHT/2),
                    color="black",
                    fontsize=40)

def update():
    pass

def draw():
    rot = sinus_oszillator(0.1, 0, 255)
    gruen = sinus_oszillator(0.11, 0, 255)
    blau = sinus_oszillator(0.12, 0, 255)
    screen.fill((rot, gruen, blau))
    
    rot2 = 255 - rot
    gruen2 = 255 - gruen
    blau2 = 255 - blau
    
    x = sinus_oszillator(0.1, 0, WIDTH)
    y = sinus_oszillator(0.15, 0, HEIGHT)
    
    rechteck.center = (x, y)
    screen.draw.filled_rect(rechteck, (rot2, gruen2, blau2))
    
    textnachricht(nachricht)
    
def on_mouse_down(pos):
    global nachricht
    
    if rechteck.collidepoint(pos):
        print("Treffer!")
        nachricht = "Treffer!"
    else:
        print("Daneben!")
        nachricht = "Daneben!"

pgzrun.go( )
Zuletzt geändert: Donnerstag, 27. März 2025, 16:05