diff --git a/evalgamepad.py b/evalgamepad.py index 88151f0..94a2620 100644 --- a/evalgamepad.py +++ b/evalgamepad.py @@ -12,9 +12,11 @@ # /dev/input/event7 # -import sys, os, fcntl, time +import sys, os, fcntl, time, random import libevdev +from playsound import playsound + # Buttons in 'state' BTN_TRIGGER = 1 # [Blue/X] BTN_THUMB = 2 # [Red/A] @@ -28,7 +30,7 @@ BTN_BASE4 = 128 # [Centre right] GAMEAREA_MIN_X = 0 # Game area definition GAMEAREA_MAX_X = 1920 # Assume FHD resolution GAMEAREA_MIN_Y = 15 -GAMEAREA_MAX_Y = 920-GAMEAREA_MIN_Y +GAMEAREA_MAX_Y = 940 #-GAMEAREA_MIN_Y def init_player(path, x, y, w, h): # Gamepad init. @@ -55,7 +57,7 @@ def exit_player(player): def init_object(x, y, w, h, delta_x, delta_y): return { "delta_x" : delta_x, # Current movement - "delta_y" : delta_y, + "delta_y" : (random.random() - 0.5) * 10, "x" : x, # Current position "y" : y, "w" : w, # Size of object @@ -193,9 +195,11 @@ def eval_object(obj): if sum_y < GAMEAREA_MIN_Y: sum_y = GAMEAREA_MIN_Y obj["delta_y"] = -obj["delta_y"] + playsound('/usr/share/mint-artwork/sounds/logout.ogg') elif sum_y + obj["h"] > GAMEAREA_MAX_Y: sum_y = GAMEAREA_MAX_Y - obj["h"] obj["delta_y"] = -obj["delta_y"] + playsound('/usr/share/mint-artwork/sounds/logout.ogg') obj["x"] = sum_x obj["y"] = sum_y diff --git a/pictures/pong.jpeg b/pictures/pong.jpeg deleted file mode 100644 index 4cde205..0000000 Binary files a/pictures/pong.jpeg and /dev/null differ diff --git a/pictures/pong_background_test.png b/pictures/pong_background_test.png deleted file mode 100644 index 4adcbdc..0000000 Binary files a/pictures/pong_background_test.png and /dev/null differ diff --git a/simple_pong.py b/simple_pong.py index 4fdd773..1223ad2 100644 --- a/simple_pong.py +++ b/simple_pong.py @@ -10,7 +10,7 @@ History: 21.10.20/KQ Initial version """ -import sys, os, fcntl, time +import sys, os, fcntl, time, random import os import time @@ -26,15 +26,29 @@ import evalgamepad def crashvectors(ball, player1, player2): if ball["delta_x"] < 0: # Ball moves right->left (<----) - if ball["x"] <= player1["x"] + player1["w"]: # Left player in range? + if ball["x"] <= player1["x"] + player1["w"]: # Left player #1 in range? if ball["y"] + ball["h"] >= player1["y"]: # Ball lower than player upper bound? if ball["y"] <= player1["y"] + player1["h"]: # Ball higher than player lower bound? - ball["delta_x"] = -ball["delta_x"]*1.2 # Invert direction + ball["delta_x"] = -ball["delta_x"] * 1.2 # Invert direction & speed up + if player1["delta_y"] > 0.0: # Going downwards + if ball["delta_y"] > 0.0: # As well? + ball["delta_y"] = ball["delta_y"] * 1.2 # Increase angle + else: + ball["delta_y"] = ball["delta_y"] * 0.8 # Decrease angle + elif player1["delta_y"] < 0.0: + if ball["delta_y"] < 0.0: # As well? + ball["delta_y"] = ball["delta_y"] * 1.2 # Increase angle + else: + ball["delta_y"] = ball["delta_y"] * 0.8 # Decrease angle + + playsound('/usr/share/mint-artwork/sounds/logout.ogg') # Enforce caching ... + else: # Ball moves left->right (---->) - if ball["x"] + ball["w"] >= player2["x"]: # Right player in range? + if ball["x"] + ball["w"] >= player2["x"]: # Right player #2 in range? if ball["y"] + ball["h"] >= player2["y"]: # Ball lower than player upper bound? if ball["y"] <= player2["y"] + player2["h"]: # Ball higher than player lower bound? - ball["delta_x"] = -ball["delta_x"]*1.2 # Invert direction + ball["delta_x"] = -ball["delta_x"] * 1.2 # Invert direction & speed up + playsound('/usr/share/mint-artwork/sounds/logout.ogg') # Enforce caching ... def pong_game(ball, player1, player2): @@ -68,15 +82,13 @@ def pong_game(ball, player1, player2): # 4. Adjust object positions (ball & potentially others ...) rc = evalgamepad.eval_object(ball) if rc != 0: # Miss left(-1) or right(1) - if rc == -1: - print("***** Player #2: +1") + if rc == -1: # Player #2: +1 player2["score"] = player2["score"] + 1 - evalgamepad.reinit_object(ball, 6, 2) + evalgamepad.reinit_object(ball, 6 + (random.random() - 0.5) * 4, (random.random() - 0.5) * 10) playsound('/usr/lib/libreoffice/share/gallery/sounds/laser.wav') - else: - print("***** Player #1: +1") + else: # Player #1: +1 player1["score"] = player1["score"] + 1 - evalgamepad.reinit_object(ball, -6, 2) + evalgamepad.reinit_object(ball, -(6 + (random.random() - 0.5) * 4), (random.random() - 0.5) * 10) playsound('/usr/lib/libreoffice/share/gallery/sounds/laser.wav') return True @@ -121,7 +133,9 @@ class pongWindow(QMainWindow): self.fm = QFontMetrics(self.fnt) self.show() + playsound('/usr/share/mint-artwork/sounds/close.oga') playsound('/usr/lib/libreoffice/share/gallery/sounds/laser.wav') # Enforce caching ... + playsound('/usr/share/mint-artwork/sounds/logout.ogg') self.timer = QTimer() # Start processing 'loop' self.timer.setInterval(25) # 25ms @@ -142,8 +156,10 @@ class pongWindow(QMainWindow): painter.drawRect(int(player1["x"]), int(player1["y"]), int(player1["w"]), int(player1["h"])) # Player left painter.drawRect(int(player2["x"]), int(player2["y"]), int(player2["w"]), int(player2["h"])) # Player right - #painter.drawText(10, 30, "X: {}".format(ball["x"])) + #painter.drawText(10, 30, "x: {}".format(ball["x"])) #painter.drawText(10, 40, "y: {}".format(ball["y"])) + painter.drawText(10,50, "Player #1 delta_y: {}".format(player1["delta_y"])) + painter.drawText(10,60, "Player #2 delta_y: {}".format(player2["delta_y"])) # Game scores painter.setFont(self.fnt) @@ -176,6 +192,7 @@ class pongWindow(QMainWindow): if __name__ == '__main__': try: # Initialize + random.seed() evalgamepad.init_gamearea() ball = evalgamepad.init_object(evalgamepad.GAMEAREA_MAX_X/2, evalgamepad.GAMEAREA_MAX_Y/2, 20, 20, 8.0, 2.5) player1 = evalgamepad.init_player(sys.argv[1], 10, evalgamepad.GAMEAREA_MAX_Y/2-50, 20, 160)