void Board::checkBall(int x, int y) { int ly = leftPaddle->y(); int ry = rightPaddle->y(); if (x > 646) emit scored(Right); else if (x < -6) emit scored(Left); if (y < 18) emit ballCollision(Top); else if (y > 360-18) emit ballCollision(Bottom); if ((x < 18) && (y > ly-6) && (y < ly+Paddle+6)) emit ballCollision(Left); if ((x > 640-18) && (y > ry-6) && (y < ry+Paddle+6)) emit ballCollision(Right); }
void Game::update(){ animateColors(); if(ball.bounces > 3){ ball.vx *= 1.2; ball.bounces = 0; } ball.move(duration); ballCollision(); //Check for Points on each side made if(ball.x < 0 - abs(ball.vx)){ //Because vx is negative abs() needed here because if the Ball turns it would be a point for the enemy rightPoints++; ball.x = width /2; ball.y = height/2; ball.old_x = width/2; ball.old_y = height/2; ball.vx = -5; ball.vy = 0; } else if(ball.x > 31 + abs(ball.vx)){ leftPoints++; ball.x = width /2; ball.y = height/2; ball.old_x = width/2; ball.old_y = height/2; ball.vx = 5; ball.vy = 0; } }
void Board::sceneChanged(const QList<QRectF> ®ion) { Q_UNUSED(region); QList<QGraphicsItem *>items = scene->collidingItems(ball); while (!items.empty()) { QGraphicsItem *i = items.takeFirst(); if (i == topWall) emit ballCollision(Top); if (i == bottomWall) emit ballCollision(Bottom); if (i == leftPaddle) emit ballCollision(Left); if (i == rightPaddle) emit ballCollision(Right); } }
Tennis::Tennis(QWidget *parent) : QDialog(parent), ui(new Ui_Tennis), board(new Board), controller(new Controller), socket(0), m_discoveryAgent(new QBluetoothServiceDiscoveryAgent), m_handover(0) { // start Bluetooth if not started QBluetoothLocalDevice *device = new QBluetoothLocalDevice(); device->powerOn(); delete device; device = 0; //! [Construct UI] ui->setupUi(this); isClient = false; isConnected = false; quickDiscovery = true; #if defined (Q_OS_SYMBIAN) || defined(Q_OS_WINCE) || defined(Q_WS_MAEMO_5) || defined(Q_WS_MAEMO_6) setWindowState(Qt::WindowMaximized); #endif ui->pongView->setScene(board->getScene()); connect(ui->pongView, SIGNAL(mouseMove(int, int)), this, SLOT(mouseMove(int, int))); ui->pongView->setMouseTracking(false); connect(board, SIGNAL(ballCollision(Board::Edge)), controller, SLOT(ballCollision(Board::Edge))); connect(board, SIGNAL(scored(Board::Edge)), controller, SLOT(scored(Board::Edge))); connect(controller, SIGNAL(moveBall(int,int)), board, SLOT(setBall(int,int))); connect(this, SIGNAL(moveLeftPaddle(int)), board, SLOT(setLeftPaddle(int))); connect(this, SIGNAL(moveRightPaddle(int)), board, SLOT(setRightPaddle(int))); connect(controller, SIGNAL(score(int,int)), board, SLOT(setScore(int,int))); connect(controller, SIGNAL(fps(const QString&)), this, SLOT(fps(const QString&))); setFocusPolicy(Qt::WheelFocus); paddle_pos = (Board::Height-12)/2-Board::Paddle/2; endPaddlePos = paddle_pos; emit moveLeftPaddle(paddle_pos); board->setRightPaddle(paddle_pos); server = new TennisServer(this); connect(controller, SIGNAL(moveBall(int,int)), server, SLOT(moveBall(int,int))); connect(controller, SIGNAL(score(int,int)), server, SLOT(score(int,int))); connect(this, SIGNAL(moveLeftPaddle(int)), server, SLOT(moveLeftPaddle(int))); connect(server, SIGNAL(clientConnected(QString)), this, SLOT(serverConnected(QString))); connect(server, SIGNAL(clientDisconnected(QString)), this, SLOT(serverDisconnected())); connect(server, SIGNAL(moveRightPaddle(int)), board, SLOT(setRightPaddle(int))); connect(server, SIGNAL(lag(int)), this, SLOT(lagReport(int))); connect(server, SIGNAL(clientConnected(QString)), controller, SLOT(refresh())); server->startServer(); client = new TennisClient(this); connect(client, SIGNAL(moveBall(int,int)), board, SLOT(setBall(int,int))); connect(client, SIGNAL(moveLeftPaddle(int)), board, SLOT(setLeftPaddle(int))); connect(client, SIGNAL(connected(QString)), this, SLOT(clientConnected(QString))); connect(client, SIGNAL(disconnected()), this, SLOT(clientDisconnected())); connect(this, SIGNAL(moveRightPaddle(int)), client, SLOT(moveRightPaddle(int))); connect(client, SIGNAL(score(int,int)), board, SLOT(setScore(int,int))); connect(client, SIGNAL(lag(int)), this, SLOT(lagReport(int))); connect(this, SIGNAL(moveLeftPaddle(int)), controller, SLOT(moveLeftPaddle(int))); connect(this, SIGNAL(moveRightPaddle(int)), controller, SLOT(moveRightPaddle(int))); connect(server, SIGNAL(moveRightPaddle(int)), controller, SLOT(moveRightPaddle(int))); // ui->pongView->setBackgroundBrush(QBrush(Qt::white)); ui->pongView->setCacheMode(QGraphicsView::CacheBackground); QNearFieldManager nearFieldManager; if (nearFieldManager.isAvailable()) { m_handover = new Handover(server->serverPort(), this); connect(m_handover, SIGNAL(bluetoothServiceChanged()), this, SLOT(nearFieldHandover())); connect(m_discoveryAgent, SIGNAL(serviceDiscovered(QBluetoothServiceInfo)), this, SLOT(serviceDiscovered(QBluetoothServiceInfo))); connect(m_discoveryAgent, SIGNAL(finished()), this, SLOT(discoveryFinished())); } m_discoveryAgent->setUuidFilter(QBluetoothUuid(serviceUuid)); QString address; QString port; QStringList args = QCoreApplication::arguments(); if(args.length() >= 2){ address = args.at(1); if(args.length() >= 3){ port = args.at(2); } } if(address.isEmpty()){ QSettings settings("QtDF", "bttennis"); address = settings.value("lastclient").toString(); } if(!address.isEmpty()){ qDebug() << "Connect to" << address << port; QBluetoothDeviceInfo device = QBluetoothDeviceInfo(QBluetoothAddress(address), "", QBluetoothDeviceInfo::ComputerDevice); QBluetoothServiceInfo service; if (!port.isEmpty()) { QBluetoothServiceInfo::Sequence protocolDescriptorList; QBluetoothServiceInfo::Sequence protocol; protocol << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::L2cap)) << QVariant::fromValue(port.toUShort()); protocolDescriptorList.append(QVariant::fromValue(protocol)); service.setAttribute(QBluetoothServiceInfo::ProtocolDescriptorList, protocolDescriptorList); qDebug() << "port" << port.toUShort() << service.protocolServiceMultiplexer(); } else { service.setServiceUuid(QBluetoothUuid(serviceUuid)); } service.setDevice(device); client->startClient(service); board->setStatus("Connecting", 100, 25); } else if (nearFieldManager.isAvailable()) { board->setStatus(tr("Touch to play"), 100, 25); } setEnabled(true); paddleAnimation = new QPropertyAnimation(this, "paddlePos", this); paddleAnimation->setEasingCurve(QEasingCurve::InOutQuad); ui->pongView->installEventFilter(this); }