Pretty ugly version ready ...

pull/2/head
kaqu 2 years ago
parent 817910a2a3
commit 9698defbb2
  1. 86
      evalgamepad.py
  2. 93
      pandemic_pong.py
  3. BIN
      pictures/pandemic_pong.png

@ -32,6 +32,14 @@ GAMEAREA_MAX_X = 1920 # Assume FHD resolution
GAMEAREA_MIN_Y = 15
GAMEAREA_MAX_Y = 940 #-GAMEAREA_MIN_Y
COL_WHITE = 0
COL_BLUE = 1
COL_GREEN = 2
COL_RED = 3
COL_YELLOW = 4
COLORDELAY = 100
def init_player(path, x, y, w, h):
# Gamepad init.
fd = open(path, "rb")
@ -44,20 +52,34 @@ def init_player(path, x, y, w, h):
"state" : 0, # Button mask (as above)
"delta_x" : 0, # Current delta setting
"delta_y" : 0,
"color" : COL_WHITE, # 'Infection' colour
"delay" : 0, # 'Infectious' time
"x" : x, # Current actual position
"y" : y, # Start at center
"w" : w,
"h" : h,
"score" : 0
"score" : 0
}
def reinit_player(player, x, y, delta_x, delta_y):
player["x"] = x
player["y"] = y
player["delta_x"] = delta_x
player["delta_y"] = delta_y
player["state"] = 0
player["color"] = COL_WHITE
player["delay"] = 0
def exit_player(player):
player["fd"].close()
def init_object(x, y, w, h, delta_x, delta_y):
return {
"delta_x" : delta_x, # Current movement
"delta_y" : (random.random() - 0.5) * 10,
"color" : COL_WHITE,
"delay" : 0,
"x" : x, # Current position
"y" : y,
"w" : w, # Size of object
@ -69,6 +91,8 @@ def reinit_object(obj, delta_x, delta_y):
obj["y"] = GAMEAREA_MAX_Y/2 - obj["h"]/2
obj["delta_x"] = delta_x
obj["delta_y"] = delta_y
obj["color"] = COL_WHITE
obj["delay"] = 0
def print_event(e):
@ -104,14 +128,27 @@ def eval_gamepad(player):
if e.type.value == 1: # Non-Cross events
if e.value == 1: # Simple buttons may act as toggles
if e.code.value == 288: # BTN_TRIGGER
player["state"] = player["state"] ^ BTN_TRIGGER
if player["delay"] <= 0: # As long as we're not infectious ...
player["state"] = BTN_TRIGGER # player["state"] ^ BTN_TRIGGER
player["color"] = COL_BLUE # Infect 'blue'
player["delay"] = COLORDELAY # Start infectious time immediately ...
elif e.code.value == 289: # BTN_THUMB
player["state"] = player["state"] ^ BTN_THUMB
if player["delay"] <= 0: # As long as we're not infectious ...
player["state"] = BTN_THUMB # player["state"] ^ BTN_THUMB
player["color"] = COL_RED
player["delay"] = COLORDELAY
elif e.code.value == 290: # BTN_THUMB2
player["state"] = player["state"] ^ BTN_THUMB2
if player["delay"] <= 0: # As long as we're not infectious ...
player["state"] = BTN_THUMB2 # player["state"] ^ BTN_THUMB2
player["color"] = COL_YELLOW
player["delay"] = COLORDELAY
elif e.code.value == 291: # BTN_TOP
player["state"] = player["state"] ^ BTN_TOP
elif e.code.value == 292: # BTN_TOP2
if player["delay"] <= 0: # As long as we're not infectious ...
player["state"] = BTN_TOP # player["state"] ^ BTN_TOP
player["color"] = COL_GREEN
player["delay"] = COLORDELAY
elif e.code.value == 292: # BTN_TOP2
player["state"] = player["state"] ^ BTN_TOP2
elif e.code.value == 293: # BTN_PINKIE
player["state"] = player["state"] ^ BTN_PINKIE
@ -152,26 +189,24 @@ def eval_gamepad(player):
return bChanged
def eval_position(player):
def eval_position(player, min_x, max_x):
bChanged = False
'''
sum_x = player["x"] + player["delta_x"] * 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"]
'''
if sum_x < min_x:
sum_x = min_x
elif sum_x + player["w"] > max_x:
sum_x = 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_y != player["y"]:
if (sum_x != player["x"]) or (sum_y != player["y"]):
bChanged = True
#player["x"] = sum_x
player["x"] = sum_x
player["y"] = sum_y
return bChanged
@ -266,8 +301,8 @@ def next_gamestate(ga, player1, player2):
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
reinit_player(player1, 10, GAMEAREA_MAX_Y/2-50, 0, 0)
reinit_player(player2, GAMEAREA_MAX_X-40, GAMEAREA_MAX_Y/2-50, 0, 0)
ga["p1_game"] = 0
ga["p2_game"] = 0
player1["score"] = 0
@ -275,6 +310,17 @@ def next_gamestate(ga, player1, player2):
ga["state"] = STATE_PLAY
elif ga["state"] == STATE_PLAY: # Remain playing 'til 10 points
if player1["delay"] > 0: # Infectious time running?
player1["delay"] = player1["delay"] - 1 # Yap!
if player1["delay"] <= 0:
player1["color"] = COL_WHITE
player1["state"] = 0
if player2["delay"] > 0: # for both players ...
player2["delay"] = player2["delay"] - 1
if player2["delay"] <= 0:
player2["color"] = COL_WHITE
player2["state"] = 0
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
@ -291,8 +337,8 @@ def next_gamestate(ga, player1, player2):
else:
player1["score"] = 0
player2["score"] = 0
player1["state"] = 0
player2["state"] = 0
reinit_player(player1, GAMEAREA_MAX_X-40, GAMEAREA_MAX_Y/2-50, 0, 0)
reinit_player(player2, 10, GAMEAREA_MAX_Y/2-50, 0, 0)
ga["state"] = STATE_PLAY # Nope, move on to next game
elif ga["state"] == STATE_FINALRESULTS:

@ -26,9 +26,15 @@ 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 #1 in range?
if (ball["x"] <= player1["x"] + player1["w"]) and (ball["x"] > player1["x"]): # 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?
if ball["color"] == evalgamepad.COL_WHITE:
ball["color"] = player1["color"] # Pick up 'infection' (if any)
elif ball["color"] != player1["color"]: # Infectious ball!
return # w/o vaccine -> no save!
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?
@ -44,10 +50,27 @@ def crashvectors(ball, player1, player2):
playsound('/usr/share/mint-artwork/sounds/logout.ogg') # Enforce caching ...
else: # Ball moves left->right (---->)
if ball["x"] + ball["w"] >= player2["x"]: # Right player #2 in range?
if (ball["x"] + ball["w"] >= player2["x"]) and (ball["x"] < player2["x"] + player2["w"]): # 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?
if ball["color"] == evalgamepad.COL_WHITE:
ball["color"] = player2["color"] # Pick up 'infection' (if any)
elif ball["color"] != player2["color"]: # Infectious ball!
return # w/o vaccine -> no save!
ball["delta_x"] = -ball["delta_x"] * 1.2 # Invert direction & speed up
if player2["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 player2["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 ...
@ -64,10 +87,10 @@ def pong_game(game, ball, player1, player2):
if game["state"] == evalgamepad.STATE_PLAY: # Actually playing?
# 2. Adjust player positions
bChanged = evalgamepad.eval_position(player1)
bChanged = evalgamepad.eval_position(player1, evalgamepad.GAMEAREA_MIN_X, evalgamepad.GAMEAREA_MAX_X/2-50)
#if bChanged:
# print("Player #1: ", player1["x"], " / ", player1["y"])
bChanged = evalgamepad.eval_position(player2)
bChanged = evalgamepad.eval_position(player2, evalgamepad.GAMEAREA_MAX_X/2+50, evalgamepad.GAMEAREA_MAX_X)
#if bChanged:
# print("Player #2: ", player2["x"], " / ", player2["y"])
@ -153,18 +176,76 @@ class pongWindow(QMainWindow):
#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.drawPixmap(self.rect(), QPixmap("pictures/pandemic_pong.png"))
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")
painter.drawText(int(evalgamepad.GAMEAREA_MAX_X / 2) + 300, 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 ...")
painter.drawText(300, int(evalgamepad.GAMEAREA_MAX_Y / 2), "Press [Start] ...")
painter.drawText(int(evalgamepad.GAMEAREA_MAX_X/2)+100, int(evalgamepad.GAMEAREA_MAX_Y / 5), "Press [color] button")
painter.drawText(int(evalgamepad.GAMEAREA_MAX_X/2)+100, int(evalgamepad.GAMEAREA_MAX_Y / 5 * 2), "to infect player,")
painter.drawText(int(evalgamepad.GAMEAREA_MAX_X/2)+100, int(evalgamepad.GAMEAREA_MAX_Y / 5 * 3), "ball will pickup virus!")
painter.drawText(int(evalgamepad.GAMEAREA_MAX_X/2)+100, int(evalgamepad.GAMEAREA_MAX_Y / 5 * 4), "Match color to deflect!")
elif game["state"] == evalgamepad.STATE_PLAY: # Actual play 'til score reaches 10 points
""" Ball gives NO indication ...
if ball["color"] == evalgamepad.COL_WHITE:
painter.setPen(QPen(Qt.white, 1, Qt.SolidLine)) # Color/Linewidth/Pattern
painter.setBrush(QBrush(Qt.white, Qt.SolidPattern)) # Fill
elif ball["color"] == evalgamepad.COL_BLUE:
painter.setPen(QPen(Qt.blue, 1, Qt.SolidLine)) # Color/Linewidth/Pattern
painter.setBrush(QBrush(Qt.blue, Qt.SolidPattern)) # Fill
elif ball["color"] == evalgamepad.COL_GREEN:
painter.setPen(QPen(Qt.green, 1, Qt.SolidLine)) # Color/Linewidth/Pattern
painter.setBrush(QBrush(Qt.green, Qt.SolidPattern)) # Fill
elif ball["color"] == evalgamepad.COL_RED:
painter.setPen(QPen(Qt.red, 1, Qt.SolidLine)) # Color/Linewidth/Pattern
painter.setBrush(QBrush(Qt.red, Qt.SolidPattern)) # Fill
elif ball["color"] == evalgamepad.COL_YELLOW:
painter.setPen(QPen(Qt.yellow, 1, Qt.SolidLine)) # Color/Linewidth/Pattern
painter.setBrush(QBrush(Qt.yellow, Qt.SolidPattern)) # Fill
"""
painter.drawRect(int(ball["x"]), int(ball["y"]), int(ball["w"]), int(ball["h"])) # Ball
if player1["color"] == evalgamepad.COL_WHITE:
painter.setPen(QPen(Qt.white, 1, Qt.SolidLine)) # Color/Linewidth/Pattern
painter.setBrush(QBrush(Qt.white, Qt.SolidPattern)) # Fill
elif player1["color"] == evalgamepad.COL_BLUE:
painter.setPen(QPen(Qt.blue, 1, Qt.SolidLine)) # Color/Linewidth/Pattern
painter.setBrush(QBrush(Qt.blue, Qt.SolidPattern)) # Fill
elif player1["color"] == evalgamepad.COL_GREEN:
painter.setPen(QPen(Qt.green, 1, Qt.SolidLine)) # Color/Linewidth/Pattern
painter.setBrush(QBrush(Qt.green, Qt.SolidPattern)) # Fill
elif player1["color"] == evalgamepad.COL_RED:
painter.setPen(QPen(Qt.red, 1, Qt.SolidLine)) # Color/Linewidth/Pattern
painter.setBrush(QBrush(Qt.red, Qt.SolidPattern)) # Fill
elif player1["color"] == evalgamepad.COL_YELLOW:
painter.setPen(QPen(Qt.yellow, 1, Qt.SolidLine)) # Color/Linewidth/Pattern
painter.setBrush(QBrush(Qt.yellow, Qt.SolidPattern)) # Fill
painter.drawRect(int(player1["x"]), int(player1["y"]), int(player1["w"]), int(player1["h"])) # Player left
if player2["color"] == evalgamepad.COL_WHITE:
painter.setPen(QPen(Qt.white, 1, Qt.SolidLine)) # Color/Linewidth/Pattern
painter.setBrush(QBrush(Qt.white, Qt.SolidPattern)) # Fill
elif player2["color"] == evalgamepad.COL_BLUE:
painter.setPen(QPen(Qt.blue, 1, Qt.SolidLine)) # Color/Linewidth/Pattern
painter.setBrush(QBrush(Qt.blue, Qt.SolidPattern)) # Fill
elif player2["color"] == evalgamepad.COL_GREEN:
painter.setPen(QPen(Qt.green, 1, Qt.SolidLine)) # Color/Linewidth/Pattern
painter.setBrush(QBrush(Qt.green, Qt.SolidPattern)) # Fill
elif player2["color"] == evalgamepad.COL_RED:
painter.setPen(QPen(Qt.red, 1, Qt.SolidLine)) # Color/Linewidth/Pattern
painter.setBrush(QBrush(Qt.red, Qt.SolidPattern)) # Fill
elif player2["color"] == evalgamepad.COL_YELLOW:
painter.setPen(QPen(Qt.yellow, 1, Qt.SolidLine)) # Color/Linewidth/Pattern
painter.setBrush(QBrush(Qt.yellow, Qt.SolidPattern)) # Fill
painter.drawRect(int(player2["x"]), int(player2["y"]), int(player2["w"]), int(player2["h"])) # Player right
painter.setPen(QPen(Qt.white, 1, Qt.SolidLine)) # Reset standard colour
painter.setBrush(QBrush(Qt.white, Qt.SolidPattern))
#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"]))

Binary file not shown.

After

Width:  |  Height:  |  Size: 78 KiB

Loading…
Cancel
Save