import sas_pygame as sas

view = sas.createView(600, 400, "white","Bälle fangenr")
baelle = []
for i in range(20):
    x = sas.Tools.randomNumber(50,570)
    y = sas.Tools.randomNumber(-400,-10)
    baelle.append(sas.Circle(x, y, 15, "blue"))
korb = sas.Rectangle(375, 340, 50, 60, "green")
anzahl = sas.Text(10, 10, "Gefangen: 0", "red")

def update(view, gefangen, ende, start):
    for ball in baelle:
        if ball.getShapeY() > 400:
            ball.moveTo(sas.Tools.randomNumber(10, 570),sas.Tools.randomNumber(-150, -10))
            ball.setColor("blue")
        elif ball.getShapeY() + 30 > 340 and not ball.intersects(korb):
            ball.setColor("orange")
            ball.move(0,6)
        elif korb.contains(ball) and not ball.getHidden() and not ball.getColor == "orange":
            gefangen += 1
            anzahl.set_text(f"Gefangen: {gefangen}")
            ball.setHidden(True)  
        else:
            ball.move(0,6)
    if view.keyLeftPressed():
        korb.move(-5)
    elif view.keyRightPressed():
        korb.move(5)
    if gefangen == 20 and not ende:
            stop = sas.Tools.getElapsedTimeString(start)
            ergebnis = sas.Text(10,50, f"Zeit: {stop} Sekunden") 
            ende = True
    return ende, gefangen

def main():
    gefangen = 0
    ende = False
    start = sas.Tools.getStartTime()
    ende = False
    while view.running:
        view.step()
        ende, gefangen = update(view, gefangen, ende, start)
    view.quit()

main()