void Pong::slotDataAvailable() { QByteArray array = mPort->readAll(); switch(array[0]) { case InitGame: { mSeed = array.right(array.size() - 1).toUInt(); qsrand(mSeed); mThisPIsLeftP = false; slotNewGame(false); break; } case StartGame: { slotStartGame(false); break; } case NewGame: { slotNewGame(false); break; } case StopGame: { slotStopGame(false); break; } case MoveSecondPlayerUp: { if (mThisPIsLeftP) { mRightP->r->moveBy(0, -mPaddleInc); } else { mLeftP->r->moveBy(0, -mPaddleInc); } checkIfPlayersGoOutOfMap(); break; } case MoveSecondPlayerDown: { if (mThisPIsLeftP) { mRightP->r->moveBy(0, mPaddleInc); } else { mLeftP->r->moveBy(0, mPaddleInc); } checkIfPlayersGoOutOfMap(); break; } } }
// socat -d -d pty,raw,echo=0 pty,raw,echo=0 void Pong::slotSerialMode() { mMode = MODE_SERIAL; //Zobrazenie formulára pre používateľa. SetPortForm pf((QWidget *)(this->parent())); if (!(pf.exec() == QDialog::Accepted)) return; //Inicializácia objektu reprezentujúceho port pre sériovú komunikáciu. mPort = new QextSerialPort(pf.getPortName().trimmed()); if (!mPort->open(QIODevice::ReadWrite)) { qDebug() << "Cannot open serial port"; return; } //Zavedenie slotu - Ak sú dáta pripravené na čítanie z portu, //zavolaj funkciu slotDataAvailable connect(mPort, SIGNAL(readyRead()), this, SLOT(slotDataAvailable())); //Nastav port na základe hodnôt získaných od používateľa. mPort->setBaudRate(pf.getBaudRate()); mPort->setStopBits(pf.getStopBits()); mPort->setParity(pf.getParity()); mPort->setFlowControl(pf.getFlow()); //Pošli zariadeniu na druhej strane portu informáciu o tom že //začala nová hra a seed ktorý vygenerovala táto aplikácia mPort->write(QByteArray(1, InitGame).append(QString::number(mSeed))); slotNewGame(false); }
void KAstTopLevel::keyReleaseEvent( QKeyEvent *event ) { if ( event->isAutoRepeat() || !actions.contains( event->key() ) ) { event->ignore(); return; } Action a = actions[ event->key() ]; switch ( a ) { case RotateLeft: view->rotateLeft( FALSE ); break; case RotateRight: view->rotateRight( FALSE ); break; case Thrust: view->thrust( FALSE ); break; case Shoot: view->shoot( FALSE ); break; case Brake: view->brake( FALSE ); break; case Shield: view->setShield( FALSE ); break; case Teleport: view->teleport( FALSE ); break; case Launch: if ( waitShip ) { view->newShip(); waitShip = FALSE; view->hideText(); } else { event->ignore(); return; } break; case NewGame: slotNewGame(); break; /* case Pause: { view->pause( TRUE ); QMessageBox::information( this, tr("KAsteroids is paused"), tr("Paused") ); view->pause( FALSE ); } break; */ default: event->ignore(); return; } event->accept(); }
MainWindow::MainWindow(QWidget *parent, const char* name, WFlags /*fl*/) : KMainWindow(parent, name, WStyle_NoBorder) { m_clickcount = 0; contrdirs[Cell::U] = Cell::D; contrdirs[Cell::R] = Cell::L; contrdirs[Cell::D] = Cell::U; contrdirs[Cell::L] = Cell::R; KNotifyClient::startDaemon(); KStdGameAction::gameNew(this, SLOT(slotNewGame()), actionCollection()); KStdGameAction::highscores(this, SLOT(showHighscores()), actionCollection()); KStdGameAction::quit(this, SLOT(close()), actionCollection()); KStdGameAction::configureHighscores(this, SLOT(configureHighscores()), actionCollection()); m_levels = KStdGameAction::chooseGameType(0, 0, actionCollection()); QStringList lst; lst += i18n("Novice"); lst += i18n("Normal"); lst += i18n("Expert"); lst += i18n("Master"); m_levels->setItems(lst); setFixedSize(minimumSizeHint()); statusBar()->insertItem("abcdefghijklmnopqrst: 0 ",1); setAutoSaveSettings(); createGUI(); connect(m_levels, SIGNAL(activated(int)), this, SLOT(newGame(int))); QWhatsThis::add(this, i18n("<h3>Rules of the Game</h3>" "<p>You are the system administrator and your goal" " is to connect each computer to the central server." "<p>Click the right mouse button to turn the cable" " in a clockwise direction, and the left mouse button" " to turn it in a counter-clockwise direction." "<p>Start the LAN with as few turns as possible!")); //const int cellsize = KGlobal::iconLoader()->loadIcon("knetwalk/background.png", KIcon::User, 32).width(); const int cellsize = 32; const int gridsize = cellsize * MasterBoardSize + 2; QGrid* grid = new QGrid(MasterBoardSize, this); grid->setFrameStyle(QFrame::Panel | QFrame::Sunken); grid->setFixedSize(gridsize, gridsize); setCentralWidget(grid); Cell::initPixmaps(); for(int i = 0; i < MasterBoardSize * MasterBoardSize; i++) { board[i] = new Cell(grid, i); board[i]->setFixedSize(cellsize, cellsize); connect(board[i], SIGNAL(lClicked(int)), SLOT(lClicked(int))); connect(board[i], SIGNAL(rClicked(int)), SLOT(rClicked(int))); connect(board[i], SIGNAL(mClicked(int)), SLOT(mClicked(int))); } srand(time(0)); slotNewGame(); }
Pong::Pong(QRect rect, QObject * parent) : QGraphicsScene(rect, parent) { setBackgroundBrush(QBrush(Qt::black)); mSeed = (uint) QTime::currentTime().msec(); mRect = rect; mMode = MODE_SINGLE; mThisPIsLeftP = true; mStoppable = false; mPort = NULL; mStirr = 15; mStirrAngle = 135; mSpeedX = 10; mSpeedY = 10; mMaxSpeedX = 20; mMaxSpeedY = 20; mMinSpeedX = 1; mMinSpeedY = 1; mFrequency = 35; mActionPeriod = 15; mNumberOfBalls = 5; mPoints = 10; mPaddleHeight = 96; mPaddleInc = mPaddleHeight / 1.5; mLeftP = new Player(); mLeftP->r = new QGraphicsRectItem(0, 0, mRect.width() / 25, mRect.height() / 5); mLeftP->r->setPos(mLeftP->r->x(), mRect.height() / 2 - mLeftP->r->rect().height() / 2); mLeftP->r->setBrush(QBrush(PLAYER_BRUSH_COLOR)); addItem(mLeftP->r); mRightP = new Player(); mRightP->r = new QGraphicsRectItem(0, 0, mRect.width() / 25, mRect.height() / 5); mRightP->r->setPos(mRect.width() - mRightP->r->rect().width(), mRect.height() / 2 - mRightP->r->rect().height() / 2); mRightP->r->setBrush(QBrush(PLAYER_BRUSH_COLOR)); addItem(mRightP->r); dsmax = (qreal) (mMaxSpeedX - mSpeedX) / (mActionPeriod * mFrequency); dsmin = (qreal) (mSpeedX - mMinSpeedX) / (mActionPeriod * mFrequency); mScoreTextGroup = new QGraphicsItemGroup(); mSeparator = new QGraphicsSimpleTextItem(); mSeparator->setText(":"); mSeparator->setPen(QPen(Qt::black)); mSeparator->setBrush(QBrush(Qt::white)); mSeparator->setFont(QFont("Comic Sans", 25, QFont::Black)); mSeparator->setPos(mRect.width() / 2 - mSeparator->boundingRect().width() / 2, 0); mScoreTextGroup->addToGroup(mSeparator); mLeftPScoreText = new QGraphicsSimpleTextItem(); mLeftPScoreText->setText("0"); mLeftPScoreText->setPen(QPen(Qt::black)); mLeftPScoreText->setBrush(QBrush(Qt::white)); mLeftPScoreText->setFont(QFont("Comic Sans", 25, QFont::Black)); mLeftPScoreText->setPos(mRect.width() / 2 - mLeftPScoreText->boundingRect().width() - mSeparator->boundingRect().width() / 2, 0); mScoreTextGroup->addToGroup(mLeftPScoreText); mRightPScoreText = new QGraphicsSimpleTextItem(); mRightPScoreText->setText("0"); mRightPScoreText->setPen(QPen(Qt::black)); mRightPScoreText->setBrush(QBrush(Qt::white)); mRightPScoreText->setFont(QFont("Comic Sans", 25, QFont::Black)); mRightPScoreText->setPos(mRect.width() / 2 + mSeparator->boundingRect().width() / 2, 0); mScoreTextGroup->addToGroup(mRightPScoreText); addItem(mScoreTextGroup); mTimer = new QTimer(this); connect(mTimer, SIGNAL(timeout()), this, SLOT(slotUpdate())); mActionTimer = new QTimer(this); connect(mActionTimer, SIGNAL(timeout()), this, SLOT(slotAction())); slotNewGame(); }