Rename, game object, welcome, button wait etc.

pull/2/head
kaqu 2 years ago
parent bb88ae455f
commit 817910a2a3
  1. 65
      evalgamepad.py
  2. 168
      pandemic_pong.py

@ -242,9 +242,70 @@ def gameloop(ball, player1, player2):
# 5. Little delay for testing
time.sleep(0.1)
STATE_WELCOME = 0
STATE_START = 1
STATE_PLAY = 2
STATE_GAMERESULTS = 3
STATE_FINALRESULTS = 4
STATE_RESTART = 5
STATE_EXIT = 6
def init_game(): # Create base playground
return {
"state" : STATE_WELCOME, # State of game
"delay" : 100,
"p1_game" : 0, # Results
"p2_game" : 0
}
def init_gamearea(): # Create base playground
pass
def next_gamestate(ga, player1, player2):
if ga["state"] == STATE_WELCOME:
ga["delay"] = ga["delay"] - 1
if ga["delay"] < 1:
ga["state"] = STATE_START
elif ga["state"] == STATE_START: # Start new game
if (player1["state"] & BTN_BASE4) > 0 or (player2["state"] & BTN_BASE4) > 0: # [Start] pressed?
player1["state"] = 0
player2["state"] = 0
ga["p1_game"] = 0
ga["p2_game"] = 0
player1["score"] = 0
player2["score"] = 0
ga["state"] = STATE_PLAY
elif ga["state"] == STATE_PLAY: # Remain playing 'til 10 points
if player1["score"] > 9 or (player2["score"] > 9):
if player1["score"] > 9: # Figure out winner
ga["p1_game"] = ga["p1_game"] + 1 # & count game accordingly
else:
ga["p2_game"] = ga["p2_game"] + 1
ga["delay"] = 100
ga["state"] = STATE_GAMERESULTS
elif ga["state"] == STATE_GAMERESULTS:
ga["delay"] = ga["delay"] - 1
if ga["delay"] < 1:
if (ga["p1_game"] > 2) or (ga["p2_game"] > 2): # Set won?
ga["state"] = STATE_FINALRESULTS # Yap!
else:
player1["score"] = 0
player2["score"] = 0
player1["state"] = 0
player2["state"] = 0
ga["state"] = STATE_PLAY # Nope, move on to next game
elif ga["state"] == STATE_FINALRESULTS:
if (player1["state"] & BTN_BASE3) > 0 or (player2["state"] & BTN_BASE3) > 0:
return False
elif ((player1["state"] & BTN_BASE4) > 0) or ((player2["state"] & BTN_BASE4) > 0):
player1["state"] = 0
player2["state"] = 0
ga["state"] = STATE_START # Continue by start button press
#if ga["state"] >= STATE_EXIT: not used ...
# return False
return True
def main(args):
try:

@ -1,9 +1,9 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
simple_pong.py
pandemic_pong.py
Simple python/Qt5 pong clone
Pandemic python/Qt5 pong clone
History:
--------
@ -51,46 +51,41 @@ def crashvectors(ball, player1, player2):
playsound('/usr/share/mint-artwork/sounds/logout.ogg') # Enforce caching ...
def pong_game(ball, player1, player2):
def pong_game(game, ball, player1, player2):
if evalgamepad.next_gamestate(game, player1, player2) == False: # Exit
return False
#print("Game state: {}".format(game["state"]))
# 1. Retrieve user entries
bChanged = evalgamepad.eval_gamepad(player1)
if bChanged:
if (player1["state"] & evalgamepad.BTN_BASE4) > 0: # Exit?
print("Player #1 aborted game")
return False
#else:
# print("Player #1: {}".format(hex(player1["state"])))
bChanged = evalgamepad.eval_gamepad(player2) # Exit?
if bChanged:
if (player2["state"] & evalgamepad.BTN_BASE4) > 0:
print("Player #2 aborted game")
return False
#else:
# print("Player #2: {}".format(hex(player2["state"])))
# 2. Adjust player positions
bChanged = evalgamepad.eval_position(player1)
#if bChanged:
# print("Player #1: ", player1["x"], " / ", player1["y"])
bChanged = evalgamepad.eval_position(player2)
#if bChanged:
# print("Player #2: ", player2["x"], " / ", player2["y"])
# 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: # Player #2: +1
player2["score"] = player2["score"] + 1
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: # Player #1: +1
player1["score"] = player1["score"] + 1
evalgamepad.reinit_object(ball, -(6 + (random.random() - 0.5) * 4), (random.random() - 0.5) * 10)
playsound('/usr/lib/libreoffice/share/gallery/sounds/laser.wav')
if game["state"] == evalgamepad.STATE_PLAY: # Actually playing?
# 2. Adjust player positions
bChanged = evalgamepad.eval_position(player1)
#if bChanged:
# print("Player #1: ", player1["x"], " / ", player1["y"])
bChanged = evalgamepad.eval_position(player2)
#if bChanged:
# print("Player #2: ", player2["x"], " / ", player2["y"])
# 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: # Player #2: +1
player2["score"] = player2["score"] + 1
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: # Player #1: +1
player1["score"] = player1["score"] + 1
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
def draw_buttonstate(x, y, player, painter):
@ -114,7 +109,7 @@ def draw_buttonstate(x, y, player, painter):
class pongWindow(QMainWindow):
def __init__(self):
super().__init__()
self.title = "Simple Pong"
self.title = "Pandemic Pong"
self.top= 0
self.left= 0
self.width = 1920
@ -127,16 +122,19 @@ class pongWindow(QMainWindow):
#self.showFullScreen()
self.pic = QPixmap("pictures/pong_background.png")
# Font metrics assume font is monospaced!
self.fnt = QFont("Monospace",120)
self.fnt.setKerning(False)
self.fnt.setFixedPitch(True)
self.fm = QFontMetrics(self.fnt)
self.fntLarge = QFont("Monospace", 120)
self.fntLarge.setKerning(False)
self.fntLarge.setFixedPitch(True)
self.fm = QFontMetrics(self.fntLarge)
self.fntMedium = QFont("Monospace", 40)
self.fntMedium.setKerning(False)
self.fntMedium.setFixedPitch(True)
self.show()
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
self.timer.setTimerType(Qt.PreciseTimer)
@ -144,45 +142,73 @@ class pongWindow(QMainWindow):
self.timer.start()
def paintEvent(self, event):
global ball, player1, player2
global game, ball, player1, player2
painter = QPainter(self) # Painter need's to be freshly picked ...
painter.setPen(QPen(Qt.white, 1, Qt.SolidLine)) # Color/Linewidth/Pattern
painter.setBrush(QBrush(Qt.white, Qt.SolidPattern)) # Fill
painter.drawPixmap(self.rect(), self.pic) # Game background
#painter.drawText(10,30, "Game state: {} ({}) Scores: P1={} P2={}".format(game["state"], game["delay"], game["p1_game"], game["p2_game"]))
if game["state"] == evalgamepad.STATE_WELCOME: # Welcome screen
painter.setFont(self.fntLarge)
painter.drawText(50, int(evalgamepad.GAMEAREA_MAX_Y / 2)-100, "PANDEMIC")
painter.drawText(int(evalgamepad.GAMEAREA_MAX_X / 2) + 50, int(evalgamepad.GAMEAREA_MAX_Y / 2)+100, "PONG")
elif game["state"] == evalgamepad.STATE_START: # Wait for start button pressed
painter.setFont(self.fntMedium)
painter.drawText(50, int(evalgamepad.GAMEAREA_MAX_Y / 2), "Press [Start] to start ...")
elif game["state"] == evalgamepad.STATE_PLAY: # Actual play 'til score reaches 10 points
painter.drawRect(int(ball["x"]), int(ball["y"]), int(ball["w"]), int(ball["h"])) # Ball
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, 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.fntLarge)
# Score player #1
msg = str(player1["score"])
msg_width = self.fm.width(msg)
painter.drawText(800 - msg_width, 154, "{}".format(player1["score"]))
# Score player #2
painter.drawText(evalgamepad.GAMEAREA_MAX_X-840, 154, "{}".format(player2["score"]))
painter.drawRect(int(ball["x"]), int(ball["y"]), int(ball["w"]), int(ball["h"])) # Ball
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, 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)
# Score player #1
msg = str(player1["score"])
msg_width = self.fm.width(msg)
painter.drawText(800 - msg_width, 154, "{}".format(player1["score"]))
# Score player #2
painter.drawText(evalgamepad.GAMEAREA_MAX_X-840, 154, "{}".format(player2["score"]))
# Indicate Button states
draw_buttonstate(60, 50, player1, painter)
draw_buttonstate(evalgamepad.GAMEAREA_MAX_X - 180, 50, player2, painter)
# Indicate Button states
draw_buttonstate(60, 50, player1, painter)
draw_buttonstate(evalgamepad.GAMEAREA_MAX_X - 180, 50, player2, painter)
elif game["state"] == evalgamepad.STATE_GAMERESULTS: # Display winner of this game
painter.setFont(self.fntMedium)
if player1["score"] > 9:
msg = "Game player #1"
else:
msg = "Game player #2"
painter.drawText(50, int(evalgamepad.GAMEAREA_MAX_Y / 2), msg)
elif game["state"] == evalgamepad.STATE_FINALRESULTS: # Display set winner
painter.setFont(self.fntMedium)
if game["p1_game"] > 2:
msg = "Match player #1"
else:
msg = "Match player #2"
painter.drawText(50, int(evalgamepad.GAMEAREA_MAX_Y / 2), msg)
elif game["state"] == evalgamepad.STATE_EXIT: # Indicate good bye ...
painter.setFont(self.fntMedium)
painter.drawText(50, int(evalgamepad.GAMEAREA_MAX_Y / 2), "Bye!")
@pyqtSlot()
def on_timer(self):
'''Qt5 timer event'''
global ball, player1, player2
global game, ball, player1, player2
self.timer.stop() # Block overrun
if pong_game(ball, player1, player2) == False:
if pong_game(game, ball, player1, player2) == False:
sys.exit(0)
self.update() # Redraw
@ -193,7 +219,7 @@ if __name__ == '__main__':
try:
# Initialize
random.seed()
evalgamepad.init_gamearea()
game = evalgamepad.init_game()
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)
player2 = evalgamepad.init_player(sys.argv[2], evalgamepad.GAMEAREA_MAX_X-40, evalgamepad.GAMEAREA_MAX_Y/2-50, 20, 160)
Loading…
Cancel
Save