Mechanics adjusted (not ready ...)

pull/2/head
kaqu 2020-11-22 19:15:07 +01:00
parent dc7b6a487a
commit 8224cb88b7
2 changed files with 78 additions and 35 deletions

View File

@ -30,7 +30,7 @@ GAMEAREA_MAX_X = 1920 # Assume FHD resolution
GAMEAREA_MIN_Y = 15
GAMEAREA_MAX_Y = 920-GAMEAREA_MIN_Y
def init_player(path):
def init_player(path, x, y):
# Gamepad init.
fd = open(path, "rb")
fcntl.fcntl(fd, fcntl.F_SETFL, os.O_NONBLOCK)
@ -42,10 +42,11 @@ def init_player(path):
"state" : 0, # Button mask (as above)
"delta_x" : 0, # Current delta setting
"delta_y" : 0,
"x" : GAMEAREA_MAX_X/2, # Current actual position
"y" : GAMEAREA_MAX_Y/2, # Start at center
"x" : x, # Current actual position
"y" : y, # Start at center
"w" : 10,
"h" : 100
"h" : 100,
"score" : 0
}
def exit_player(player):
@ -61,6 +62,13 @@ def init_object(x, y, w, h, delta_x, delta_y):
"h" : h,
}
def reinit_object(obj, delta_x, delta_y):
obj["x"] = GAMEAREA_MAX_X/2 - obj["w"]/2
obj["y"] = GAMEAREA_MAX_Y/2 - obj["h"]/2
obj["delta_x"] = delta_x
obj["delta_y"] = delta_y
def print_event(e):
print("Event: time {}.{:06d}, ".format(e.sec, e.usec), end='')
if e.matches(libevdev.EV_SYN):
@ -124,9 +132,9 @@ def eval_gamepad(player):
player["delta_x"] = 0
else: # ABS_Y
if e.value == 0:
player["delta_y"] = player["delta_y"] + 1
elif e.value == 255:
player["delta_y"] = player["delta_y"] - 1
elif e.value == 255:
player["delta_y"] = player["delta_y"] + 1
else: # 127/Neutral
player["delta_y"] = 0
bChanged = True
@ -144,48 +152,54 @@ def eval_gamepad(player):
def eval_position(player):
bChanged = False
'''
sum_x = player["x"] + player["delta_x"] * 10 #255
sum_y = player["y"] + player["delta_y"] * 10 #255
if sum_x < GAMEAREA_MIN_X:
sum_x = GAMEAREA_MIN_X
elif sum_x + player["w"] > GAMEAREA_MAX_X:
sum_x = GAMEAREA_MAX_X - player["w"]
'''
sum_y = player["y"] + player["delta_y"] * 10 #255
if sum_y < GAMEAREA_MIN_Y:
sum_y = GAMEAREA_MIN_Y
elif sum_y + player["h"] > GAMEAREA_MAX_Y:
sum_y = GAMEAREA_MAX_Y - player["h"]
if (sum_x != player["x"]) or (sum_y != player["y"]):
#if (sum_x != player["x"]) or (sum_y != player["y"]):
if sum_y != player["y"]:
bChanged = True
player["x"] = sum_x
#player["x"] = sum_x
player["y"] = sum_y
return bChanged
def eval_object(obj):
bChanged = 0
sum_x = obj["x"] + obj["delta_x"]
sum_y = obj["y"] + obj["delta_y"]
if sum_x < GAMEAREA_MIN_X:
sum_x = GAMEAREA_MIN_X
obj["delta_x"] = -obj["delta_x"]
#sum_x = GAMEAREA_MIN_X
#obj["delta_x"] = -obj["delta_x"]
bChanged = -1 # Miss left
elif sum_x + obj["w"] > GAMEAREA_MAX_X:
sum_x = GAMEAREA_MAX_X - obj["w"]
obj["delta_x"] = -obj["delta_x"]
#sum_x = GAMEAREA_MAX_X - obj["w"]
#obj["delta_x"] = -obj["delta_x"]
bChanged = 1 # Miss right
if sum_y < GAMEAREA_MIN_Y:
sum_y = GAMEAREA_MIN_Y
obj["delta_y"] = -obj["delta_y"]
obj["delta_y"] = -obj["delta_y"]
elif sum_y + obj["h"] > GAMEAREA_MAX_Y:
sum_y = GAMEAREA_MAX_Y - obj["h"]
obj["delta_y"] = -obj["delta_y"]
obj["x"] = sum_x
obj["y"] = sum_y
return bChanged
def draw_objects(ball, player1, player2): # Update objects on playground
print("\rB: ", ball["x"], "/", ball["y"], end="")

View File

@ -23,6 +23,22 @@ from PyQt5.QtGui import QPainter, QBrush, QPen
import evalgamepad
def crashvectors(ball, player1, player2):
if ball["delta_x"] < 0: # Ball moves right->left
if ball["x"] - ball["w"]/2 <= player1["x"] + player1["w"]: # Left player in range?
if ball["y"] + ball["h"] >= player1["y"]: # Ball lower than player upper bound?
if ball["y"] - ball["h"]/2 <= player1["y"] + player1["h"]: # Ball higher than player lower bound?
ball["delta_x"] = -ball["delta_x"] # Invert direction
print("********************* PLAYER #1/BALL SAVED ********************")
else: # Ball moves left->right
if ball["x"] + ball["w"]/2 >= player2["x"]: # Right player in range?
if ball["y"] + ball["h"] >= player2["y"]: # Ball lower than player upper bound?
if ball["y"] - ball["h"]/2 <= player2["y"] + player2["h"]: # Ball higher than player lower bound?
ball["delta_x"] = -ball["delta_x"] # Invert direction
print("********************* PLAYER #2/BALL SAVED ********************")
def pong_game(ball, player1, player2):
# 1. Retrieve user entries
bChanged = evalgamepad.eval_gamepad(player1)
@ -48,8 +64,21 @@ def pong_game(ball, player1, player2):
if bChanged:
print("Player #2: ", player2["x"], " / ", player2["y"])
# 3. Adjust object positions
evalgamepad.eval_object(ball)
# 3. Crash analyses
crashvectors(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")
player2["score"] = player2["score"] + 1
evalgamepad.reinit_object(ball, 6, 2)
else:
print("***** Player #1: +1")
player1["score"] = player1["score"] + 1
evalgamepad.reinit_object(ball, -6, 2)
# use: playsound('/usr/lib/libreoffice/share/gallery/sounds/laser.wav')
return True
@ -64,17 +93,15 @@ class pongWindow(QMainWindow):
self.height = 960
self.InitWindow()
def InitWindow(self):
def InitWindow(self):
self.setWindowTitle(self.title)
self.setGeometry(self.top, self.left, self.width, self.height)
#self.showFullScreen()
pic = QPixmap("pong_background.png")
painter = QPainter(self)
painter.drawPixmap(self.rect(), pic)
self.pic = QPixmap("pictures/pong_background.png")
self.show()
self.timer = QTimer() # Start processing 'loop'
self.timer.setInterval(25) # 50ms
self.timer.setInterval(25) # 25ms
self.timer.setTimerType(Qt.PreciseTimer)
self.timer.timeout.connect(self.on_timer)
self.timer.start()
@ -82,20 +109,22 @@ class pongWindow(QMainWindow):
def paintEvent(self, event):
global ball, player1, player2
painter = QPainter(self)
painter = QPainter(self) # Painter need's to be freshly picked ...
painter.setPen(QPen(Qt.white, 1, Qt.SolidLine)) # Circle: Color/Linewidth/Pattern
painter.setBrush(QBrush(Qt.white, Qt.SolidPattern)) # Fill
painter.drawPixmap(self.rect(), self.pic) # Game background
pic = QPixmap("pictures/pong_background.png")
painter.drawPixmap(self.rect(), pic) # Game background
painter.drawEllipse(ball["x"], ball["y"], 50, 50) # Ball
painter.drawRect(10, player1["y"], 20, 100) # Player left
painter.drawRect(evalgamepad.GAMEAREA_MAX_X - 30, player2["y"], 20, 100) # Player right
painter.drawEllipse(int(ball["x"]), int(ball["y"]), 50, 50) # Ball
painter.drawRect(10, int(player1["y"]), 20, 100) # Player left
painter.drawRect(evalgamepad.GAMEAREA_MAX_X - 30, int(player2["y"]), 20, 100) # Player right
painter.drawText(10, 30, "X: {}".format(ball["x"]))
painter.drawText(10, 60, "y: {}".format(ball["y"]))
painter.drawText(10, 40, "y: {}".format(ball["y"]))
painter.drawText(200,40, "Player #1: {}".format(player1["score"]))
painter.drawText(evalgamepad.GAMEAREA_MAX_X-400,40, "Player #2: {}".format(player2["score"]))
@pyqtSlot()
def on_timer(self):
@ -115,9 +144,9 @@ if __name__ == '__main__':
try:
# Initialize
evalgamepad.init_gamearea()
ball = evalgamepad.init_object(10, 10, 15, 15, 8.0, 2.5)
player1 = evalgamepad.init_player(sys.argv[1])
player2 = evalgamepad.init_player(sys.argv[2])
ball = evalgamepad.init_object(evalgamepad.GAMEAREA_MAX_X/2, evalgamepad.GAMEAREA_MAX_Y/2, 15, 15, 8.0, 2.5)
player1 = evalgamepad.init_player(sys.argv[1], 10, evalgamepad.GAMEAREA_MAX_Y/2-50)
player2 = evalgamepad.init_player(sys.argv[2], evalgamepad.GAMEAREA_MAX_X-20, evalgamepad.GAMEAREA_MAX_Y/2-50)
except IOError as e:
import errno