PiecesTable::PiecesTable(TQWidget* parent, const char* name ) : QtTableView(parent, name), _activeRow(-1), _activeCol(-1), _randomized(false) { _menu = new TQPopupMenu(this); _menu->insertItem(i18n("R&andomize Pieces"), this, TQT_SLOT(randomizeMap())); _menu->insertItem(i18n("&Reset Pieces"), this, TQT_SLOT(resetMap())); _menu->adjustSize(); // setup table view setFrameStyle(StyledPanel | Sunken); setBackgroundMode(NoBackground); setMouseTracking(true); setNumRows(4); setNumCols(4); // init arrays initMap(); initColors(); }
void PiecesTable::slotRandomize() { randomizeMap(); }
void PiecesTable::mousePressEvent(QMouseEvent* e) { QTableView::mousePressEvent(e); if (e->button() == RightButton) { // setup RMB pupup menu if(!_menu) { _menu = new QPopupMenu(this); _menu->insertItem(tr("R&andomize Pieces"), mRandomize); _menu->insertItem(tr("&Reset Pieces"), mReset); _menu->adjustSize(); } // execute RMB popup and check result switch(_menu->exec(mapToGlobal(e->pos()))) { case mRandomize: randomizeMap(); break; case mReset: initMap(); repaint(); break; default: break; } } else { // GAME LOGIC int cols = numCols(); int rows = numRows(); int item = cols*rows -1; // find the free position int pos = _map.find(item); if(pos < 0) return; int frow = pos / cols; int fcol = pos - frow * cols; // find click position int row = findRow(e->y()); int col = findCol(e->x()); // sanity check if (row < 0 || row >= rows) return; if (col < 0 || col >= cols) return; if ( row != frow && col != fcol ) return; // valid move? if(row != frow && col != fcol) return; // rows match -> shift pieces if(row == frow) { if (col < fcol) { for(int c = fcol; c > col; c--) { _map[c + row * cols] = _map[ c-1 + row *cols]; updateCell(row, c, false); } } else if (col > fcol) { for(int c = fcol; c < col; c++) { _map[c + row * cols] = _map[ c+1 + row *cols]; updateCell(row, c, false); } } } // cols match -> shift pieces else if (col == fcol) { if (row < frow) { for(int r = frow; r > row; r--) { _map[col + r * cols] = _map[ col + (r-1) *cols]; updateCell(r, col, false); } } else if (row > frow) { for(int r = frow; r < row; r++) { _map[col + r * cols] = _map[ col + (r+1) *cols]; updateCell(r, col, false); } } } // move free cell to click position _map[col + row * cols] = item; updateCell(row, col, false); // check if the player wins with this move checkwin(); } }
void getInput() { if (theShip.fuel > 0) { if (key[KEY_W]) { theShip.rocketPower = 400; theShip.fuel--; } else if (key[KEY_S]) { theShip.rocketPower = -150; theShip.fuel--; } else { theShip.rocketPower = 0; } if (key[KEY_A]) { theShip.velRotation -= 0.05; theShip.fuel--; } if (key[KEY_D]) { theShip.velRotation += 0.05; theShip.fuel--; } } else { theShip.rocketPower = 0; } if (theShip.rotation < -255) { theShip.rotation = 255; } if (theShip.rotation > 255) { theShip.rotation = -255; } if (key[KEY_LCONTROL]) { theShip.velX = 0; theShip.velY = 0; theShip.velRotation = 0; } if (key[KEY_C]) { pointHeader = NULL; } if (key[KEY_R]) { randomizeMap(); } if (key[KEY_P]) { readkey(); } createPoint(makecol(255, 255, 255), theShip.x + 0.5 * theShip.width, theShip.y + 0.5 * theShip.height); createPoint(makecol(255, 0, 0), theShip.x + 0.5 * theShip.width + (fixtof(fixcos(ftofix(theShip.rotation))) * 10), theShip.y + 0.5 * theShip.height + (fixtof(fixsin(ftofix(theShip.rotation))) * 10)); }