Moving objects now ...

pull/2/head
kaqu 2 years ago
parent eb28d884d0
commit c3947790c8
  1. 1
      .gitignore
  2. 3
      .vscode/settings.json
  3. 4
      evalgamepad.py
  4. BIN
      pong_background.png
  5. 223
      simple_pong.py

1
.gitignore vendored

@ -0,0 +1 @@
__pycache__/*

@ -0,0 +1,3 @@
{
"python.pythonPath": "/usr/bin/python"
}

@ -144,8 +144,8 @@ def eval_gamepad(player):
def eval_position(player):
bChanged = False
sum_x = player["x"] + player["delta_x"]/255
sum_y = player["y"] + player["delta_y"]/255
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

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.1 MiB

@ -0,0 +1,223 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
simple_pong.py
Simple python/Qt5 pong clone
History:
--------
21.10.20/KQ Initial version
"""
import sys, os, fcntl, time
import os
import time
#from playsound import playsound
from PyQt5.QtGui import (QPixmap, QPaintDevice, QImage, QMouseEvent, QColor)
from PyQt5.QtWidgets import *
from PyQt5.QtCore import Qt, QTimer, pyqtSlot, QEvent
from PyQt5.QtGui import QPainter, QBrush, QPen
import evalgamepad
# Buttons in 'state<n>'
BTN_TRIGGER = 1 # [Blue/X]
BTN_THUMB = 2 # [Red/A]
BTN_THUMB2 = 4 # [Yellow/B]
BTN_TOP = 8 # [Green/Y]
BTN_TOP2 = 16 # [Frontal left]
BTN_PINKIE = 32 # [Frontal right]
BTN_BASE3 = 64 # [Centre left]
BTN_BASE4 = 128 # [Centre right]
GAMEAREA_MIN_X = 0 # Game area definition
GAMEAREA_MAX_X = 1920 # Assume FHD resolution
GAMEAREA_MIN_Y = 0
GAMEAREA_MAX_Y = 1080
ball_x = 0
ball_y = 0
"""
class pongQT5BaseClass(QWidget):
""Pong GUI""
def __init__(self):
""Qt5 requirement""
super().__init__() # Init base classes
self.initUI() # Init our own stuff
def initUI(self):
""Initialize Qt5 & pong setup""
qlblBall = QLabel(self)
#q.setAutoFillBackground(False) # No automatic filling
#q.RenderFlags = 0 # Disable automatic rendering
#q.WA_OpaquePaintEvent = True # We paint over everything ourself
#q.WA_PaintOnScreen = True # No parent background propagation please!
#q.WA_NoSystemBackground = True # No parent background propagation please!
# Headline
self.qpbHeader = QPushButton("Ball")
f = self.qpbHeader.font()
f.setPointSize(32)
self.qpbHeader.setFont(f)
#self.qpbHeader.setFrameStyle(QFrame.Panel or QFrame.Sunken)
#self.qpbHeader.setTextFormat(Qt.PlainText)
#self.qpbHeader.setText("Boat Detector")
#self.qpbHeader.setAlignment(Qt.AlignCenter or Qt.AlignHCenter)
p = self.qpbHeader.palette()
p.setColor(self.qpbHeader.backgroundRole(), Qt.darkCyan)
p.setColor(self.qpbHeader.foregroundRole(), Qt.lightGray)
self.qpbHeader.setPalette(p)
self.qpbHeader.autoFillBackground()
self.addWidget(self.qpbHeader,1)
self.move(300, 200) # Upper left corner
self.setWindowTitle("Simple Pong")
self.Width = 1024 # Indicate non-valid entry for 1st event
self.Height = 800
# Modify main window background
pW = self.qpbHeader.parentWidget()
p = pW.palette()
p.setColor(pW.backgroundRole(), QColor(8,8,0,255)) #Qt.darkGray)
pW.setPalette(p)
pW.setAutoFillBackground(True)
#self.show()
self.showFullScreen()
#self.resize(res_x, res_y)
self.timer = QTimer() # Start processing 'loop'
self.timer.setInterval(50) # 50ms
self.timer.setTimerType(Qt.PreciseTimer)
self.timer.timeout.connect(self.on_timer)
self.timer.start()
# if bBoatDetected:
# playsound('/usr/lib/libreoffice/share/gallery/sounds/laser.wav')
@pyqtSlot()
def on_timer(self):
''Qt5 timer event''
self.timer.stop() # Block overrun
# Do something
self.timer.start() # Re-enable
# end class
"""
def pong_game(ball, player1, player2):
# 1. Retrieve user entries
bChanged = evalgamepad.eval_gamepad(player1)
if bChanged:
if (player1["state"] & BTN_BASE4) > 0: # Exit?
print("Player #1 aborted game")
return False
else:
print("Player #1: {}h".format(hex(player1["state"])))
bChanged = evalgamepad.eval_gamepad(player2) # Exit?
if bChanged:
if (player2["state"] & BTN_BASE4) > 0:
print("Player #2 aborted game")
return False
else:
print("Player #2: {}h".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. Adjust object positions
evalgamepad.eval_object(ball)
# 4. Refresh game area drawing
#evalgamepad.draw_objects(ball, player1, player2)
return True
class pongWindow(QMainWindow):
def __init__(self):
super().__init__()
self.title = "Simple Pong"
self.top= 0
self.left= 0
self.width = 1920
self.height = 1080
self.InitWindow()
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.show()
self.timer = QTimer() # Start processing 'loop'
self.timer.setInterval(25) # 50ms
self.timer.setTimerType(Qt.PreciseTimer)
self.timer.timeout.connect(self.on_timer)
self.timer.start()
def paintEvent(self, event):
global ball, player1, player2
painter = QPainter(self)
painter.setPen(QPen(Qt.white, 1, Qt.SolidLine)) # Circle: Color/Linewidth/Pattern
painter.setBrush(QBrush(Qt.white, Qt.SolidPattern)) # Fill
pic = QPixmap("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(GAMEAREA_MAX_X - 30, player2["y"], 20, 100) # Player right
@pyqtSlot()
def on_timer(self):
'''Qt5 timer event'''
global ball, player1, player2
self.timer.stop() # Block overrun
if pong_game(ball, player1, player2) == False:
sys.exit(0)
self.update() # Redraw
self.timer.start() # Re-enable
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])
except IOError as e:
import errno
if e.errno == errno.EACCES:
print("Insufficient permissions to access {} or {}".format(path1, path2))
elif e.errno == errno.ENOENT:
print("Device {} or {} does not exist".format(path1, path2))
else:
raise e
sys.exit(-1)
app = QApplication(sys.argv) # Eval command line args
window = pongWindow() # Create Qt5 GUI
rc = app.exec_() # & run logic
# Quit gracefully
evalgamepad.exit_player(player2)
evalgamepad.exit_player(player1)
sys.exit(rc)
Loading…
Cancel
Save