# pgzero-keyboard-joystick.py (21.01.2023-17:00) import pgzrun, time, math, random from pgzhelper import * WIDTH = 1920; HEIGHT = 1024 font_size = 40 gmax = 50 dg = 0.5 #WIDTH = 1920//2-150; HEIGHT = 1024//2-70 WIDTH = 800; HEIGHT = 600 mitte_pos = WIDTH//2, HEIGHT//2 print(WIDTH, HEIGHT, mitte_pos) 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 is_fullscreen = True gx=0; gy=0 dx = 0; dy = 0 start = False print("##### start #####") 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) #screen.draw.text("(c)ASigismund mit Pygame Zero", (HEIGHT-20,WIDTH-100), color='yellow', fontsize=10) else: startzeit = time.time() kugel.pos = mitte.pos[0]+300+random.randint(-100, 100), mitte.pos[1]+random.randint(-400, 400) #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 print("joystick.pos, mitte.pos", 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 if keyboard[keys.RETURN]: print("ENTER") start = True if keyboard.up: gy -= dg; if gy<-gmax: gy = -gmax if keyboard.down: gy += dg; if gy>gmax: gy = gmax if keyboard.left: gx -= dg; if gx<-gmax: gx = -gmax if keyboard.right: gx += dg if gx>gmax: gx = gmax if keyboard.space: start = not start if keyboard.x: print(start, gx, gy, dx, dy) time.sleep(1.0) if keyboard.space: start=True 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 fehler+=1 kugel.right = WIDTH-5 if kugel.left < 0: kugel.left = 0+5 fehler+=1 dx = -dx if kugel.bottom > HEIGHT: dy = - dy fehler+=1 kugel.bottom = HEIGHT-5 if kugel.top < 0: kugel.top = 0+5 fehler+=1 dy = -dy pgzrun.go()