QT Flappy_bird

相逢意气为君饮,系马高楼垂柳边。这篇文章主要讲述QT Flappy_bird相关的知识,希望能为你提供帮助。
【QT Flappy_bird】版权声明:访问者可将本网站提供的内容或服务用于个人学习、研究或欣赏,以及其他非商业性或非盈利性用途,但同时应遵守著作权法及其他相关法律的规定,不得侵犯本网站及相关权利人的合法权利。除此以外,将本网站任何内容或服务用于其他用途时,须征得本网站及相关权利人的书面许可,并支付报酬。转载需表明文章地址。
本网站内容原作者如不愿意在本网站刊登内容,请及时通知本站,予以删除。
作者QQ:1765813715
由于C++课程设计的原因 打算用QT来写一个Flappy bird 锻炼一下自己对QT的掌握. 
还是花了不少时间 
总结一下 这次我没有把游戏元素 抽象为类     所以代码可读性很差.   导致后面修改的时候自己都晕了  以后要习惯用类来开发
 
挺累的所以这次我就不写设计报告什么的了  
直接贴关键代码   文件我还是上传到百度云
http://pan.baidu.com/s/1dF8CTQp

QT Flappy_bird

文章图片
QT Flappy_bird

文章图片
1 //mainwindow.h 2 #ifndef MAINWINDOW_H 3 #define MAINWINDOW_H 4 5 #include< QMainWindow> 6 #include< QKeyEvent> 7 #include< QPainter> 8 #include< QPixmap> 9 #include< QMouseEvent> 10 #include< QTimer> 11 #include< QTime> 12 #include< QFile> 13 #include< QDataStream> 14 #include< QMediaPlayer> 15 using namespace std; 16 namespace Ui { 17 class MainWindow; 18 } 19 20 class MainWindow : public QMainWindow 21 { 22Q_OBJECT 23 24 public: 25explicit MainWindow(QWidget *parent = 0); 26void paintEvent(QPaintEvent *); 27void keyPressEvent(QKeyEvent* e); 28void mousePressEvent(QMouseEvent* m); 29void QTimerEvent(int timerId); 30void welcome(); 31void lead(); 32void game(); 33~MainWindow(); 34 35 private: 36QMediaPlayer* m_fly; 37QMediaPlayer* m_die; 38QMediaPlayer* m_hit; 39QMediaPlayer* swoosh; 40QMediaPlayer* m_point; 41bool cheat; 42int last; 43void gradesave(); 44void gradeload(); 45void Rank(); 46void reset(); 47int distance; //上下两管道之间的距离 48void drawgrades(int x, int y, int grade_); 49void over(); 50bool doesadd; //用于加分判断 51bool collisionjudge(); 52QRect* pipe[6]; 53QTimer* timer; 54int land_site; 55int grades; //保存得分 56int best_grades; 57int tag; //表示现在窗口位于哪一个界面 58int bird_tag; 59int l_tag; //记录引导界面位于在绘制哪一个图像 60int fly; 61int v,a1,a2; 62int graderank[4]; // 63QRect menu; 64QRect begin; //保存开始按钮的矩形区域 65QRect rank; //保存排行按钮的矩形区域 66QPixmap medal; 67QPixmap gameover; 68QPixmap again; 69QPixmap ank; 70QPixmap socreboard; 71QPixmap b_pixmap; //背景图片 72QPixmap w_pixmap; //欢迎界面 73QPixmap c_pixmap; //通用的 保存的图片会改变 74QPixmap land; 75QPixmap pip_up; 76QPixmap pip_down; 77QPixmap bird; //保存鸟的图片 78QPixmap number[10]; 79QRect r_bird; //保存鸟的坐标 80Ui::MainWindow *ui; 81 }; 82 83 #endif // MAINWINDOW_H

View Code
QT Flappy_bird

文章图片
QT Flappy_bird

文章图片
1 //mainwindow.cpp 2 #include "mainwindow.h" 3 #include "ui_mainwindow.h" 4 QString dat[3][4]={ 5":/图片/鸟/上20.png",":/图片/鸟/上0.png",":/图片/鸟/上-20.png",":/图片/鸟/上-90.png", 6":/图片/鸟/中20.png",":/图片/鸟/中0.png",":/图片/鸟/中-20.png",":/图片/鸟/中-90.png", 7":/图片/鸟/下20.png",":/图片/鸟/下0.png",":/图片/鸟/下-20.png",":/图片/鸟/下-90.png" 8 }; 9 MainWindow::MainWindow(QWidget *parent) : 10QMainWindow(parent), 11ui(new Ui::MainWindow) 12 { 13QTime t; 14t= QTime::currentTime(); 15qsrand(t.msec()+t.second()*1000); 16ui-> setupUi(this); 17tag=1; 18best_grades=0; 19cheat=0; 20for(int i=0; i< 4; i++) graderank[i]=0; 21gradeload(); 22 23m_fly=new QMediaPlayer; 24m_fly-> setMedia(QUrl("qrc:/m/sounds/sfx_wing.mp3")); 25m_fly-> setVolume(100); 26 27m_die=new QMediaPlayer; 28m_die-> setMedia(QUrl("qrc:/m/sounds/sfx_die.mp3")); 29m_die-> setVolume(100); 30 31m_hit=new QMediaPlayer; 32m_hit-> setMedia(QUrl("qrc:/m/sounds/sfx_hit.mp3")); 33m_hit-> setVolume(100); 34 35swoosh=new QMediaPlayer; 36swoosh-> setMedia(QUrl("qrc:/m/sounds/sfx_swooshing.mp3")); 37swoosh-> setVolume(100); 38 39m_point=new QMediaPlayer; 40m_point-> setMedia(QUrl("qrc:/m/sounds/sfx_point.mp3")); 41m_point-> setVolume(100); 42distance=140; 43a1=5; a2=-5; 44timer=new QTimer(this); 45connect(timer,SIGNAL(timeout()),this,SLOT(repaint())); //将计时器和 重绘 关联起来 46begin.setTop(370); begin.setBottom(445); begin.setLeft(35); begin.setRight(170); 47rank.setTop(370); rank.setBottom(445); rank.setLeft(210); rank.setRight(345); 48menu.setTop(400); menu.setBottom(427); menu.setLeft(290); menu.setRight(367); 49socreboard.load(":/图片/其他/记分牌.png"); 50b_pixmap.load(":/图片/背景/白天.png"); 51w_pixmap.load(":/图片/其他/开始.png"); 52land.load(":/图片/背景/地.png"); 53pip_up.load(":/图片/柱子/反.png"); 54pip_down.load(":/图片/柱子/正.png"); 55gameover.load(":/图片/其他/gameover.png"); 56again.load(":/图片/其他/重来.png"); 57ank.load(":/图片/其他/排行榜.png"); 58number[1].load(":/图片/数字/1.png"); number[2].load(":/图片/数字/2.png"); number[3].load(":/图片/数字/3.png"); 59number[4].load(":/图片/数字/4.png"); number[5].load(":/图片/数字/5.png"); number[6].load(":/图片/数字/6.png"); 60number[7].load(":/图片/数字/7.png"); number[8].load(":/图片/数字/8.png"); number[9].load(":/图片/数字/9.png"); 61number[0].load(":/图片/数字/0.png"); 62reset(); 63this-> setFixedSize(385,448); 64this-> setWindowTitle("Flappy Bird_by lyon"); 65this-> setWindowIcon(QIcon(":/bird.ico")); 66 } 67 void MainWindow::reset() 68 { 69bird_tag=1; 70fly=0; 71doesadd=0; 72v=0; 73land_site=385; 74l_tag=1; 75grades=0; 76bird.load(":/图片/鸟/中0.png"); 77r_bird.setTop(240); r_bird.setLeft(95); r_bird.setRight(138); r_bird.setBottom(270); 78 79int zz=qrand()%150+100-250; 80pipe[0]=new QRect(460,zz,65,250); 81pipe[1]=new QRect(460,zz+250+distance,65,250); 82zz=qrand()%150-150; 83pipe[2]=new QRect(460+220,zz,65,250); 84pipe[3]=new QRect(460+220,zz+250+distance,65,250); 85zz=qrand()%150-150; 86pipe[4]=new QRect(460+220*2,zz,65,250); 87pipe[5]=new QRect(460+220*2,zz+250+distance,65,250); 88 89 } 90 void MainWindow::paintEvent(QPaintEvent* )//窗口重绘 91 { 92switch (tag) 93{ 94case 1: 95welcome(); 96break; 97 98case 2: 99lead(); 100break; 101case 3: 102game(); 103break; 104case 4: 105over(); 106break; 107case 5: 108Rank(); 109break; 110default: 111break; 112} 113 } 114 115 void MainWindow::keyPressEvent(QKeyEvent *e)//键盘事件 116 { 117switch(e-> key()) 118{ 119case Qt::Key_Space: 120{ 121if(tag==2) 122{ 123tag=3; 124timer-> start(70); 125} 126else if(tag==3)//这里后面再写 127{ 128//if(fly> =0) return; 129m_fly-> stop(); 130m_fly-> play(); 131fly=4; v=10; 132//repaint(); 133} 134break; 135} 136case Qt::Key_F1: 137if(distance==140) distance=200; 138else if(distance==200) distance=140; 139break; 140case Qt::Key_F2: 141cheat=!cheat; 142break; 143default: 144return ; 145 146} 147 } 148 149 void MainWindow::mousePressEvent(QMouseEvent *m) 150 { 151switch(tag) 152{ 153case 1: 154{ 155if(begin.contains(m-> x(),m-> y())) 156{ 157timer-> start(300); 158tag=2; 159swoosh-> stop(); 160swoosh-> play(); 161} 162else if(rank.contains(m-> x(),m-> y())) 163{ 164last=1; 165tag=5; 166swoosh-> stop(); 167swoosh-> play(); 168repaint(); 169} 170 171break; 172} 173case 4: 174{ 175if(begin.contains(m-> x(),m-> y())) 176{ 177timer-> start(300); 178tag=2; 179swoosh-> stop(); 180swoosh-> play(); 181reset(); 182} 183else if(rank.contains(m-> x(),m-> y())) 184{ 185last=4; 186tag=5; 187swoosh-> stop(); 188swoosh-> play(); 189repaint(); 190} 191 192break; 193} 194case 5: 195if(menu.contains(m-> x(),m-> y())) 196{ 197tag=last; 198swoosh-> stop(); 199swoosh-> play(); 200repaint(); 201} 202break; 203} 204 } 205 206 void MainWindow::welcome()//欢迎界面 tag 1 207 { 208QPainter painter(this); 209painter.drawPixmap(0,0,w_pixmap); 210 } 211 212 void MainWindow::lead()//引导界面 tag 2 213 { 214switch(l_tag) 215{ 216case 1: 217c_pixmap.load(":/图片/引导/白天/上.png"); l_tag=2; break; 218case 2: 219c_pixmap.load(":/图片/引导/白天/中.png"); l_tag=3; break; 220case 3: 221c_pixmap.load(":/图片/引导/白天/下.png"); l_tag=1; break; 222} 223QPainter painter(this); 224painter.drawPixmap(0,0,c_pixmap); 225c_pixmap.load(":/图片/数字/0.png"); 226painter.drawPixmap(180,100,c_pixmap); 227painter.drawPixmap(0,423,land); 228//painter.drawPixmap(95,240,bird); 229 } 230 231 void MainWindow::game()//画出游戏开始时 鸟 管道 232 { 233if(!cheat) 234{ 235if(collisionjudge()) 236{ 237m_hit-> play(); 238timer-> stop(); 239tag=4; 240best_grades=max(best_grades,grades); 241gradesave(); 242repaint(); 243} 244} 245QPainter painter(this); 246//painter.drawPixmap(50,50,pip_down); 247painter.drawPixmap(0,0,b_pixmap); 248//if(pipe[0]-> right()< r_bird.left()) grades++; 249for(int i=0; i< 6; i++) 250{ 251//painter.drawRect(*pipe[i]); 252 253if(i%2==0) 254painter.drawPixmap(pipe[i]-> left(),pipe[i]-> top(),pip_up); 255else 256painter.drawPixmap(pipe[i]-> left(),pipe[i]-> top(),pip_down); 257 258if(i%2==0)//判断加分 259{ 260if(pipe[i]-> right()< r_bird.left()& & !doesadd) 261{ 262doesadd=1; 263grades++; 264m_point-> stop(); 265m_point-> play(); 266} 267} 268pipe[i]-> translate(-8,0); 269} 270if(pipe[0]-> right()< 0) 271{ 272doesadd=0; 273for(int i=0; i< 4; i+=2) 274{ 275pipe[i]=pipe[i+2]; 276pipe[i+1]=pipe[i+3]; 277} 278int zz=qrand()%150-150; 279pipe[4]=new QRect(pipe[2]-> left()+220,zz,65,250); 280pipe[5]=new QRect(pipe[3]-> left()+220,zz+250+distance,65,250); 281} 282painter.drawPixmap(land_site-385,423,land); 283painter.drawPixmap(land_site,423,land); 284land_site-=5; 285if(land_site< 0) 286land_site=384; 287 288//painter.drawRect(r_bird); 289 290painter.drawPixmap(r_bird.left(),r_bird.top(),bird); 291if(fly> 0) 292{ 293r_bird.translate(0,-(v+a1/2)); 294v+=a1; 295bird.load(dat[bird_tag][0]); 296fly--; 297bird_tag++; 298if(bird_tag==3) bird_tag=0; 299} 300else if(fly==0) 301{ 302v=0; 303bird.load(dat[bird_tag][1]); 304fly--; 305bird_tag++; if(bird_tag==3) bird_tag=0; 306} 307else if(fly< 0) 308{ 309r_bird.translate(0,-(v+a2/2)); 310v+=a2; 311if(fly> =-5) 312bird.load(dat[bird_tag][2]); 313else bird.load(dat[bird_tag][3]); 314fly--; 315bird_tag++; if(bird_tag==3) bird_tag=0; 316} 317 318if(r_bird.top()< -2) r_bird.translate(0,10); 319drawgrades(180,100,grades); 320 321 } 322 323 MainWindow::~MainWindow() 324 { 325delete ui; 326 } 327 328 void MainWindow::gradesave()//保存排行榜 329 { 330for(int i=0; i< 4; i++) 331{ 332if(grades> graderank[i]) 333{ 334for(int j=3; j> i; j--) 335graderank[j]=graderank[j-1]; 336graderank[i]=grades; 337break; 338} 339} 340QFile file("rank.b"); 341file.open(QIODevice::WriteOnly); 342QDataStream out(& file); 343for(int i=0; i< 4; i++) 344out< < graderank[i]; 345 } 346 347 void MainWindow::gradeload()//加载排行榜 348 { 349QFile file("rank.b"); 350if(file.open(QIODevice::ReadOnly)) 351{ 352QDataStream in(& file); 353for(int i=0; i< 4; i++) 354in> > graderank[i]; 355} 356 357 358 } 359 360 void MainWindow::Rank() 361 { 362QPainter painter(this); 363painter.drawPixmap(0,0,b_pixmap); 364medal.load(":/图片/奖牌/金牌.png"); painter.drawPixmap(0,0,medal); 365medal.load(":/图片/奖牌/白金牌.png"); painter.drawPixmap(0,62,medal); 366medal.load(":/图片/奖牌/银牌.png"); painter.drawPixmap(0,124,medal); 367medal.load(":/图片/奖牌/铜牌.png"); painter.drawPixmap(0,186,medal); 368c_pixmap.load(":/图片/其他/menu.png"); 369painter.drawPixmap(290,400,c_pixmap); 370int t=10; 371for(int i=0; i< 4; i++) 372{ 373drawgrades(100,t,graderank[i]); 374t+=61; 375} 376 377 } 378 379 380 void MainWindow::drawgrades(int x,int y,int grade_) 381 { 382QPainter painter(this); 383if(grade_==0) 384{ 385painter.drawPixmap(x,y,number[0]); 386} 387int a=grade_; 388int bite=0; 389while(a) 390{ 391a/=10; 392bite++; 393} 394int head=x+bite/2*25; 395a=grade_; 396while(a) 397{ 398painter.drawPixmap(head,y,number[a%10]); 399head-=25; 400a/=10; 401} 402 } 403 404 void MainWindow::over()//游戏结束的画面 405 { 406QPainter painter(this); 407painter.drawPixmap(0,0,b_pixmap); 408for(int i=0; i< 6; i++) 409{ 410if(i%2==0) 411painter.drawPixmap(pipe[i]-> left(),pipe[i]-> top(),pip_up); 412else 413painter.drawPixmap(pipe[i]-> left(),pipe[i]-> top(),pip_down); 414} 415painter.drawPixmap(r_bird.left(),r_bird.top(),bird); 416painter.drawPixmap(land_site-385,423,land); 417painter.drawPixmap(land_site,423,land); 418painter.drawPixmap(45,130,socreboard); 419painter.drawPixmap(70,50,gameover); 420painter.drawPixmap(begin.left(),begin.top(),again); 421painter.drawPixmap(rank.left(),rank.top(),ank); 422if(grades> =100) 423medal.load(":/图片/奖牌/金牌.png"); 424else if(grades> =50) 425medal.load(":/图片/奖牌/白金牌.png"); 426else if(grades> =20) 427medal.load(":/图片/奖牌/银牌.png"); 428else if(grades> =10) 429medal.load(":/图片/奖牌/铜牌.png"); 430if(grades> =10) 431{ 432painter.drawPixmap(75,177,medal); 433} 434 435drawgrades(278,166,grades); 436drawgrades(278,224,best_grades); 437 } 438 439 bool MainWindow::collisionjudge()//碰撞检测 440 { 441if(r_bird.bottom()> =423) return true; 442for(int i=0; i< 6; i++) 443{ 444if(r_bird.intersects(*pipe[i])) 445{ 446return true; 447} 448} 449return false; 450 }

View Code 

    推荐阅读