summaryrefslogtreecommitdiffstats
path: root/examples
diff options
context:
space:
mode:
authorAndreas Buhr <andreas@andreasbuhr.de>2021-03-02 15:25:19 +0100
committerAndreas Buhr <andreas@andreasbuhr.de>2021-03-05 15:21:16 +0100
commitadbeef4d07cb49fa2f4b86582dfb61b545433ecb (patch)
treeb0e3a1ee973c37990bd502f16ca44484d4ad1c02 /examples
parent4f3b347ac71491db7da246b728aaaf7e0c13af58 (diff)
Pingpong example: Gamelogic works on unit square
The two parties playing pingpong agreed on a screen size in the past. This patch changes the logic to work on the unit square (0,1)x(0,1). Visualization scales this up to window size. This patch also simplifies the position update loop Change-Id: Ie89b1eaa5fc14daf6b56cd42458a16ae0a923568 Reviewed-by: Alex Blasche <alexander.blasche@qt.io>
Diffstat (limited to 'examples')
-rw-r--r--examples/bluetooth/pingpong/assets/Board.qml21
-rw-r--r--examples/bluetooth/pingpong/pingpong.cpp143
-rw-r--r--examples/bluetooth/pingpong/pingpong.h40
3 files changed, 66 insertions, 138 deletions
diff --git a/examples/bluetooth/pingpong/assets/Board.qml b/examples/bluetooth/pingpong/assets/Board.qml
index 4409d2e6..ff29bceb 100644
--- a/examples/bluetooth/pingpong/assets/Board.qml
+++ b/examples/bluetooth/pingpong/assets/Board.qml
@@ -144,8 +144,8 @@ Rectangle {
height: leftblock.width
radius: width/2
color: "#363636"
- x: pingPong.ballX
- y: pingPong.ballY
+ x: pingPong.ballX * scaleFactor
+ y: pingPong.ballY * scaleFactor
}
}
@@ -175,23 +175,20 @@ Rectangle {
}
property double leftBlockY: leftblock.y
- onLeftBlockYChanged: pingPong.updateLeftBlock(leftblock.y)
+ onLeftBlockYChanged: pingPong.updateLeftBlock(leftblock.y / scaleFactor)
property double leftBlockUpdate: pingPong.leftBlockY
- onLeftBlockUpdateChanged: leftblock.y = pingPong.leftBlockY
+ onLeftBlockUpdateChanged: leftblock.y = pingPong.leftBlockY * scaleFactor
property double rightBlockY: rightblock.y
- onRightBlockYChanged: pingPong.updateRightBlock(rightblock.y)
+ onRightBlockYChanged: pingPong.updateRightBlock(rightblock.y / scaleFactor)
property double rightBlockUpdate: pingPong.rightBlockY
- onRightBlockUpdateChanged: rightblock.y = pingPong.rightBlockY
+ onRightBlockUpdateChanged: rightblock.y = pingPong.rightBlockY * scaleFactor
+
Component.onCompleted: {
- if (menulist.height == Screen.height && menulist.width == Screen.width)
- pingPong.setSize(Screen.width, Screen.height)
- else
- pingPong.setSize(board.width, board.height)
- pingPong.updateLeftBlock(leftblock.y)
- pingPong.updateRightBlock(rightblock.y)
+ pingPong.updateLeftBlock(leftblock.y / scaleFactor)
+ pingPong.updateRightBlock(rightblock.y / scaleFactor)
}
}
diff --git a/examples/bluetooth/pingpong/pingpong.cpp b/examples/bluetooth/pingpong/pingpong.cpp
index 19683d30..5fa0ceba 100644
--- a/examples/bluetooth/pingpong/pingpong.cpp
+++ b/examples/bluetooth/pingpong/pingpong.cpp
@@ -54,9 +54,7 @@
#include <QtAndroid>
#endif
-PingPong::PingPong():
- m_serverInfo(0), socket(0), discoveryAgent(0), interval(5), m_resultLeft(0), m_resultRight(0),
- m_showDialog(false), m_role(0), m_proportionX(0), m_proportionY(0), m_serviceFound(false)
+PingPong::PingPong()
{
m_timer = new QTimer(this);
connect(m_timer, &QTimer::timeout, this, &PingPong::update);
@@ -75,10 +73,7 @@ void PingPong::startGame()
m_showDialog = false;
emit showDialogChanged();
//! [Start the game]
- if (m_role == 1)
- updateDirection();
-
- m_timer->start(50);
+ m_timer->start(10);
//! [Start the game]
}
@@ -89,10 +84,8 @@ void PingPong::update()
//! [Updating coordinates]
if (m_role == 1) {
checkBoundaries();
- m_ballPreviousX = m_ballX;
- m_ballPreviousY = m_ballY;
- m_ballY = m_direction*(m_ballX+interval) - m_direction*m_ballX + m_ballY;
- m_ballX = m_ballX + interval;
+ m_ballY += m_speedy;
+ m_ballX += m_speedx;
size.setNum(m_ballX);
size.append(' ');
@@ -114,19 +107,6 @@ void PingPong::update()
//! [Updating coordinates]
}
-
-
-void PingPong::setSize(const float &x, const float &y)
-{
- m_boardWidth = x;
- m_boardHeight = y;
- m_targetX = m_boardWidth;
- m_targetY = m_boardHeight/2;
- m_ballPreviousX = m_ballX = m_boardWidth/2;
- m_ballPreviousY = m_ballY = m_boardHeight - m_boardWidth/54;
- emit ballChanged();
-}
-
void PingPong::updateBall(const float &bX, const float &bY)
{
m_ballX = bX;
@@ -145,38 +125,33 @@ void PingPong::updateRightBlock(const float &rY)
void PingPong::checkBoundaries()
{
- float ballWidth = m_boardWidth/54;
- float blockSize = m_boardWidth/27;
- float blockHeight = m_boardHeight/5;
+ float ballWidth = 1. / 27;
+ float blockSize = 1. / 27;
+ float blockHeight = 1. / 5;
+
+ float ballCenterY = m_ballY + ballWidth / 2.;
//! [Checking the boundaries]
- if (((m_ballX + ballWidth) > (m_boardWidth - blockSize)) && ((m_ballY + ballWidth) < (m_rightBlockY + blockHeight))
- && (m_ballY > m_rightBlockY)) {
- m_targetY = 2 * m_ballY - m_ballPreviousY;
- m_targetX = m_ballPreviousX;
- interval = -5;
- updateDirection();
- }
- else if ((m_ballX < blockSize) && ((m_ballY + ballWidth) < (m_leftBlockY + blockHeight))
- && (m_ballY > m_leftBlockY)) {
- m_targetY = 2 * m_ballY - m_ballPreviousY;
- m_targetX = m_ballPreviousX;
- interval = 5;
- updateDirection();
- }
- else if (m_ballY < 0 || (m_ballY + ballWidth > m_boardHeight)) {
- m_targetY = m_ballPreviousY;
- m_targetX = m_ballX + interval;
- updateDirection();
+ if (((m_ballX + ballWidth) > (1. - blockSize)) && (ballCenterY < (m_rightBlockY + blockHeight))
+ && (ballCenterY > m_rightBlockY)) {
+ // right paddle collision
+ m_speedx = -std::abs(m_speedx);
+ } else if ((m_ballX < blockSize) && (ballCenterY < (m_leftBlockY + blockHeight))
+ && (ballCenterY > m_leftBlockY)) {
+ // left paddle collision
+ m_speedx = std::abs(m_speedx);
+ } else if (m_ballY < 0) {
+ m_speedy = std::abs(m_speedy);
+ } else if (m_ballY + ballWidth > 1.f) {
+ m_speedy = -std::abs(m_speedy);
}
//! [Checking the boundaries]
- else if ((m_ballX + ballWidth) > m_boardWidth) {
+ else if ((m_ballX + ballWidth) > 1.f) {
m_resultLeft++;
- m_targetX = m_boardWidth;
- m_targetY = m_boardHeight/2;
- m_ballPreviousX = m_ballX = m_boardWidth/2;
- m_ballPreviousY = m_ballY = m_boardHeight - m_boardWidth/54;
+ m_speedx = -0.002;
+ m_speedy = -0.002;
+ m_ballX = 0.5f;
+ m_ballY = 0.9f;
- updateDirection();
checkResult();
QByteArray result;
result.append("result ");
@@ -190,14 +165,13 @@ void PingPong::checkBoundaries()
socket->write(result);
qDebug() << result;
emit resultChanged();
- }
- else if (m_ballX < 0) {
+ } else if (m_ballX < 0) {
m_resultRight++;
- m_targetX = 0;
- m_targetY = m_boardHeight/2;
- m_ballPreviousX = m_ballX = m_boardWidth/2;
- m_ballPreviousY = m_ballY = m_boardHeight - m_boardWidth/54;
- updateDirection();
+ m_speedx = 0.002;
+ m_speedy = -0.002;
+ m_ballX = 0.5f;
+ m_ballY = 0.9f;
+
checkResult();
QByteArray result;
result.append("result ");
@@ -233,11 +207,6 @@ void PingPong::checkResult()
}
}
-void PingPong::updateDirection()
-{
- m_direction = (m_targetY - m_ballY)/(m_targetX - m_ballX);
-}
-
void PingPong::startServer()
{
setMessage(QStringLiteral("Starting the server"));
@@ -308,17 +277,8 @@ void PingPong::clientConnected()
this, &PingPong::socketError);
//! [Initiating server socket]
- setMessage(QStringLiteral("Client connected."));
-
- QByteArray size;
- size.setNum(m_boardWidth);
- size.append(' ');
- QByteArray size1;
- size1.setNum(m_boardHeight);
- size.append(size1);
- size.append(" \n");
- socket->write(size.constData());
-
+ setMessage(QStringLiteral("Client connected. Get ready!"));
+ QTimer::singleShot(3000, this, SLOT(startGame()));
}
void PingPong::clientDisconnected()
@@ -377,15 +337,8 @@ QString PingPong::message() const
void PingPong::serverConnected()
{
- setMessage("Server Connected");
- QByteArray size;
- size.setNum(m_boardWidth);
- size.append(' ');
- QByteArray size1;
- size1.setNum(m_boardHeight);
- size.append(size1);
- size.append(" \n");
- socket->write(size.constData());
+ setMessage("Server Connected. Get ready!");
+ QTimer::singleShot(3000, this, SLOT(startGame()));
}
void PingPong::serverDisconnected()
@@ -415,34 +368,22 @@ void PingPong::readSocket()
}
}
}
- if ((m_proportionX == 0 || m_proportionY == 0)) {
- QList<QByteArray> boardSize = line.split(sep);
- if (boardSize.size() > 1) {
- QByteArray boardWidth = boardSize.at(0);
- QByteArray boardHeight = boardSize.at(1);
- m_proportionX = m_boardWidth/boardWidth.toFloat();
- m_proportionY = m_boardHeight/boardHeight.toFloat();
- setMessage("Screen adjusted. Get ready!");
- QTimer::singleShot(3000, this, SLOT(startGame()));
- }
- }
- else if (m_role == 1) {
+ if (m_role == 1) {
QList<QByteArray> boardSize = line.split(sep);
if (boardSize.size() > 1) {
QByteArray rightBlockY = boardSize.at(0);
- m_rightBlockY = m_proportionY * rightBlockY.toFloat();
+ m_rightBlockY = rightBlockY.toFloat();
emit rightBlockChanged();
}
- }
- else if (m_role == 2) {
+ } else if (m_role == 2) {
QList<QByteArray> boardSize = line.split(sep);
if (boardSize.size() > 2) {
QByteArray ballX = boardSize.at(0);
QByteArray ballY = boardSize.at(1);
QByteArray leftBlockY = boardSize.at(2);
- m_ballX = m_proportionX * ballX.toFloat();
- m_ballY = m_proportionY * ballY.toFloat();
- m_leftBlockY = m_proportionY * leftBlockY.toFloat();
+ m_ballX = ballX.toFloat();
+ m_ballY = ballY.toFloat();
+ m_leftBlockY = leftBlockY.toFloat();
emit leftBlockChanged();
emit ballChanged();
}
diff --git a/examples/bluetooth/pingpong/pingpong.h b/examples/bluetooth/pingpong/pingpong.h
index 315f270b..8a813005 100644
--- a/examples/bluetooth/pingpong/pingpong.h
+++ b/examples/bluetooth/pingpong/pingpong.h
@@ -81,7 +81,6 @@ public:
float leftBlockY() const;
float rightBlockY() const;
void checkBoundaries();
- void updateDirection();
bool showDialog() const;
QString message() const;
void setMessage(const QString &message);
@@ -93,7 +92,6 @@ public:
public slots:
void startGame();
void update();
- void setSize(const float &x, const float &y);
void updateBall(const float &bX, const float &bY);
void updateLeftBlock(const float &lY);
void updateRightBlock(const float &rY);
@@ -119,32 +117,24 @@ Q_SIGNALS:
void resultChanged();
private:
- QBluetoothServer *m_serverInfo;
+ QBluetoothServer *m_serverInfo = nullptr;
QBluetoothServiceInfo m_serviceInfo;
- QBluetoothSocket *socket;
- QBluetoothServiceDiscoveryAgent *discoveryAgent;
+ QBluetoothSocket *socket = nullptr;
+ QBluetoothServiceDiscoveryAgent *discoveryAgent = nullptr;
- float m_ballX;
- float m_ballY;
- float m_ballPreviousX;
- float m_ballPreviousY;
- float m_leftBlockY;
- float m_rightBlockY;
- QTimer *m_timer;
- float m_boardWidth;
- float m_boardHeight;
- float m_direction;
- float m_targetX;
- float m_targetY;
- int interval;
- int m_resultLeft;
- int m_resultRight;
- bool m_showDialog;
+ float m_ballX = 0.5f;
+ float m_ballY = 0.9f;
+ float m_leftBlockY = 0.4f;
+ float m_rightBlockY = 0.4f;
+ QTimer *m_timer = nullptr;
+ float m_speedy = 0.002f;
+ float m_speedx = 0.002f;
+ int m_resultLeft = 0;
+ int m_resultRight = 0;
+ bool m_showDialog = false;
QString m_message;
- int m_role;
- float m_proportionX;
- float m_proportionY;
- bool m_serviceFound;
+ int m_role = 0;
+ bool m_serviceFound = false;
};
#endif // PINGPONG_H