您好,欢迎访问三七文档
用Qt做贪吃蛇游戏开始游戏:撞到自己蛇身游戏结束:跳出界外游戏结束:main.cpp代码:#includeQtGui/QApplication#includemysnake.hintmain(intargc,char*argv[]){QApplicationa(argc,argv);mySnakew;w.show();returna.exec();}mysnake.h代码:#ifndefMYSNAKE_H#defineMYSNAKE_H#includeQDialog#includeQPainter#includeQKeyEvent#includeQString#includeQFont#includeQRect#includeQTimernamespaceUi{classmySnake;}classmySnake:publicQDialog{Q_OBJECTpublic:explicitmySnake(QWidget*parent=0);~mySnake();voidpaintEvent(QPaintEvent*event);voidinit_Snake();//初始化voidspeed();//移动速度QRectcreateRect();//产生一个随机矩形voidisEat();//判断是否吃到voidisHit();//判断是否撞到自己privateslots:voidSnake_update();private:voidkeyPressEvent(QKeyEvent*key);Ui::mySnake*ui;boolisRun;//是否启动boolisOver;//是否结束QRect*SnakeRect;//蛇身QRectSnakeHeard;//蛇头QRecttemp_SnakeRect;//随机矩形intSnakeLen;//蛇长QTimer*timer;//定时器intdirection;//蛇移动方向QStringdisplay;//开始、结束提示QStringscoreLabel;//得分标签intscore;//得分};#endif//MYSNAKE_Hmysnake.cpp代码:#includemysnake.h#includeui_mysnake.hmySnake::mySnake(QWidget*parent):QDialog(parent),ui(newUi::mySnake){ui-setupUi(this);}mySnake::~mySnake(){deleteui;}voidmySnake::paintEvent(QPaintEvent*){QPainterpainter(this);if(!isRun){init_Snake();}//画背景painter.setPen(Qt::black);painter.setBrush(Qt::blue);painter.drawRect(15,15,260,260);painter.setPen(Qt::black);painter.setBrush(Qt::black);painter.drawRect(20,20,250,250);painter.setPen(Qt::blue);for(intii=2;ii=27;ii++){painter.drawLine(20,ii*10,270,ii*10);painter.drawLine(ii*10,20,ii*10,270);}QFontfont(Courier,12);painter.setFont(font);painter.setPen(Qt::blue);painter.drawText(30,290,Bychenjt3533creation!);//显示开始、结束QFontfont1(Courier,24);painter.setFont(font1);painter.setPen(Qt::red);painter.setBrush(Qt::red);painter.drawText(40,150,display);//计分QFontfont2(Courier,15);painter.setFont(font2);painter.setPen(Qt::red);painter.setBrush(Qt::red);painter.drawText(290,60,scoreLabel);painter.drawText(360,60,QString::number(score));//画蛇painter.setBrush(Qt::red);painter.setPen(Qt::black);painter.drawRect(temp_SnakeRect);painter.drawRects(SnakeRect,SnakeLen);if(!isOver)speed();//运动速度}voidmySnake::keyPressEvent(QKeyEvent*event){QKeyEvent*key=(QKeyEvent*)event;switch(key-key()){caseQt::Key_Up:direction=1;break;caseQt::Key_Down:direction=2;break;caseQt::Key_Left:direction=3;break;caseQt::Key_Right:direction=4;break;default:;}}voidmySnake::init_Snake()//初始化snake{SnakeLen=5;//初始化snake的长度为5direction=2;//初始化snake的运动方向向下display=GAMESTART...;scoreLabel=Score:;score=0;isRun=true;isOver=false;temp_SnakeRect=createRect();SnakeRect=newQRect[SnakeLen];for(inti=0;iSnakeLen;i++){QRectrect(100,70+10*i,10,10);SnakeRect[i]=rect;}SnakeHeard=SnakeRect[SnakeLen-1];}voidmySnake::Snake_update(){display=;SnakeHeard=SnakeRect[SnakeLen-1];isHit();isEat();for(intj=0;jSnakeLen-1;j++){SnakeRect[j]=SnakeRect[j+1];}switch(direction){case1:SnakeHeard.setHeight(SnakeHeard.height()-10);SnakeHeard.setTop(SnakeHeard.top()-10);break;case2:SnakeHeard.setHeight(SnakeHeard.height()+10);SnakeHeard.setTop(SnakeHeard.top()+10);break;case3:SnakeHeard.setLeft(SnakeHeard.left()-10);SnakeHeard.setRight(SnakeHeard.right()-10);break;case4:SnakeHeard.setLeft(SnakeHeard.left()+10);SnakeHeard.setRight(SnakeHeard.right()+10);break;default:;}SnakeRect[SnakeLen-1]=SnakeHeard;if(SnakeHeard.left()20||SnakeHeard.right()270||SnakeHeard.top()260||SnakeHeard.bottom()20){display=GAMEOVER!;isOver=true;}update();//paintEvent更新}voidmySnake::speed()//运动速度{timer=newQTimer(this);timer-setSingleShot(true);//将会只启动定时器一次,启动后每经过一次设定的时间就发送一次timeout()timer-start(500);//1秒connect(timer,SIGNAL(timeout()),SLOT(Snake_update()));}QRectmySnake::createRect()//产生一个随机矩形{intx,y;x=qrand()%25;y=qrand()%25;QRectrect(20+x*10,20+y*10,10,10);returnrect;}voidmySnake::isEat(){if(SnakeHeard==temp_SnakeRect){SnakeHeard=temp_SnakeRect;SnakeRect[SnakeLen]=SnakeHeard;SnakeLen++;temp_SnakeRect=createRect();score+=10;}}voidmySnake::isHit(){for(inti=0;iSnakeLen-1;i++){if(SnakeHeard==SnakeRect[i]){display=GAMEOVER!;isOver=true;update();}}}
本文标题:用Qt做贪吃蛇游戏
链接地址:https://www.777doc.com/doc-6243948 .html