QT写的反应测试游戏源码

来源:互联网 发布:c语言入门教学 编辑:程序博客网 时间:2024/04/29 07:35
[cpp] view plaincopy
  1. #ifndef REACTIONGAME_H  
  2. #define REACTIONGAME_H  
  3.   
  4. #include <QWidget>  
  5. #include <QPainter>  
  6. #include <QPixmap>  
  7. #include <QTimer>  
  8. #include <QLCDNumber>  
  9. //==================================================================================  
  10. //QT写的反应测试游戏.拖动本拉登头像,逃避美国大兵的追击。时间越长代表你的反应能力和智商就越高  
  11. //WGM约战平台游戏反应测试游戏.有利于提升你在CS中的反应能力和逻辑能力.  
  12. //程序编程人员:Jason's.Alex QQ:531401335   
  13. //QT社区群:167304303  
  14. //日期:2012/2/10  
  15. //==================================================================================  
  16. class ReactionGame : public QWidget  
  17. {  
  18.     Q_OBJECT  
  19. public:  
  20.     explicit ReactionGame(QWidget *parent = 0);  
  21.     virtual ~ReactionGame();  
  22.     void LoadResources();//载入资源  
  23.     void ReleaseResources();//释放资源  
  24.   
  25.     virtual void paintEvent(QPaintEvent *);  
  26.     virtual void mousePressEvent(QMouseEvent *);  
  27.     virtual void mouseMoveEvent(QMouseEvent *);  
  28.     virtual void mouseReleaseEvent(QMouseEvent *);  
  29.   
  30.     struct BlockAttr  
  31.     {  
  32.         BlockAttr(const QPixmap &image):pixmap(image),xDecrease(false),yDecrease(false){}  
  33.         QPoint point;  
  34.         QPixmap pixmap;  
  35.         bool xDecrease;  
  36.         bool yDecrease;  
  37.     };  
  38.   
  39. signals:  
  40.     void GameOver(const float &score,const QString&);//游戏结束  
  41. private:  
  42.     void CalculatePos(BlockAttr *block);//计算位置  
  43.     bool HasCollide(const BlockAttr *,const BlockAttr *);//计算时候碰撞  
  44.     bool CheckValidBound(const BlockAttr *);//检测有效边界  
  45.     void ResetGame();//重置游戏  
  46.     void inilizetionGame();//初始化游戏  
  47.     void NarrowValidRect(QRect *rect);//缩小有效矩形  
  48.     const QString GetIQHint(const float score);//获取IQ提示  
  49. private slots:  
  50.     void DrawEngine();//绘图引擎  
  51.   
  52. private:  
  53.     QPainter *painter;  
  54.     BlockAttr *block1,*block2,*block3,*block4,*dropBlock;  
  55.     int factor,speed;//伸展因子.和速度  
  56.     QTimer *timer;  
  57.     QRect validRect;//有效的矩形区域  
  58.     int counter;  
  59.     float score;//分数  
  60.     QLCDNumber *lcd;//LCD显示计数器  
  61. };  
  62.   
  63. #endif // REACTIONGAME_H  
[cpp] view plaincopy
  1. #include "reactiongame.h"  
  2. #include <QDebug>  
  3. #include <QResizeEvent>  
  4. #include <QFont>  
  5. //==================================================================================  
  6. //QT写的反应测试游戏.拖动本拉登头像,逃避美国大兵的追击。时间越长代表你的反应能力和智商就越高  
  7. //WGM约战平台游戏反应测试游戏.有利于提升你在CS中的反应能力和逻辑能力.  
  8. //程序编程人员:Jason's.Alex QQ:531401335   
  9. //QT社区群:167304303  
  10. //日期:2012/2/10  
  11. //==================================================================================  
  12. ReactionGame::ReactionGame(QWidget *parent) :  
  13.     QWidget(parent)  
  14. {  
  15.     timer=new QTimer(this);  
  16.     connect(timer,SIGNAL(timeout()),SLOT(DrawEngine()));  
  17.     lcd=new QLCDNumber(this);  
  18. }  
  19.   
  20.   
  21. ReactionGame::~ReactionGame()  
  22. {  
  23.   
  24. }  
  25.   
  26. void ReactionGame::LoadResources()//载入资源  
  27. {  
  28.   
  29.     block1=new BlockAttr(QPixmap(":/gamepix/game/1"));  
  30.     block2=new BlockAttr(QPixmap(":gamepix/game/2"));  
  31.     block3=new BlockAttr(QPixmap(":/gamepix/game/3"));  
  32.     block4=new BlockAttr(QPixmap(":/gamepix/game/4"));  
  33.     dropBlock=new BlockAttr(QPixmap(":/gamepix/game/drop"));  
  34.     this->inilizetionGame();  
  35. }  
  36.   
  37. void ReactionGame::ReleaseResources()//释放资源  
  38. {  
  39.     delete block1;  
  40.     delete block2;  
  41.     delete block3;  
  42.     delete block4;  
  43.     delete dropBlock;  
  44. }  
  45.   
  46.   
  47. void ReactionGame::inilizetionGame()//初始化游戏  
  48. {  
  49.     block1->point.setX(this->width()/2);  
  50.     block1->point.setY(0);  
  51.   
  52.     block2->point.setX(0);  
  53.     block2->point.setY(this->height()/2);  
  54.   
  55.     block3->point.setX(this->width()/2);  
  56.     block3->point.setY(this->height()-block3->pixmap.height());  
  57.   
  58.     block4->point.setX(this->width()-block4->pixmap.width());  
  59.     block4->point.setY(this->height()/2);  
  60.   
  61.     dropBlock->point.setX(this->width()/2);  
  62.     dropBlock->point.setY(this->height()/2);  
  63.   
  64.     validRect.setX(100);  
  65.     validRect.setY(100);  
  66.     validRect.setWidth(this->width()-200);  
  67.     validRect.setHeight(this->height()-200);  
  68.   
  69.     counter=1;  
  70.   
  71.     factor=1;  
  72.     speed=2;  
  73.     score=0;  
  74. }  
  75.   
  76. void ReactionGame::paintEvent(QPaintEvent *)  
  77. {  
  78.   
  79.     painter=new QPainter(this);  
  80.     painter->setRenderHint(QPainter::Antialiasing);  
  81.     painter->setPen(Qt::red);  
  82.     painter->setBrush(QColor(0xbe,0xbe,0xbe));  
  83.     painter->drawRect(this->rect());  
  84.   
  85.     painter->setPen(Qt::yellow);  
  86.     painter->setBrush(QColor(0xff,0x9d-speed*5,0x6f));  
  87.     painter->drawRect(this->validRect);  
  88.   
  89.     painter->setPen(Qt::blue);  
  90.     painter->setFont(QFont("Helvetica",12,QFont::Bold));  
  91.     painter->drawText(this->width()/4,20,tr("Reaction:")+QString::number(score));  
  92.   
  93.     painter->drawPixmap(block1->point,block1->pixmap);  
  94.     painter->drawPixmap(block2->point,block2->pixmap);  
  95.     painter->drawPixmap(block3->point,block3->pixmap);  
  96.     painter->drawPixmap(block4->point,block4->pixmap);  
  97.     painter->drawPixmap(dropBlock->point,dropBlock->pixmap);  
  98.   
  99.     if(counter==1)  
  100.     {  
  101.         painter->setPen(Qt::magenta);  
  102.         painter->drawText(validRect,tr("This game is to test the player an instant reaction and logical thinking ability\n"//这个游戏是测试玩家瞬间的反应和逻辑思维能力  
  103.                                        "Drag the picture of Osama bin Laden, to avoid the pursuit of American soldiers!\n"//拖动本拉登头像避开美国大兵的追击!  
  104.                                        "The longer you pursue on behalf of the higher IQ!"));//追击时间越久代表你智商越高  
  105.     }  
  106.   
  107.     delete painter;  
  108.   
  109.     this->update();  
  110.   
  111. }  
  112.   
  113.   
  114. void ReactionGame::mouseMoveEvent(QMouseEvent *e)  
  115. {  
  116.   
  117.     dropBlock->point.setX(e->pos().x()-dropBlock->pixmap.width()/2);  
  118.     dropBlock->point.setY(e->pos().y()-dropBlock->pixmap.height()/2);  
  119.   
  120. }  
  121.   
  122. void ReactionGame::mousePressEvent(QMouseEvent *e)  
  123. {  
  124.     if(e->type()==QMouseEvent::MouseButtonPress)  
  125.     {  
  126.         QPoint begin=dropBlock->point;  
  127.         QPoint end(dropBlock->point.x()+dropBlock->pixmap.size().width(),dropBlock->point.y()+dropBlock->pixmap.size().height());  
  128.   
  129.         if(begin.x()<=e->pos().x()&&begin.y()<=e->pos().y()&&end.x()>=e->pos().x()&&end.y()>=e->pos().y())  
  130.         {  
  131.             timer->start(1);  
  132.   
  133.         }  
  134.     }  
  135. }  
  136.   
  137. void ReactionGame::mouseReleaseEvent(QMouseEvent *e)  
  138. {  
  139.     if(e->type()==QMouseEvent::MouseButtonRelease)  
  140.     {  
  141.         this->ResetGame();  
  142.     }  
  143. }  
  144.   
  145. void ReactionGame::DrawEngine()//绘制引擎  
  146. {  
  147.     score=float(speed*counter)/350;//显示分数  
  148.   
  149.     for(int i=0;i<speed;++i)  
  150.     {  
  151.       CalculatePos(block1);  
  152.       CalculatePos(block2);  
  153.       CalculatePos(block3);  
  154.       CalculatePos(block4);  
  155.   
  156.       if(HasCollide(dropBlock,block1)||HasCollide(dropBlock,block2)||  
  157.         HasCollide(dropBlock,block3)||HasCollide(dropBlock,block4)||!this->CheckValidBound(dropBlock))//如果产生碰撞  
  158.       {  
  159.          emit GameOver(score,this->GetIQHint(score));  
  160.          this->ResetGame();  
  161.       }  
  162.   
  163.     }  
  164.   
  165.   
  166.     counter++;  
  167.     lcd->display((double)counter/100);  
  168.   
  169.     if(counter%500==0)//每五秒提升一次速度  
  170.     {  
  171.         speed+=1;  
  172.     }  
  173.   
  174.     if(counter%1000==0)//每十秒缩小有效范围  
  175.     {  
  176.         NarrowValidRect(&this->validRect);  
  177.     }  
  178. }  
  179.   
  180. void ReactionGame::ResetGame()//重置游戏  
  181. {  
  182.     timer->stop();  
  183.     this->inilizetionGame();  
  184.     this->update();  
  185. }  
  186.   
  187. void ReactionGame::CalculatePos(BlockAttr *block)//计算位置  
  188. {  
  189.     int x=block->point.x();  
  190.     int y=block->point.y();  
  191.   
  192.     if(x==0)  
  193.     {  
  194.         block->xDecrease=false;  
  195.   
  196.     }else if(x+block->pixmap.width()==this->width())  
  197.     {  
  198.         block->xDecrease=true;  
  199.     }  
  200.   
  201.     if(y==0)  
  202.     {  
  203.         block->yDecrease=false;  
  204.   
  205.     }else if(y+block->pixmap.width()==this->height())  
  206.     {  
  207.         block->yDecrease=true;  
  208.     }  
  209.   
  210.     if(block->xDecrease)  
  211.     {  
  212.          block->point.setX(x-factor);  
  213.   
  214.     }else  
  215.     {  
  216.         block->point.setX(x+factor);  
  217.     }  
  218.   
  219.     if(block->yDecrease)  
  220.     {  
  221.         block->point.setY(y-factor);  
  222.   
  223.     }else  
  224.     {  
  225.         block->point.setY(y+factor);  
  226.     }  
  227.  }  
  228.   
  229.   
  230. const QString ReactionGame::GetIQHint(const float score)  
  231. {  
  232.     if(score<20)  
  233.         return tr("Haha, oh you only for fuck off!! As relatively cool *_*");//哈哈,你只适合打飞机哦!!那样比较爽..  
  234.     else if(score>20&&score<=30)  
  235.         return tr("Hey. Your life can only do a cannon fodder! -_-!");//哎...你的人生只能做炮灰了!  
  236.     else if(score>30&&score<=40)  
  237.         return tr("Wow! Is designed to play the pieces of the original idol Oh! :)");//哇!原来是专门打残局的偶像哦!:)  
  238.     else if(score>40)  
  239.         return tr("You'll never who they think are cheating! Or face it! :(");//你一辈子都被他们认为是作弊的!还是面对现实吧! :(  
  240.   
  241. }  
  242.   
  243. bool ReactionGame::HasCollide(const BlockAttr *staticBlock, const BlockAttr *dynamicBlock)//是否产生碰撞  
  244. {  
  245.     QPoint staticCenter(staticBlock->point.x()+staticBlock->pixmap.width()/2,staticBlock->point.y()+staticBlock->pixmap.height()/2);//计算中心点  
  246.     QPoint dynamicCenter(dynamicBlock->point.x()+dynamicBlock->pixmap.width()/2,dynamicBlock->point.y()+dynamicBlock->pixmap.height()/2);  
  247.   
  248.     int staticWidth=staticBlock->pixmap.width();//计算矩形宽度和高度  
  249.     int staticHeiget=staticBlock->pixmap.height();  
  250.   
  251.     int dynamicWidth=dynamicBlock->pixmap.width();  
  252.     int dynamicHeight=dynamicBlock->pixmap.height();  
  253.   
  254.     int xDistance=abs(staticCenter.x()-dynamicCenter.x());//计算。两个矩形中心点的距离  
  255.     int yDistance=abs(staticCenter.y()-dynamicCenter.y());  
  256.   
  257.     if((staticWidth+dynamicWidth)/2>=xDistance&&(staticHeiget+dynamicHeight)/2>=yDistance)//计算是否产生碰撞  
  258.     {  
  259.         return true;  
  260.     }  
  261.   
  262.     return false;  
  263. }  
  264.   
  265. bool ReactionGame::CheckValidBound(const BlockAttr *block)//验证是否在有效范围  
  266. {  
  267.     QPoint blockCenter(block->point.x()+block->pixmap.width()/2,block->point.y()+block->pixmap.height()/2);//计算中心点  
  268.     QPoint rectCenter(validRect.x()+validRect.width()/2,validRect.y()+validRect.height()/2);  
  269.   
  270.     int xDistance=validRect.width()/2;//计算中心到无效区域的距离  
  271.     int yDistance=validRect.height()/2;  
  272.   
  273.     if(abs(blockCenter.x()-rectCenter.x())+block->pixmap.width()/2-2>=xDistance||abs(blockCenter.y()-rectCenter.y())+block->pixmap.height()/2-2>=yDistance)  
  274.     {  
  275.         return false;  
  276.     }  
  277.   
  278.     return true;  
  279. }  
  280.   
  281. void ReactionGame::NarrowValidRect(QRect *rect)//缩小矩形  
  282. {  
  283.     int narrowFactor=10;  
  284.     rect->setX(rect->x()+narrowFactor);  
  285.     rect->setY(rect->y()+narrowFactor);  
  286.     rect->setWidth(rect->width()-narrowFactor);  
  287.     rect->setHeight(rect->height()-narrowFactor);  

0 0