Qt贪吃蛇(代码裸写,不用creator)

 先上个图哇:


图1,第一关视图


图2,第二关视图

游戏说明:

(1).每关只需吃20颗红果即可通关

(2).增加新场景,在第二关出现

(3).增加刚打开程序READY状态,按空格开始

(4).增加暂停功能,在程序运行中按空格暂停,再按空格继续游戏

(5).只要不关闭应用程序,会保留关数,即在X关输了也是从X关开始

(6).每隔两关速度增快一次,场景重复出现


        其实我就做了两个场景,但是是无数关(速度变快~嘿嘿),因为两个场景已经做到,再加场景只是体力活了哈,就不去搞了。这是完全跨平台的,win下的图我就不截了,下面是代码:

main.cpp

[cpp]  view plain  copy
  1. #include<QApplication>  
  2. #include"paint.h"  
  3. int main(int argc,char**argv)  
  4. {  
  5.     QApplication app(argc,argv);  
  6.     paint *sb=new paint;  
  7.     sb->show();  
  8.     return app.exec();  
  9. }  
paint.h

[cpp]  view plain  copy
  1. #ifndef PAINT_H  
  2. #define PAINT_H  
  3. #include<QWidget>  
  4. #include<QPaintEvent>  
  5. #include<QKeyEvent>  
  6. #include<QTimer>  
  7. #include<QProgressBar>  
  8.   
  9. enum Direct  
  10. {  
  11.     Up,  
  12.     Down,  
  13.     Left,  
  14.     Right  
  15. };  
  16. enum Result  
  17. {  
  18.     Win,  
  19.     Lose,  
  20.     Normal,  
  21.     Ready  
  22. };  
  23. class paint:public QWidget  
  24. {  
  25.     Q_OBJECT  
  26.     public:  
  27.         paint(QWidget*parent=0);  
  28.         ~paint();  
  29.         void init();  
  30.         int getrandnum();  
  31.     protected:  
  32.         void paintEvent(QPaintEvent* );  
  33.         void keyPressEvent(QKeyEvent *keyevent);  
  34.     public slots://protected better??  
  35.         void autorun();  
  36.     private:  
  37.         int snake[100][2];  
  38.         int food[2];  
  39.         int length;  
  40.         Result res;  
  41.         Direct dir;  
  42.         QTimer *timer;  
  43.         int count;  
  44.         int level;  
  45.         int speed;  
  46.         int randsuanzi;  
  47.         int jiou;  
  48.         QProgressBar *progress;  
  49. };  
  50. #endif  

paint.cpp

[cpp]  view plain  copy
  1. #include"paint.h"  
  2. #include<QtGui>  
  3. #define COUNTRANGE 20  
  4. paint::paint(QWidget*parent):QWidget(parent)  
  5. {  
  6.     level=1;  
  7.     speed=200;  
  8.     init();  
  9.     setWindowTitle("little snake v0.2");  
  10.     timer=new QTimer;  
  11.     timer->start(speed);  
  12.     connect(timer,SIGNAL(timeout()),this,SLOT(autorun()));  
  13.     setFixedSize(360,410);  
  14.     progress=new QProgressBar;  
  15.     progress->setRange(0,COUNTRANGE);  
  16.     progress->setValue(count);  
  17.     progress->setOrientation(Qt::Horizontal);  
  18.     QVBoxLayout *main=new QVBoxLayout;  
  19.     main->addStretch(2);  
  20.     main->addWidget(progress);  
  21.     setLayout(main);  
  22.   
  23. }  
  24.   
  25. paint::~paint(){}  
  26.   
  27. void paint::init()  
  28. {  
  29.     count=0;  
  30.     length=4;  
  31.     randsuanzi=0;  
  32.     jiou=0;  
  33.     dir=Up;  
  34.     res=Ready;  
  35.     snake[0][0]=20*1;  
  36.     snake[0][1]=20*13;  
  37.     snake[1][0]=20*1;  
  38.     snake[1][1]=20*14;  
  39.     snake[2][0]=20*1;  
  40.     snake[2][1]=20*15;  
  41.     snake[3][0]=20*1;  
  42.     snake[3][1]=20*16;  
  43.       
  44.     food[0]=(getrandnum()+1)*20;  
  45.     food[1]=(getrandnum()+1)*20;  
  46.         for(int i=0;i<length;++i)  
  47.         {  
  48.             if(level%2)  
  49.             {  
  50.                 if((food[0]-snake[i][0]==0)  
  51.                     &&(food[1]-snake[i][1]==0))  
  52.                 {  
  53.                     food[0]=(getrandnum()+1)*20;  
  54.                     food[1]=(getrandnum()+1)*20;  
  55.                     i=-1;  
  56.                     continue;  
  57.                 }  
  58.             }  
  59.             else  
  60.             {  
  61.                 if(((food[0]==snake[i][0])&&(food[1]==snake[i][1]))  
  62.                         ||((food[0]==5*20)&&(food[1]>=7*20)&&(food[1]<=10*20))  
  63.                         ||((food[0]==12*20)&&(food[1]>=7*20)&&(food[1]<=10*20))  
  64.                         ||((food[1]==5*20)&&(food[0]>=7*20)&&(food[0]<=10*20))  
  65.                         ||((food[1]==12*20)&&(food[0]>=7*20)&&(food[0]<=10*20)))  
  66.                 {  
  67.                     food[0]=(getrandnum()+1)*20;  
  68.                     food[1]=(getrandnum()+1)*20;  
  69.                     i=-1;  
  70.                     continue;  
  71.                 }  
  72.             }  
  73.         }  
  74. }  
  75. int paint::getrandnum()  
  76. {  
  77.     randsuanzi++;  
  78.     srand(time(0)+randsuanzi);  
  79.     if(randsuanzi>9999)  
  80.         randsuanzi=0;  
  81.     return rand()%16;//0~15   
  82. }  
  83. void paint::paintEvent(QPaintEvent* )  
  84. {  
  85.     QPainter p(this);  
  86.         QRectF border(20-20,20-20,16*20+40,16*20+40);  
  87.         QRectF inter(20,20,16*20,16*20);  
  88.         p.setPen(Qt::NoPen);  
  89.     if(level%2)  
  90.     {  
  91.         p.setBrush(QBrush(Qt::darkGreen,Qt::SolidPattern));  
  92.     }  
  93.     else  
  94.     {  
  95.         p.setBrush(QBrush(Qt::darkCyan,Qt::SolidPattern));  
  96.     }  
  97.         p.drawRect(border);  
  98.         p.setBrush(QBrush(Qt::lightGray,Qt::SolidPattern));  
  99.         p.drawRect(inter);  
  100.         p.setPen(QPen(Qt::darkGray,1,Qt::SolidLine,Qt::RoundCap,Qt::RoundJoin));  
  101.         p.setBrush(QBrush(Qt::green));  
  102.         QRectF snakehead(snake[0][0],snake[0][1],20,20);  
  103.         p.drawRoundRect(snakehead);  
  104.           
  105.         QRectF snakefood(food[0],food[1],20,20);  
  106.         p.setBrush(QBrush(Qt::darkRed));  
  107.         p.drawEllipse(snakefood);  
  108.           
  109.         for(int i=1;i<length;++i)  
  110.         {  
  111.             QRectF snakebody(snake[i][0],snake[i][1],20,20);  
  112.             p.setBrush(QBrush(Qt::red));  
  113.             p.drawRoundRect(snakebody);  
  114.         }  
  115.         QRectF levelinfo(12*20,20,80,20);  
  116.         if(level%2)  
  117.         {  
  118.             p.setPen(QPen(Qt::darkGreen));  
  119.         }  
  120.         else  
  121.         {  
  122.             p.setPen(QPen(Qt::darkCyan));  
  123.         }  
  124.         QString levelnum=QString::number(level,10);  
  125.         QString levelstr="level ";  
  126.         levelstr.append(levelnum);  
  127.         p.drawText(levelinfo,Qt::AlignRight,levelstr);  
  128.   
  129.         QRectF xiaolei(9*20,0,8*20,20);  
  130.         p.setPen(QPen(Qt::lightGray));  
  131.         p.drawText(xiaolei,Qt::AlignRight,"made by kanglei");  
  132.   
  133.       
  134.     if(!(level%2))  
  135.     {  
  136.         QRectF block1(5*20,7*20,20,4*20);  
  137.         p.setBrush(QBrush(Qt::darkCyan));  
  138.         p.drawRect(block1);  
  139.         QRectF block2(7*20,5*20,4*20,20);  
  140.         p.setBrush(QBrush(Qt::darkCyan));  
  141.         p.drawRect(block2);  
  142.         QRectF block3(12*20,7*20,20,4*20);  
  143.         p.setBrush(QBrush(Qt::darkCyan));  
  144.         p.drawRect(block3);  
  145.         QRectF block4(7*20,12*20,4*20,20);  
  146.         p.setBrush(QBrush(Qt::darkCyan));  
  147.         p.drawRect(block4);  
  148.         QRectF block_add1(0,8*20,20,40);  
  149.         p.setBrush(QBrush(Qt::lightGray));  
  150.         p.drawRect(block_add1);  
  151.         QRectF block_add2(17*20,8*20,20,40);  
  152.         p.setBrush(QBrush(Qt::lightGray));  
  153.         p.drawRect(block_add2);  
  154.         QRectF block_add3(8*20,0,40,20);  
  155.         p.setBrush(QBrush(Qt::lightGray));  
  156.         p.drawRect(block_add3);  
  157.         QRectF block_add4(8*20,17*20,40,20);  
  158.         p.setBrush(QBrush(Qt::lightGray));  
  159.         p.drawRect(block_add4);  
  160.     }  
  161.     if(res==Ready)  
  162.     {     
  163.         p.setFont(QFont("",16,16));  
  164.         p.setPen(QPen(Qt::yellow,60));  
  165.         QRectF text(50,50,16*20-50,16*20-50);  
  166.         p.drawText(text,Qt::AlignCenter,"Ready?\nPress Space start");  
  167.     }  
  168.     if(res==Lose)  
  169.     {  
  170.             p.setFont(QFont("",16,16));  
  171.             p.setPen(QPen(Qt::yellow,60));  
  172.             QRectF text(50,50,16*20-50,16*20-50);  
  173.             p.drawText(text,Qt::AlignCenter,"LOSE!!\nPress Space restart");  
  174.     }  
  175.     if(res==Win)  
  176.     {  
  177.         p.setFont(QFont("",16,16));  
  178.         p.setPen(QPen(Qt::yellow,60));  
  179.         QRectF text(50,50,16*20-50,16*20-50);  
  180.         p.drawText(text,Qt::AlignCenter,"WIN!!");  
  181.     }  
  182.     if((res==Normal)&&(jiou))  
  183.     {  
  184.         p.setFont(QFont("",16,16));  
  185.         p.setPen(QPen(Qt::yellow,60));  
  186.         QRectF text(50,50,16*20-50,16*20-50);  
  187.         p.drawText(text,Qt::AlignCenter,"Pause");  
  188.     }  
  189. }  
  190.   
  191. void paint::keyPressEvent(QKeyEvent* keyevent)  
  192. {  
  193.     switch(keyevent->key())  
  194.     {  
  195.         case Qt::Key_Up:  
  196.             dir=Up;  
  197.             break;  
  198.         case Qt::Key_Down:  
  199.             dir=Down;  
  200.             break;  
  201.         case Qt::Key_Left:  
  202.             dir=Left;  
  203.             break;  
  204.         case Qt::Key_Right:  
  205.             dir=Right;  
  206.             break;  
  207.         case Qt::Key_Space:  
  208.             if(res==Ready)//start  
  209.             {  
  210.                 res=Normal;  
  211.                 break;  
  212.             }  
  213.             if(res==Lose)//restart  
  214.             {  
  215.                 init();  
  216.                 progress->setValue(count);  
  217.                 update();  
  218.                 break;  
  219.             }  
  220.             if(res==Normal)//pause  
  221.             {  
  222.                 if(!jiou)  
  223.                 {  
  224.                     timer->stop();  
  225.                     jiou=1;  
  226.                 }  
  227.                 else  
  228.                 {  
  229.                     timer->start();  
  230.                     jiou=0;  
  231.                 }  
  232.                 update();  
  233.                 break;  
  234.             }  
  235.     }  
  236. }  
  237. void paint::autorun()  
  238. {  
  239. if(res==Normal)  
  240. {  
  241.     int temp[2];  
  242.     temp[0]=snake[length-1][0];  
  243.     temp[1]=snake[length-1][1];  
  244.     for(int i=length-1;i>0;--i)  
  245.     {  
  246.         snake[i][0]=snake[i-1][0];  
  247.         snake[i][1]=snake[i-1][1];  
  248.     }  
  249.     if(dir==Up)  
  250.     {  
  251.         snake[0][1]-=20;  
  252.         if(snake[0][1]==snake[2][1])  
  253.         {  
  254.             snake[0][1]+=40;  
  255.         }  
  256.         if(snake[0][1]<20||snake[0][1]>16*20)  
  257.         {  
  258.             if(level%2)//1,3,5...  
  259.                 res=Lose;     
  260.             else  
  261.             {  
  262.                 if(((snake[0][0]==8*20)  
  263.                             ||(snake[0][0]==9*20))  
  264.                             &&(snake[0][1]==0))  
  265.                 {  
  266.                     snake[0][1]=16*20;  
  267.                 }  
  268.                 else if(((snake[0][0]==8*20)  
  269.                             ||(snake[0][0]==9*20))  
  270.                         &&(snake[0][1]==17*20))  
  271.                 {  
  272.                     snake[0][1]=20;  
  273.                 }  
  274.                 else  
  275.                     res=Lose;  
  276.             }  
  277.         }  
  278.     }  
  279.     if(dir==Down)  
  280.     {  
  281.         snake[0][1]+=20;  
  282.         if(snake[0][1]==snake[2][1])  
  283.         {  
  284.             snake[0][1]-=40;  
  285.         }  
  286.         if(snake[0][1]<20||snake[0][1]>16*20)  
  287.         {  
  288.             if(level%2)  
  289.                 res=Lose;  
  290.             else  
  291.             {  
  292.                 if(((snake[0][0]==8*20)  
  293.                             ||(snake[0][0]==9*20))  
  294.                             &&(snake[0][1]==0))  
  295.                 {  
  296.                     snake[0][1]=16*20;  
  297.                 }  
  298.                 else if(((snake[0][0]==8*20)  
  299.                             ||(snake[0][0]==9*20))  
  300.                         &&(snake[0][1]==17*20))  
  301.                 {  
  302.                     snake[0][1]=20;  
  303.                 }  
  304.                 else  
  305.                     res=Lose;  
  306.             }  
  307.         }  
  308.     }  
  309.     if(dir==Left)  
  310.     {  
  311.         snake[0][0]-=20;  
  312.         if(snake[0][0]==snake[2][0])  
  313.         {  
  314.             snake[0][0]+=40;  
  315.         }  
  316.         if(snake[0][0]<20||snake[0][0]>16*20)  
  317.         {  
  318.             if(level%2)  
  319.                 res=Lose;  
  320.             else  
  321.             {  
  322.                 if(((snake[0][1]==8*20)  
  323.                             ||(snake[0][1]==9*20))  
  324.                         &&(snake[0][0]==0))  
  325.                 {  
  326.                     snake[0][0]=16*20;  
  327.                 }  
  328.                 else if(((snake[0][1]==8*20)  
  329.                             ||(snake[0][1]==9*20))  
  330.                         &&(snake[0][0]==17*20))  
  331.                 {  
  332.                     snake[0][0]=20;  
  333.                 }  
  334.                 else  
  335.                     res=Lose;  
  336.             }  
  337.         }  
  338.     }  
  339.     if(dir==Right)  
  340.     {  
  341.         snake[0][0]+=20;  
  342.         if(snake[0][0]==snake[2][0])  
  343.         {  
  344.             snake[0][0]-=40;  
  345.         }  
  346.         if(snake[0][0]<20||snake[0][0]>16*20)  
  347.         {  
  348.             if(level%2)  
  349.                 res=Lose;  
  350.             else  
  351.             {  
  352.                 if(((snake[0][1]==8*20)  
  353.                             ||(snake[0][1]==9*20))  
  354.                         &&(snake[0][0]==0))  
  355.                 {  
  356.                     snake[0][0]=16*20;  
  357.                 }  
  358.                 else if(((snake[0][1]==8*20)  
  359.                             ||(snake[0][1]==9*20))  
  360.                         &&(snake[0][0]==17*20))  
  361.                 {  
  362.                     snake[0][0]=20;  
  363.                 }  
  364.                 else  
  365.                     res=Lose;  
  366.             }  
  367.         }  
  368.     }  
  369.     for(int i=1;i<length;++i)  
  370.     {  
  371.         if((snake[0][0]-snake[i][0]==0)\  
  372.                 &&(snake[0][1]-snake[i][1]==0))  
  373.         {  
  374.             res=Lose;  
  375.         }  
  376.     }  
  377.     if(!(level%2))//2,4,6....  
  378.     {  
  379.         if((snake[0][0]==5*20)  
  380.             &&(snake[0][1]>=7*20)  
  381.                     &&(snake[0][1]<=10*20))  
  382.         {  
  383.             res=Lose;  
  384.         }  
  385.         if((snake[0][0]==12*20)  
  386.             &&(snake[0][1]>=7*20)  
  387.                     &&(snake[0][1]<=10*20))  
  388.         {  
  389.             res=Lose;  
  390.         }  
  391.         if((snake[0][1]==5*20)  
  392.             &&(snake[0][0]>=7*20)  
  393.                     &&(snake[0][0]<=10*20))  
  394.         {  
  395.             res=Lose;  
  396.         }  
  397.         if((snake[0][1]==12*20)  
  398.             &&(snake[0][0]>=7*20)  
  399.                     &&(snake[0][0]<=10*20))  
  400.         {  
  401.             res=Lose;  
  402.         }  
  403.     }  
  404.     if((snake[0][0]==food[0])&&(snake[0][1]==food[1]))  
  405.     {  
  406.         count++;  
  407.         progress->setValue(count);  
  408.         length++;  
  409.         snake[length-1][0]=temp[0];  
  410.         snake[length-1][1]=temp[1];  
  411.         food[0]=(getrandnum()+1)*20;  
  412.         food[1]=(getrandnum()+1)*20;  
  413.         for(int i=0;i<length;++i)  
  414.         {  
  415.             if(level%2)  
  416.             {  
  417.                 if((food[0]-snake[i][0]==0)\  
  418.                         &&(food[1]-snake[i][1]==0))  
  419.                 {  
  420.                     food[0]=(getrandnum()+1)*20;  
  421.                     food[1]=(getrandnum()+1)*20;  
  422.                     i=-1;  
  423.                     continue;  
  424.                 }  
  425.             }  
  426.             else  
  427.             {  
  428.                 if(((food[0]==snake[i][0])&&(food[1]==snake[i][1]))  
  429.                         ||((food[0]==5*20)&&(food[1]>=7*20)&&(food[1]<=10*20))  
  430.                         ||((food[0]==12*20)&&(food[1]>=7*20)&&(food[1]<=10*20))  
  431.                         ||((food[1]==5*20)&&(food[0]>=7*20)&&(food[0]<=10*20))  
  432.                         ||((food[1]==12*20)&&(food[0]>=7*20)&&(food[0]<=10*20)))  
  433.                 {  
  434.                     food[0]=(getrandnum()+1)*20;  
  435.                     food[1]=(getrandnum()+1)*20;  
  436.                     i=-1;  
  437.                     continue;  
  438.                 }  
  439.             }  
  440.         }  
  441.         if(count>=COUNTRANGE)  
  442.         {  
  443.             level++;  
  444.             if(level%2)  
  445.                 speed=speed-50;  
  446.             if(speed<=0)  
  447.                 speed=20;  
  448.             timer->start(speed);  
  449.             init();  
  450.             progress->setValue(count);  
  451.         }  
  452.     }  
  453.     update();  
  454. }  
  455. }  

        代码很简单,我觉得写的也很条理,应该很清晰哈~用Qt creator确实可以快捷开发,但是搞的和visual studio一样,就没有意思了。所以我坚持vim下代码写,也很方便,400多行小游戏,很开心~哈哈~

转载请注明:转自 http://blog.youkuaiyun.com/littlethunder/article/details/8916987

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值