void Board::setSize(int x, int y) { if(x == x_tiles() && y == y_tiles()) return; if(field != 0) delete [] field; field = new int[ x * y ]; _x_tiles = x; _y_tiles = y; for(int i = 0; i < x; i++) for(int j = 0; j < y; j++) setField(i, j, EMPTY); // set the minimum size of the scalable window const double MINIMUM_SCALE = 0.2; int w = qRound(tiles.unscaledTileWidth() * MINIMUM_SCALE) * x_tiles(); int h = qRound(tiles.unscaledTileHeight() * MINIMUM_SCALE) * y_tiles(); w += tiles.unscaledTileWidth(); h += tiles.unscaledTileWidth(); setMinimumSize(w, h); resizeBoard(); newGame(); emit changed(); }
void Board::mouseMoveEvent(QMouseEvent *event) { if(event!=0){ if( event->buttons().testFlag(Qt::LeftButton) && isMoving) { this->move(this->pos() + (event->globalPos() - lastMousePos)); lastMousePos = event->globalPos(); } if(isResizing){ resizeBoard(widthAtPress-mapFromGlobal(lastMousePos).x()+event->x()); } } int x; if(event==0){ x = this->mapFromGlobal(QCursor::pos()).x(); x= round(float(x)/float(this->width())*(float)this->originalWidth); } else{ x = round(float(event->x())/float(this->width())*(float)this->originalWidth); } if((parent->parent->gamestate==parent->PLAYER_1_TO_MOVE && parent->parent->player1->isManual)|| (parent->parent->gamestate==parent->parent->PLAYER_2_TO_MOVE && parent->parent->player2->isManual)){ int col = ceil((x+OFFSET_LEFT()-H_SPACING())/(COL_WIDTH()+H_SPACING())); if(col>7) col = 7; if(col<1) col = 1; this->highlight(col); } }
void EnemyField::fill(){ srand(time(0)); int size = 4; while (!allShipsPlaced){ int x = rand() % 10; int y = rand() % (10 - size - 1); current_ship->moveTo(x, y); if (addShipToArray(current_ship)){ current_ship_index++; if (current_ship_index >= 1) size = 3; else if (current_ship_index >= 3) size = 2; else if (current_ship_index >= 6) size = 1; if (current_ship_index == 10) { allShipsPlaced = true; resizeBoard(); return; } current_ship = ships[current_ship_index]; } else { x = rand() % (10 - size - 1); y = rand() % 10; current_ship->moveTo(x, y); current_ship->rotate(); if (addShipToArray(current_ship)){ current_ship_index++; if (current_ship_index >= 1) size = 3; else if (current_ship_index >= 3) size = 2; else if (current_ship_index >= 6) size = 1; if (current_ship_index == 10) { allShipsPlaced = true; resizeBoard(); return; } current_ship = ships[current_ship_index]; } } } }
void EnemyField::reset(){ reset_field(); setShipsInvisible(); resizeBoard(); fill(); }
EnemyField::EnemyField(){ setShipsInvisible(); resizeBoard(); fill(); }
void Board::resizeEvent(QResizeEvent*) { resizeBoard(); emit resized(); }
Board::Board(ControlPanel *parent) :parent(parent), QGraphicsView() { moderator = parent->parent; //this->setRenderHint(QPainter::SmoothPixmapTransform); isResizing = false; isMoving = false; // Create the view and the scene! scene = new QGraphicsScene(); this->setScene(scene); // Make it all cool and transparent and frameless and everything like that! this->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); this->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); this->window()->setWindowFlags(Qt::FramelessWindowHint); this->setAttribute(Qt::WA_TranslucentBackground); this->setAutoFillBackground(false); this->setStyleSheet( "QGraphicsView { border-style: none; background: transparent; }" ); // Resize! int width = QApplication::desktop()->width(); int height = QApplication::desktop()->height(); if((width * 1574 / 1260) > (height)){ this->resize(width*2/3,(width*2/3*1260/1574)); } else{ this->resize(height*2/3*1574/1260,height); } originalWidth = this->width(); originalHeight = this->height(); // Center (ish)! int center_x = QDesktopWidget().availableGeometry().center().x(); int center_y = QDesktopWidget().availableGeometry().center().y(); int total_width = QDesktopWidget().availableGeometry().width(); this->move(center_x - this->width()/2 + (total_width / 8),center_y - this->height()/2); // Track the mouse wherever it goes! //this->setMouseTracking(true); // Paint the picture of the board boardImage = new QPixmap(":/images/board.png"); QPixmap scaledBoardImage = boardImage->scaled(this->size(), Qt::KeepAspectRatio); highlightImage = new QPixmap(":/images/Highlight.png"); QPixmap scaledHighlightImage = highlightImage->scaled(this->size(), Qt::KeepAspectRatio); highlightGraphic = new QGraphicsPixmapItem(scaledHighlightImage); board = new QGraphicsPixmapItem(scaledBoardImage); board->setAcceptHoverEvents(true); scene->installEventFilter(this); scene->addItem(highlightGraphic); highlightGraphic->hide(); scene->addItem(board); gameResultText = new QGraphicsSimpleTextItem(); gameResultText->setBrush(QBrush(Qt::white)); QFont font = QFont("Arial Black", 100); gameResultText->setFont(font); gameResultText->setPen(QPen(Qt::black)); scene->addItem(gameResultText); if(parent->settings->value("boardsize").toInt()==1) resizeBoard(500); if(parent->settings->value("boardsize").toInt()==2) resizeBoard(1000); if(parent->settings->value("boardsize").toInt()==3) resizeBoard(1500); this->show(); currentPlayer = 2; playerGoesFirst = 2; currentPiece = new Piece(currentPlayer, this); }