Design and Implementation of the QT class for online wuziqi game in Huaqing vision learning Manual (4)

Source: Internet
Author: User

Design and Implementation of Network wuziqi game QT class (4)

 

After a period of study in C ++ and QT, I plan to make a good transformation of the C ++ game.

As you know, the previous programs are not even used in constructor, and even though object-oriented classes are used, the idea still falls into the mud of procedural programming, the object is implemented as a subfunction, that is, a parameter is passed in, and a result is obtained through the return value. For this reason, I picked up the gel pen and started to draw a peach character (^ OK if I can understand it myself ):

1. Based on the original C ++ wuziqi program, we re-optimized it with the object-oriented approach and added the user interface class.

2. The original coordinates were input manually. Now we need to use the mouse to get the coordinates (mousepressevent ()?), There should be a certain range of clicks.

3. added the graphic interface class (paintevent ()?), The display function is separated from the checkerboard. The checkerboard is only responsible for storing and updating the coordinate data of the chess pieces.

4. To make the interface more beautiful, I plan to paste a background image (Label-> setpixmap ()? Qimage? Qpixmap ?).

5. the grid of the board is implemented using drawline () of the qpainter class, and 20 straight lines are drawn in the X and Y directions respectively.

6. Do the pawns use drawellipse () of the qpainter class to draw circles and fill them? Or is it implemented by directly drawimage () Textures?

7. This is only a dialog box program that can play chess with itself.

8. Okay. Now, let's change the third version of the wuziqi game to a new version:

Board type chessboard. h:

Const int board_size = 19;
Const int white_chess = 'W ';
Const int black_chess = 'B ';
Const int no_chess = '';
Class chessboard
{
Public:

Chessboard (); // Constructor


Void clear (); // clear the screen


Void setchess (int x, int y, int CHS );
Int getchess (int x, int y); // obtain the position of the pawn.


Bool judgevictory (const int X, const int y );
PRIVATE:
Int Board [board_size] [board_size];

};

Pawn class mychess. h:

Class mychess
{
Public:

Mychess (INT, chessboard *);

// Constructor



Void setlocation (INT, INT );

Bool setchess ();
Int getchess (INT, INT );

Bool endflag; // The end flag.


PRIVATE:
Int chesstype;
Int currentx;
Int currenty;
Chessboard * Board;
// Chessboard

};

Board interface base class ChEssBase. h:

Class ChEssBase: Public qdialog
{
Q_object
Public:
ChEssBase (INT, char **);
Public slots:
Void slotturn (bool );
Void slotvictory (bool );
Void slotclear ();
Void slotstart ();
PRIVATE:
Chessboard * Board; // Board

Mychess * white; // white chess

Mychess * black; // black

Chessview * view; // chessboard View


Qlabel * statuslabel;
Qpushbutton * startbutton;
Qpushbutton * restartbutton;
Qpushbutton * quitbutton;
};

The checker interface View class chessview. h:

Const int board_height_default = 320; // The height of the Board

Class chessview: Public qwidget
{
Q_object
Public:
Chessview (INT, char **, chessboard *, mychess *, mychess *, qwidget * parent = 0 );
Protected:
Void paintevent (qpaintevent * event); // process the UI re-painting event

Void mousepressevent (qmouseevent * event); // handle the mouse button event

Void judgelocation (int x, int y );
Void drawpix (); // display the drawing

Public slots:
Void slotclear ();
Void slotstart ();
Signals:
Void signalturn (bool );
Void signalvictory (bool );
PRIVATE:
Qpixmap * pix;
Qimage imagwhite; // a white image

Qimage imagblack; // a black image

Int startx;
Int starty;
Int step;
Int width;
Int height;
Bool key;
Bool startflag;
Chessboard * Board; // Board

Mychess * whitechess; // white chess

Mychess * blackchess; // heiqi


};

Main Program Main. cpp:

Int main (INT argc, char * argv [])
{
Qapplication app (argc, argv );
Qtextcodec: setcodecfortr (qtextcodec: codecforlocale ());
ChEssBase * base = new ChEssBase (argc, argv );
Base-> show (); // display interface

Return app.exe C ();
}

9. The following figure shows the running effect of the fourth version of the wuziqi game installed in QT:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.