import pygame
import sas_pygame as sas

view = sas.createView(600, 400, "white","Torwand")
hintergrund = sas.Rectangle(0,0,600,400, "green")
torwandOben = sas.Rectangle(520,0,20,50,"blue")
lochOben = sas.Rectangle(520,50,72,90,"yellow")
torwandMitte = sas.Rectangle(520,140,20,110, "blue")
lochUnten = sas.Rectangle(520,250,72,70, "yellow")
torwandUnten = sas.Rectangle(520,320,20,40, "blue")
boden = sas.Rectangle(0, 360,600,40,"red")

#Ball und Pfeil zeichnen
ball = sas.Circle(60,300,30,"magenta")
pfeil = sas.Polygon([(60,327)],"yellow")
pfeil.add (40,0)
pfeil.add(40,-6)
pfeil.add(60,3)
pfeil.add(40,12)
pfeil.add(40,6)
pfeil.add(0,6)
ergebnis = sas.Text(50,10,"","red")
    
def daneben():
    if ball.intersects(torwandOben) or ball.intersects(torwandMitte) or ball.intersects(torwandUnten):
        return True
    else:
        return False
    
def treffer():
    if lochOben.getShapeX()< ball.getShapeX() and lochOben.intersects(ball)  or lochUnten.contains(ball):  
        return True
    return False
    
def rueckmeldung(getroffen):
        if getroffen:
            ergebnis.set_text("Treffer (n: neues Spiel")
        else:
            ergebnis.set_text("Leider daneben (n: neues Spiel)")
       
def main():
    schiessen = False
    getroffen = False
    fehlschuss = False
    while view.running:
        view.step()
        if not schiessen and view.keyUpPressed():
            pfeil.turn(-0.5)
        elif not schiessen and view.keyDownPressed():
            pfeil.turn(0.5)
        elif view.keyRightPressed():
            schiessen = True
        elif view.keyPressed('n'):
            schiessen = False
            ball.moveTo(60, 300)
            pfeil.turnTo(90)
        if schiessen and pfeil.getDirection()<=90 and pfeil.getDirection()>=48:
                ball.setDirection(pfeil.getDirection())
                ball.move(4)
        getroffen = treffer()
        fehlschuss = daneben()
        if getroffen:
            rueckmeldung(True)
            getroffen = False
            schiessen = False
        elif fehlschuss:
            rueckmeldung(False)
            fehlschuss = False
            schiessen = False 


    view.quit()
        

main()


