qt Edition snake game
Reprint please indicate the source: Muni column http://blog.csdn.net/u012027907
recently in the study of QT, took one months to master the most basic knowledge of QT, also completed the "QT version of the music player", "QT version of the snake Game", "QT version of Double Tetris" and "QT version of the scientific calculator," and so on, before the VC wrote these programs, So in QT just changed the display, and so on, I wrote "C + + version of the snake Game", "VC version of the Snake Game", at that time will be unrelated to display and other things encapsulated in the QT under the direct use, only changed the display and other statements.
The following are running under Windows 7:
Below is the Ubuntu run:
After I've written it under Windows, I'll recompile the source code under Ubuntu and run it under Ubuntu, which also shows the benefits of QT writing and running everywhere.
About the snake game design principle is not speaking, specific can refer to my blog "C + + version of Snake Game", "VC version of the snake game."
The following is mainly about QT and VC different things.
First, the definition of variables and slots in the file:
Class Snake:public qmainwindow{q_objectpublic:explicit Snake (qwidget *parent = 0); ~snake ();p rivate slots:void Gamestart (); void OnPause (); void SuperSpeed (); void Fastspeed (); void Midspeed (); void Slowspeed (); void Veryslowspeed ();p rivate:ui::snake *ui; int image[20][20]; Game panel, that is, the small snake activity range int foodx; The x-coordinate of the occurrence of the thing int FoodY; The y-coordinate of a thing appears int snakex; Record the small snake's head x coordinate int snakey; Record the small snake's head y coordinate int head; Small snake head under the mark int tail; Small snake tail subscript int snake[2][20000]; Record the coordinates of the small snake all body int node; The number of knots of the small snake int direction; The direction of the small snake head int i,j; int gamespeed; speed int score; Record fractional bool Ispause; suspend bool p; int Gameover; The variable int level that makes the game end; Set the rank int length; The length int timerid, which is recorded as node in order to set the rank; void ReInIt (); int Drawsnake (); Record the small snake each time it moves the back of the body and the next coordinate void automove (); Make the small snake automatically move int gameover (); Determine if the game is over int generatefood (); Produce food void shiftleft (); Control left shift void Shiftright (); Control right Shift void Shiftdown (); Control the Move Down void Shiftup (); Control up-shift protected:void keypressevent (qkeyevent *event); void TimerEvent (Qtimerevent *event); void PaintEvent (Qpaintevent *event);};
The difference is just
void Keypressevent (Qkeyevent *event); void TimerEvent (Qtimerevent *event); void PaintEvent (Qpaintevent *event);
The realization of three slots is the way of Qt.
Reprint Please indicate the source: Muni column http://blog.csdn.net/u012027907
The specific code is as follows:
void Snake::keypressevent (Qkeyevent *event) {if (direction = = 2 | | Direction = = 8) {if (Event->key () = = Qt::key_right) shiftright (); else if (event->key () = = Qt::key_left) shiftleft (); else if (event->key () = = Qt::key_0) onPause (); } else if (direction = = 4 | | direction = = 6) {if (Event->key () = = qt::key_up) shiftup (); else if (event->key () = = Qt::key_down) Shiftdown (); else if (event->key () = = Qt::key_0) onPause (); }}void snake::timerevent (qtimerevent *event) {if (Event->timerid () = = Timerid) {if (! Gameover) {if (! Ispause) Automove (); The small snake automatically moves gameover (); Determine if the game is over if (gameover==1) {KillTimer (Timerid); if (level>8) if (qmessagebox::question (tHIS,TR ("hint"), TR ("You must be a hardcore player!") Brother Admire! Do you still want to challenge? "), qmessagebox::yes,qmessagebox::no) = = Qmessagebox::yes) {reinit (); Gamestart (); } else close (); else if (level>5) if (Qmessagebox::question (THIS,TR ("hint"), TR ("You must be a master!") Do you still want to challenge? "), qmessagebox::yes,qmessagebox::no) = = Qmessagebox::yes) {reinit (); Gamestart (); } else close (); else if (Qmessagebox::question (THIS,TR ("hint"), tr ("Keep trying! Do you still want to challenge? "), qmessagebox::yes,qmessagebox::no) = = Qmessagebox::yes) {reinit (); Gamestart (); } else close (); } drawsnake (); Determine whether to eat food} this->repaint (); }}void Snake::p aintevent (qpaintevent *event) {qpainter painter (this); int nSize = 20; int offset = 1; Qimage img; Img.load (":/images/border.png"); Qbrush Brushborder (IMG); Painter.setbrush (Brushborder); Qrect Bottomrec (0,nsize*22+offset,nsize*22,nsize); Painter.fillrect (Bottomrec,brushborder); Qrect Toprec (0,20+offset,nsize*22,nsize); Painter.fillrect (Toprec,brushborder); Qrect Leftrec (0,20+offset,nsize,nsize*21); Painter.fillrect (Leftrec,brushborder); Qrect Rightrec (nsize*21,20+offset,nsize,nsize*21); Painter.fillrect (Rightrec,brushborder); Qrect rect (20,40+offset,nsize*20,nsize*20); Qimage Imageback; Imageback.load (":/images/grass.png"); Qbrush brushback (Imageback); Painter.setbrush (Brushback); Painter.drawrect (rect); Qrect RC; Qbrush Brush (Qcolor (255,0,0)); Painter.setbrush (brush); QString level = "level:"; Level + = Qstring::number (level); Ui->label_level->settext (level); QString score = "Score:"; Score + = Qstring::number (Score); Ui->label_score->settext (score); for (i=0;i<20;i++) {for (j=0;j<20;j++) {rc = Qrect (j*nsize+20,i*nsize+40+offset,nsize,ns ize); if (image[i][j]!=0) {if (image[i][j]==3) {qimage img; Img.load (":/images/body.png"); Qbrush Brush (IMG); Painter.setbrush (brush); Painter.fillrect (Rc,brush); } else if (image[i][j] = = 1) {if (direction==2) { Qimage img; Img.load (":/images/headdown.png"); Qbrush Brush (IMG); Painter.setbrush (brush); Painter.fillrect (Rc,brush); } else if (direction = = 4) {qimage img; Img.load (":/images/headleft.png "); Qbrush Brush (IMG); Painter.setbrush (brush); Painter.fillrect (Rc,brush); } else if (direction = = 6) {qimage img; Img.load (":/images/headright.png"); Qbrush Brush (IMG); Painter.setbrush (brush); Painter.fillrect (Rc,brush); } else {qimage img; Img.load (":/images/headup.png"); Qbrush Brush (IMG); Painter.setbrush (brush); Painter.fillrect (Rc,brush); }} else if (image[i][j] = = 2) {qimage img; Img.load (":/images/apple.png"); Qbrush Brush (IMG); Painter.setbrush (brush); Painter.fillrect (Rc,brush); } } } }}//////////////////////////////////////////////////////////////////////
There is also a signal and slot for the key response to create the associated code:
Connect (Ui->action_startgame, SIGNAL (triggered ()), this, SLOT ( Gamestart ())); Connect (Ui->action_pause, SIGNAL (triggered ()), this, SLOT (OnPause ())); Connect (Ui->action_quit, SIGNAL (triggered ()), this, SLOT (Close ())); Connect (Ui->action_veryfast, SIGNAL (triggered ()), this, SLOT (SuperSpeed ())); Connect (Ui->action_fast, SIGNAL (triggered ()), this, SLOT (Fastspeed ())); Connect (Ui->action_mid, SIGNAL (triggered ()), this, SLOT (Midspeed ())); Connect (Ui->action_slow, SIGNAL (triggered ()), this, SLOT (Slowspeed ())); Connect (Ui->action_veryslow, SIGNAL (triggered ()), this, SLOT (Veryslowspeed ()));
Another small detail is the random function: Qsrand () where the food is produced:
produce food int Snake::generatefood () { Qsrand (Qtime::currenttime (). msec ()); Generate random sequences of do{ Foodx=qrand ()%20 with time as seed; Food output x-coordinate foody=qrand ()%20; The y-coordinate of food output }while (image[foodx][foody]!=0); The resulting food coordinates are limited to the game panel, and the food coordinates are not coincident with the image[foodx][foody]=2 of the small snake body coordinates ; return image[foodx][foody];}
Reprint Please indicate the source:Muni's Column
http://blog.csdn.net/u012027907
Qt Edition Snake Game