# pgzero-touch-serial-joystick.py (25.01.2023-20:40)
import pgzrun, time, math, random
from pgzhelper import *

import serial
import serial.tools.list_ports

port_ = 'COM11'   # Hier ist ESP-Modul mit Beschleunigungssensor angeschlossen
baudrate_ = 115200
ser = serial.Serial( port_, baudrate_, bytesize=serial.EIGHTBITS,
                     parity=serial.PARITY_NONE, timeout=1 )

WIDTH = 1920; HEIGHT = 1024
#WIDTH = 1920//2-150; HEIGHT = 1024//2-70
#WIDTH = 800; HEIGHT = 600

is_fullscreen = True

font_size = 40
gmax = 50
dg = 0.5


mitte_pos = WIDTH//2, HEIGHT//2
kugel = Actor('kugel-blau.png', center = mitte_pos)
mitte = Actor('mitte.png', center = mitte_pos)

diamond = Actor('diamond.png')
joystick = Actor('star1.png',center = mitte_pos)

cnt=0; treffer = 0; fehler = 0; startzeit = 0; weg = 0

cnt=0; treffer = 0; fehler = 0; startzeit = 0; weg = 0
gx=0; gy=0
dx = 0; dy = 0
start = False
print("##### start #####")
ser.flushInput()

def draw():
  global cnt, treffer, fehler, startzeit, weg, is_fullscreen
  screen.clear()
  if start:
    cnt += 1
    screen.fill((128, 128, 0))
    zeit = time.time()-startzeit
    laufzeit = f'{zeit:5.1f} ms'
    screen.draw.text(laufzeit, (100,3), color='yellow', fontsize=20)
    #screen.draw.text(str(cnt)+" Frames", (200,3), color='yellow', fontsize=20)
    screen.draw.text(f'{weg:7.1f} Pixel', (200,3), color='yellow', fontsize=20)
    screen.draw.text(str(treffer)+" Treffer", (500,3), color='yellow', fontsize=20)
    screen.draw.text(str(fehler)+" Fehler", (400,3), color='yellow', fontsize=20)
    txt = f'Diamond: {diamond.pos[0]:4} {diamond.pos[1]:4} gx={gx:4.1f} gy={gy:4.1f} '
    screen.draw.text( txt, (WIDTH//2-100, HEIGHT-40), color='yellow', fontsize=20)
    
    #screen.draw.text("(c)ASigismund mit Pygame Zero", (HEIGHT-20,WIDTH-100), color='yellow', fontsize=10)
  else:
    startzeit = time.time()  #WIDTH = 800; HEIGHT//+100 
    x_tmp = mitte.pos[0]+100+random.randint(-WIDTH//2 + 100, WIDTH//2 - 100)
    y_tmp = mitte.pos[1]+100+random.randint(-HEIGHT//2 + 100 , HEIGHT//2 - 100 )
    
    kugel.pos = x_tmp, y_tmp 
    #diamond.pos = mitte.pos[0]+random.randint(-WIDTH+100,WIDTH-100), mitte.pos[1]+random.randint(-HEIGHT+100,HEIGHT-100)
    diamond.pos = mitte.pos[0]+random.randint(-200,200), mitte.pos[1]+random.randint(-222,222)
    #diamond.pos = mitte.pos[0]+200, mitte.pos[1]+200
    joystick.pos = mitte.pos

    cnt=0; treffer = 0; fehler = 0
    screen.fill((128, 0, 0))
    screen.draw.text("Pfeiltasten-Joystick", (WIDTH//3,50), color='yellow', fontsize=font_size*1.5)
    screen.draw.text("Drücke die EINGABE-Taste, um die Challange zu starten!", (WIDTH//3,100), color='yellow', fontsize=font_size)
    screen.draw.text("Durch die Position des Joysticks zum Kreismittelpunkt wird ", (WIDTH//3,150), color='yellow', fontsize=font_size)
    screen.draw.text("wird die Bewegung der Kugel bestimmt.", (WIDTH//3, 150+1.2*font_size), color='yellow', fontsize=font_size)
    screen.draw.text("Sammle die Diamanten ein, ohne den Fensterrand zu berühren.", (WIDTH//3,290), color='yellow', fontsize=font_size)
    screen.draw.text("Mit Rechts-Klick wird das Spiel neu gestartet.", (WIDTH//3,330), color='yellow', fontsize=font_size)
    #screen.draw.text("(c)ASigismund mit Pygame Zero", (HEIGHT-150, WIDTH-400), color='yellow', fontsize=20)
  screen.draw.circle( mitte.pos, 30, 'white')

  joystick.draw()
  if start:
    kugel.draw()  
    diamond.draw()
  if is_fullscreen:
    toggle_fullscreen()
    is_fullscreen = False
    print("fullscreen", is_fullscreen)

def update():
  global dx, dy, gx, gy, gmax, dg, start, treffer, fehler, weg, startzeit


  if keyboard[keys.RETURN]:
    print("ENTER")
    start = True

  if ser.inWaiting():
      txt = ser.readline().decode().rstrip()
      print(txt)   
      if txt=="32":
        gy -= dg;
        if gy<-gmax: gy = -gmax
      if txt=="8": 
        gy += dg;
        if gy>gmax: gy = gmax
      if txt=="1":
        gx -= dg;
        if gx<-gmax: gx = -gmax
      if txt=="4":
        gx += dg
        if gx>gmax: gx = gmax
    
  if keyboard.x:
    print(start, gx, gy, dx, dy)
    time.sleep(1.0)
  if keyboard.space:
    start=True; startzeit = time.time()
    treffer = 0;  fehler = 0; weg = 0
    kugel.pos = mitte_pos
    dx = 0; dy = 0
    gx = 0; gy = 0
  if keyboard.escape:
      print("diamond.pos: ",diamond.pos)
      exit()
  if keyboard.f:
      toggle_fullscreen()
    
  if start:
    abstand_max = 50
    tmp1 = joystick.left
    tmp2 = joystick.top
    joystick.center = mitte.x + gx, mitte.y + gy
    abstand = joystick.distance_to(mitte)
    winkel  = joystick.angle_to(mitte)
    if abstand>abstand_max:
        joystick.top  = tmp2
        joystick.left = tmp1
    #print(f'abstand: {abstand} joystick.center: {joystick.center[0]:5.1f}, {joystick.center[1]:5.1f}')
    if kugel.distance_to(diamond)<25:
        treffer += 1
        tmp_x = mitte.pos[0]+random.randint(-mitte.pos[0]+100,mitte.pos[0]-100)
        tmp_y = mitte.pos[1]+random.randint(-mitte.pos[1]+100,mitte.pos[1]-100)
        diamond.pos = tmp_x, tmp_y
        print("neue diamond.center: ",diamond.pos)
        #diamond.draw()
        weg += math.sqrt((kugel.pos[0]-diamond.pos[0])**2 + (kugel.pos[1]-diamond.pos[1])**2)
    dy = 0.05 * abstand * math.sin(winkel/180*math.pi)
    dx = 0.05 * abstand * math.cos(winkel/180*math.pi)
    kugel.left -= dx
    kugel.top  += dy
    if kugel.right > WIDTH:
      dx = - dx; gx = -gx
      fehler+=1
      kugel.right = WIDTH-5
    if kugel.left < 0:
      kugel.left = 0+5
      fehler+=1
      dx = -dx; gx = -gx
    if kugel.bottom > HEIGHT:
      dy = - dy; gy = -gy
      fehler+=1
      kugel.bottom = HEIGHT-5
    if kugel.top < 0:
      kugel.top = 0+5
      fehler+=1
      dy = -dy;  gy = -gy

pgzrun.go()