MainWindow::MainWindow(QWidget * parent, Qt::WindowFlags flags) : QMainWindow(parent, flags) { preview = NULL; setWindowTitle("ImageGlue"); setWindowIcon(QIcon(":/ImageGlue.png")); setAcceptDrops(true); // QWidget* widget = new QWidget(this); Q_CHECK_PTR(widget); QGridLayout *gridLayout = new QGridLayout(); Q_CHECK_PTR(gridLayout); gridLayout->setGeometry(geometry()); widget->setLayout(gridLayout); setCentralWidget(widget); QLabel *label1 = new QLabel(); Q_CHECK_PTR(label1); label1->setText(tr("Images:")); gridLayout->addWidget(label1, 0, 0); list = new QListWidget(); Q_CHECK_PTR(list); connect(list, SIGNAL(itemSelectionChanged()), this, SLOT(ListSelectionChanged())); gridLayout->addWidget(list, 1, 0, 1, 3); QPushButton *add = new QPushButton(); Q_CHECK_PTR(add); add->setText(tr("Add")); gridLayout->addWidget(add, 2, 0); connect(add, SIGNAL(clicked()), this, SLOT(AddImage())); fPreview = new QPushButton(); Q_CHECK_PTR(fPreview); fPreview->setText(tr("Preview")); fPreview->setEnabled(false); gridLayout->addWidget(fPreview, 2, 1); connect(fPreview, SIGNAL(clicked()), this, SLOT(PreviewImage())); fRemove = new QPushButton(); Q_CHECK_PTR(fRemove); fRemove->setText(tr("Remove")); fRemove->setEnabled(false); gridLayout->addWidget(fRemove, 2, 2); connect(fRemove, SIGNAL(clicked()), this, SLOT(RemoveImage())); LoadSettings(); }
int main(int argc, char *argv[]) { QApplication app(argc, argv); /* 常數們 */ int boardSize = BOARD_SIZE; int stoneSize = STONE_SIZE; int boardWidth = stoneSize*boardSize; /* 新視窗 */ QWidget *window = new QMouseEventWindow; window->setWindowTitle("ReversiGrid"); window->setFixedSize(boardWidth+200, boardWidth); QWidget *board = new QWidget(window); board->setFixedSize(boardWidth, boardWidth); /* 載入棋子圖片 */ QPixmap *stoneWPix = new QPixmap(":/img/stoneW.png"); QPixmap *stoneWPixS = new QPixmap; *stoneWPixS = stoneWPix->scaled(stoneSize, stoneSize, Qt::IgnoreAspectRatio, Qt::SmoothTransformation); QPixmap *stoneBPix = new QPixmap(":/img/stoneB.png"); QPixmap *stoneBPixS = new QPixmap; *stoneBPixS = stoneBPix->scaled(stoneSize, stoneSize, Qt::IgnoreAspectRatio, Qt::SmoothTransformation); QPixmap *stoneWaPix = new QPixmap(":/img/stoneWa.png"); QPixmap *stoneWaPixS = new QPixmap; *stoneWaPixS = stoneWaPix->scaled(stoneSize, stoneSize, Qt::IgnoreAspectRatio, Qt::SmoothTransformation); QPixmap *stoneBaPix = new QPixmap(":/img/stoneBa.png"); QPixmap *stoneBaPixS = new QPixmap; *stoneBaPixS = stoneBaPix->scaled(stoneSize, stoneSize, Qt::IgnoreAspectRatio, Qt::SmoothTransformation); QPixmap *stoneWsPix = new QPixmap(":/img/stoneWs.png"); QPixmap *stoneWsPixS = new QPixmap; *stoneWsPixS = stoneWsPix->scaled(stoneSize, stoneSize, Qt::IgnoreAspectRatio, Qt::SmoothTransformation); QPixmap *stoneBsPix = new QPixmap(":/img/stoneBs.png"); QPixmap *stoneBsPixS = new QPixmap; *stoneBsPixS = stoneBsPix->scaled(stoneSize, stoneSize, Qt::IgnoreAspectRatio, Qt::SmoothTransformation); QPixmap *nullPix = new QPixmap(":/img/null.png"); /* 棋盤網格排版 */ QGridLayout *grid = new QGridLayout; grid->setSpacing(0); grid->setMargin(0); grid->setGeometry(QRect(0, 0, 10, 100)); /* 構建棋盤 */ QLabel *square[BOARD_SIZE*BOARD_SIZE]; for (int i=0; i<boardSize; i++) { for (int j=0; j<boardSize; j++) { square[i*BOARD_SIZE+j] = new QLabel; square[i*BOARD_SIZE+j]->setFrameStyle(QFrame::Panel + QFrame::Sunken); square[i*BOARD_SIZE+j]->setAlignment(Qt::AlignCenter); square[i*BOARD_SIZE+j]->setAttribute(Qt::WA_TranslucentBackground); // square[i*BOARD_SIZE+j]->setPixmap(); square[i*BOARD_SIZE+j]->setAttribute(Qt::WA_TransparentForMouseEvents, true); grid->addWidget(square[i*BOARD_SIZE+j], i, j); } } QLabel *turnT = new QLabel(window); turnT->setGeometry(boardWidth, 20, 200, 14); turnT->setText("<center>Next move:</center>"); QLabel *turn = new QLabel(window); turn->setGeometry(boardWidth+100-(STONE_SIZE/2), 45, STONE_SIZE, STONE_SIZE); turn->setFrameStyle(QFrame::Sunken); /* 遊戲物件 */ game = new Game(square, turn, stoneWPixS, stoneBPixS, stoneWaPixS, stoneBaPixS, stoneWsPixS, stoneBsPixS, nullPix); /* UI */ QCheckBox *hintCheckBox = new QCheckBox("Show hints"); hintCheckBox->setParent(window); hintCheckBox->setGeometry(boardWidth+50, 210, 100, 20); QObject::connect(hintCheckBox, SIGNAL(stateChanged(int)), game, SLOT(hintSwitch(int))); QLabel *bScore = new QLabel(window); bScore->setGeometry(boardWidth, 100, 200, 14); bScore->setText("<center>Black</center>"); QLabel *wScore = new QLabel(window); wScore->setGeometry(boardWidth, 150, 200, 14); wScore->setText("<center>White</center>"); QLCDNumber *lcdB = new QLCDNumber(window); QLCDNumber *lcdW = new QLCDNumber(window); lcdB->setGeometry(boardWidth+50, 120, 100, 24); lcdW->setGeometry(boardWidth+50, 170, 100, 24); QObject::connect(game, SIGNAL(updateBScore(int)), lcdB, SLOT(display(int))); QObject::connect(game, SIGNAL(updateWScore(int)), lcdW, SLOT(display(int))); QCheckBox *aiBCheckBox = new QCheckBox("AI"); aiBCheckBox->setParent(window); aiBCheckBox->setGeometry(boardWidth+140, 100, 50, 14); QObject::connect(aiBCheckBox, SIGNAL(stateChanged(int)), game, SLOT(aiBSwitch(int))); QCheckBox *aiWCheckBox = new QCheckBox("AI"); aiWCheckBox->setParent(window); aiWCheckBox->setGeometry(boardWidth+140, 150, 50, 14); QObject::connect(aiWCheckBox, SIGNAL(stateChanged(int)), game, SLOT(aiWSwitch(int))); /* 按鍵 */ QPushButton *undoBtn = new QPushButton("Undo"); undoBtn->setParent(window); undoBtn->setGeometry(boardWidth+140, 64, 42, 22); undoBtn->setEnabled(false); QObject::connect(undoBtn, SIGNAL(clicked()), game, SLOT(undo())); QObject::connect(game, SIGNAL(undoAvai(bool)), undoBtn, SLOT(setEnabled(bool))); QPushButton *restartBtn = new QPushButton("Restart"); restartBtn->setParent(window); restartBtn->setGeometry(boardWidth+50, 242, 100, 36); QObject::connect(restartBtn, SIGNAL(clicked()), game, SLOT(reset())); QPushButton *exitBtn = new QPushButton("Exit"); exitBtn->setParent(window); exitBtn->setGeometry(boardWidth+50, 272, 100, 36); QObject::connect(exitBtn, SIGNAL(clicked()), &app, SLOT(quit())); /* 計時器 */ QTimer *update = new QTimer; update->start(10); QObject::connect(update, SIGNAL(timeout()), game, SLOT(update())); /* 準備視窗 */ board->setLayout(grid); window->setMouseTracking(true); window->show(); game->reset(); return app.exec(); }
/** * @brief GameBoard::GameBoard * @param parent The QWidget which controls the game board. * Game Board inherits from MainWindow. It shows when the user presses "continue". * This is the constructor for the GameBoard; it creates the grid, shows levels, * shows the hallows left, and places pictures. */ GameBoard::GameBoard(QWidget *parent) : QMainWindow(parent), ui(new Ui::GameBoard) { ui->setupUi(this); //This code designs the Top Bar. // How many Hallows left: Level: Top = new QWidget; Top->setStyleSheet("QLabel { background-color : black; color : white; }"); user_points = 0; //Start with 0 points. //This layout will space out the elements above. top_bar = new QHBoxLayout(Top); //Start at three hallows remaining. //How many Hallows Remaining (leftmost side of top bar). hallows_remaining = 4; level = 1; hallows = new QLabel*[hallows_remaining-1]; hallows_label = new QLabel("Hallows: "); top_bar->addWidget(hallows_label); //An array of QLabel pointers. //GRADER: CHANGE FILE PATH HERE. //Deathly Hallows picture. QString fileNameHallows("/Users/nataliesaragosaharris/desktop/GameHomeworkSix/DeathlyHallows.jpg"); //QString fileNameHallows("//mrburns/nsaragosaharris.$/Desktop/DeathlyHallows.jpg"); hallows_picture = new QPixmap(fileNameHallows); QString fileNameHarry("/Users/nataliesaragosaharris/desktop/GameHomeworkSix/harry_potter_on_broom.gif"); //QString fileNameHarry("//mrburns/nsaragosaharris.$/Desktop/harry_potter_on_broom.gif"); harry_picture = new QPixmap(fileNameHarry); QString fileNameSnitch("/Users/nataliesaragosaharris/desktop/GameHomeworkSix/Snitch.jpg"); //QString fileNameSnitch("//mrburns/nsaragosaharris.$/Desktop/Snitch.jpg"); snitch_picture = new QPixmap(fileNameSnitch); QString fileNameDementor("/Users/nataliesaragosaharris/desktop/GameHomeworkSix/dementor.png"); //QString fileNameDementor("//mrburns/nsaragosaharris.$/Desktop/dementor.png"); dementor_picture = new QPixmap(fileNameDementor); QString fileNameQuaffle("/Users/nataliesaragosaharris/desktop/GameHomeworkSix/quaffle.gif"); //QString fileNameQuaffle("//mrburns/nsaragosaharris.$/Desktop/Quaffle.gif"); quaffle_picture = new QPixmap(fileNameQuaffle); QString fileNameCup("/Users/nataliesaragosaharris/desktop/GameHomeworkSix/TriwizardCup.jpeg"); cup_picture = new QPixmap(fileNameCup); //Here, need to add pictures of the hallows to the top bar. for(size_t i =0; i<hallows_remaining-1; i++) { hallows[i]= new QLabel; hallows[i]->setPixmap(QPixmap(fileNameHallows)); //Here, use PixMap to set picture of the hallows in the top bar. hallows[i]->setMinimumSize(40,40); hallows[i]->setMaximumSize(40,40); hallows[i]->setScaledContents(true); top_bar->addWidget(hallows[i]); } //Add in some horizontal space between labels on the top bar. QSpacerItem* horizontal_space = new QSpacerItem(100,10); top_bar->addSpacerItem(horizontal_space); //Here, indicate which broomstick they chose. QLabel* broom_type_text = new QLabel("Broom: "); if(broom_ID == 1) { broom_chosen = new QLabel("Nimbus 2000"); } else if(broom_ID == 2) { broom_chosen = new QLabel("Firebolt"); } else broom_chosen = new QLabel("Moontrimmer"); top_bar->addWidget(broom_type_text); top_bar->addWidget(broom_chosen); //Hallows Wizard Name Level QLabel* level_text = new QLabel("Level: "); level_value = new QLabel(QString::number(1)); //Start at first level. QSpacerItem* horizontal_space_2 = new QSpacerItem(*horizontal_space); top_bar->addSpacerItem(horizontal_space_2); top_bar->addWidget(level_text); top_bar->addWidget(level_value); //Create the game board, with the grid and the pictures inside. Board = new QWidget; board_size = 10; //Initialize the value of the board size here. labels = new QLabel*[board_size*board_size]; QGridLayout *SquareGrid = new QGridLayout(Board); SquareGrid->setGeometry(QRect()); SquareGrid->setSpacing(0); for(int i = 0; i<board_size; i++) { for(int j = 0; j<board_size; j++) { //Create label. labels[i*board_size+j] = new QLabel; labels[i*board_size+j]->setMaximumSize(40,40); labels[i*board_size+j]->setMinimumSize(40,40); labels[i*board_size+j]->setStyleSheet("QLabel { background-color : purple; color: white; }"); labels[i*board_size+j]->setFrameStyle(3); //If you don't want there to be any spaces between squares, do setFrameStyle(0). labels[i*board_size+j]->setAlignment(Qt::AlignCenter); //Add Label to Layout. SquareGrid->addWidget(labels[i*board_size+j], i, j); } } //Add Harry to the corner of the board for starters. Harry_position = new QPoint(0,0); labels[0]->setPixmap(*harry_picture); labels[0]->setScaledContents(true); //Put together the layout of the main window. Board->setFixedSize(400,400); QLabel *my_label = new QLabel("<b><font color=white>Welcome to Quidditch Quest!</font></h2></b>"); my_label->setGeometry(10, 10, 10, 10);//Gives label a constant size. my_label->setAlignment(Qt::AlignHCenter); //Centers the label. put_together = new QVBoxLayout; put_together->addWidget(Top, 0, Qt::AlignCenter); put_together->addWidget(my_label); put_together->addWidget(Board, 0, Qt::AlignCenter); QWidget* CoreWidget = new QWidget; CoreWidget->setLayout(put_together); CoreWidget->setStyleSheet("QLabel { background-color : black; color : white; }"); setCentralWidget(CoreWidget); this->setWindowTitle("Quidditch Quest"); this->setFixedSize(600,600); //Moving the dementors around the board. //Dementor_position is an array of pointers to a QPoint, where each dementor is placed. number_dementors = 5; Dementor_position = new QPoint[number_dementors]; std::uniform_int_distribution<int> distribution(1,10); // Set dementors randomly just outside of the board. Dementor_position[0].setX(-1); Dementor_position[0].setY(distribution(generator)%board_size); Dementor_position[1].setX(board_size); Dementor_position[1].setY(distribution(generator)%board_size); Dementor_position[2].setY(-1); Dementor_position[2].setX(distribution(generator)%board_size); //In paintevent(), it uses this value to draw it on the board. Snitch_position = new QPoint; Snitch_position->setX(9); Snitch_position->setY(9); Quaffle_position = new QPoint; Quaffle_position->setX(5); Quaffle_position->setY(5); Cup_position = new QPoint; Cup_position->setX(2); Cup_position->setY(8); how_many_times_caught = 0; //Set for how many times Harry has caught the dementor. Can only be once. how_many_times_caught_cup = 0;//Set for how many times Harry has caught the cup. Can only be once. //This timer updates the position of the dementors every 100 milliseconds. final_snitch_caught = false; QTimer *timer = new QTimer(this); connect(timer, SIGNAL(timeout()), this, SLOT(updateDementors())); timer->start(100); }
/** Creates a gameboard object based on the paramenters which are set @param parent is the Qwidget object - In this case the parent widget is set to as the MainWindow @param timer is the timer object on the board @param clock keeps track of time */ gameboard::gameboard(QWidget *parent, size_t board_sz, int min) : QWidget(parent),board_size(board_sz), minute(min) { setStyleSheet("background-color: red;"); ///Marshawn Lynch Image Loading QString fileName(":/images/Marshawn Lynch BoardPiece.jpeg"); marshawns_image = new QPixmap(fileName); /************** Skittle images **************/ QString filename_red_skittle(":/images/Red Skittle.gif"); red_skittle = new QPixmap(filename_red_skittle); QString filename_orange_skittle(":/images/Orange Skittle.gif"); orange_skittle = new QPixmap(filename_orange_skittle); QString filename_green_skittle(":/images/Green Skittle.gif"); green_skittle = new QPixmap(filename_green_skittle); QString filename_yellow_skittle(":/images/Yellow Skittle.gif"); yellow_skittle = new QPixmap(filename_yellow_skittle); QString filename_purple_skittle(":/images/Purple Skittle.gif"); purple_skittle = new QPixmap(filename_purple_skittle); /************** Top Bar **************/ QFont Helvetica_italic( "Helvetica"); Helvetica_italic.setItalic(true); Top = new QWidget; Top->setStyleSheet("QLabel { background-color:white; " "color:purple; " "border-style: outset; " "border-width:2px; " "border-color: black }"); QLabel* life_label = new QLabel("Lives Remaining:"); life_label->setFont(Helvetica_italic); // This code sets up the Top bar // This layout will space out the elements above QHBoxLayout *top_bar = new QHBoxLayout(Top); lives_remaining = 3; lives = new QLabel*[lives_remaining]; top_bar->addWidget(life_label); // This line above is important, it is an array of QLabel POINTERS. // You have to input heap memory pointers for the memory management system to work properly. for(size_t i=0;i<lives_remaining;i++) { lives[i] = new QLabel; lives[i]->setPixmap(*marshawns_image); lives[i]->setMinimumSize(30,30); lives[i]->setMaximumSize(30,30); lives[i]->setScaledContents(true); top_bar->addWidget(lives[i]); } /************** Game Board Build **************/ ///create board object for all the widgets to be laid out on board = new QWidget; labels = new QLabel*[board_size*board_size]; values = new int[board_size*board_size]; QGridLayout *SquareGrid = new QGridLayout(board); SquareGrid->setGeometry(QRect()); SquareGrid->setSpacing(0); /************** Board Layout **************/ /// Assigning all the values on the board to be 0 for(size_t i=0;i<board_size;i++) { for(size_t j=0;j<board_size;j++) { /// Assigning all the values to 0 values[i*board_size+j] = 0; /// Create label and set properties. labels[i*board_size+j] = new QLabel; labels[i*board_size+j]->setMinimumSize(30,30); labels[i*board_size+j]->setMaximumSize(30,30); labels[i*board_size+j]->setStyleSheet("QLabel { background-color : white; color : black; }"); labels[i*board_size+j]->setFrameStyle(2); labels[i*board_size+j]->setAlignment(Qt::AlignCenter); ///Each skittle color is assigned to a random number from 1-5. If the number is 6 then no skittle is added onto the gameboard int number_color_conversion = skittles_colors(generator); if (number_color_conversion<6){ if (number_color_conversion==1){ random_skittle=red_skittle; values[i*board_size+j]=1; } if (number_color_conversion==2){ random_skittle=orange_skittle; values[i*board_size+j]=2; } if (number_color_conversion==3){ random_skittle=yellow_skittle; values[i*board_size+j]=3; } if (number_color_conversion==4){ random_skittle=green_skittle; values[i*board_size+j]=4; } if (number_color_conversion==5){ random_skittle=purple_skittle; values[i*board_size+j]=5; } labels[i*board_size+j]->setPixmap(*random_skittle); labels[i*board_size+j]->setScaledContents(true); } /// Add label to the layout SquareGrid->addWidget(labels[i*board_size+j] ,i,j); } std::cout << std::endl; } /// Initialize the marshawn at the top left corner, coordinate (0,0). marshawns_position = new QPoint(0,0); /************** Timer **************/ QLabel* time_remaining = new QLabel("Time Remaining:"); time_remaining->setFont(Helvetica_italic); QFont clock_font( "Helvetica", 60, QFont::Bold); timer = new QLabel; clock = new LCDNumber(timer,minute,00); clock->setFixedSize(280, 80); clock->setFont(clock_font); clock->setAlignment(Qt::AlignCenter); clock->start(); clock->timer->start(1000); // starts the clock. /************** Score Board **************/ QLabel* score_label = new QLabel("Score:"); score_label->setFont(Helvetica_italic); score_lcd = new QLabel; score_lcd->setNum(points); score_lcd->setFixedSize(280,50); score_lcd->setAlignment(Qt::AlignCenter); QHBoxLayout* score_layout = new QHBoxLayout; score_layout->addWidget(score_label); score_layout->addWidget(score_lcd); /************** crush_output/multiplier **************/ QLabel* crush_output_label = new QLabel("Type of Crush:"); crush_output_label->setFont(Helvetica_italic); crush_output = new QLabel; crush_output->setText(type_of_crush); crush_output->setFixedSize(280,50); crush_output->setAlignment(Qt::AlignCenter); QHBoxLayout* crush_output_layout = new QHBoxLayout; crush_output_layout->addWidget(crush_output_label); crush_output_layout->addWidget(crush_output); /************** score and crush output **************/ QVBoxLayout* score_and_multiplier = new QVBoxLayout; score_and_multiplier->addLayout(score_layout); score_and_multiplier->addLayout(crush_output_layout); /************** Bottom **************/ Bottom = new QWidget; Bottom->setStyleSheet("QLabel { background: red; color : white; }"); QHBoxLayout* bottom_bar = new QHBoxLayout(Bottom); bottom_bar->addWidget(time_remaining); bottom_bar->addWidget(clock); bottom_bar->addLayout(score_and_multiplier); /// Create a vertical box layout for the two pieces QVBoxLayout* piece_together = new QVBoxLayout; piece_together->addWidget(Top,0,Qt::AlignCenter); piece_together->addWidget(board,0,Qt::AlignCenter); piece_together->addWidget(Bottom,0,Qt::AlignCenter); this->setLayout(piece_together); }