凤凰平台注册开户_凤凰彩票app下载安装_凤凰彩票投注网

热门关键词: 凤凰平台注册开户,凤凰彩票app下载安装,凤凰彩票投注网

QT贯彻两条贪吃蛇

作者: 编程知识  发布:2019-09-26

Snake.pro文件

QT实现两条贪吃蛇,qt两条贪吃蛇

Snake.pro文件

 1 #-------------------------------------------------
 2 #
 3 # Project created by QtCreator 2017-12-11T22:59:40
 4 #
 5 #-------------------------------------------------
 6 
 7 QT       += core gui
 8 
 9 greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
10 
11 TARGET = 02_Snake
12 TEMPLATE = app
13 
14 # The following define makes your compiler emit warnings if you use
15 # any feature of Qt which as been marked as deprecated (the exact warnings
16 # depend on your compiler). Please consult the documentation of the
17 # deprecated API in order to know how to port your code away from it.
18 DEFINES += QT_DEPRECATED_WARNINGS
19 
20 # You can also make your code fail to compile if you use deprecated APIs.
21 # In order to do so, uncomment the following line.
22 # You can also select to disable deprecated APIs only up to a certain version of Qt.
23 #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
24 
25 
26 SOURCES += main.cpp
27         widget.cpp 
28     mythread.cpp
29 
30 HEADERS  += widget.h 
31     mythread.h 
32     allparameter.h
33 
34 FORMS    += widget.ui
35 
36 CONFIG +=C++11

头文件

allparameter.h

 1 #ifndef ALLPARAMETER_H
 2 #define ALLPARAMETER_H
 3 
 4 
 5 extern QVector<QRect>  rects;
 6 extern QVector<QRect>  rectsfight;
 7 extern QRect newrect;
 8 
 9 extern int state,statefight;
10 extern int begin[2];
11 extern int g_length,g_width,g_lines,g_rows;
12 
13 
14 
15 #endif // ALLPARAMETER_H

mythread.h

 1 #ifndef MYTHREAD_H
 2 #define MYTHREAD_H
 3 
 4 #include <QObject>
 5 #include<QImage>
 6 
 7 class MyThread : public QObject
 8 {
 9     Q_OBJECT
10 public:
11     explicit MyThread(QObject *parent = 0);
12     void DrawImage();
13 
14 signals:
15     void UpdateImage(QImage temp);
16     void Touch();
17 
18 public slots:
19 
20 private:
21     QImage image;
22 
23 };
24 
25 #endif // MYTHREAD_H

widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include<QThread>
#include<QTimer>
#include"mythread.h"
#include<QPaintEvent>
namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = 0);
    ~Widget();

protected:
    void paintEvent(QPaintEvent *event);
    void dealUpdateImage(QImage);
    void closeThread();

    void keyPressEvent(QKeyEvent *event);
    void Paint();

private slots:
    void on_pushButton_clicked();

    void on_pushButton_2_clicked();

private:
    Ui::Widget *ui;
    MyThread *MyT;
    QThread *thread;
    QTimer *time_1;
    QImage image;
    int timeset;

};

#endif // WIDGET_H

源文件

main.cpp

 1 #include "widget.h"
 2 #include <QApplication>
 3 
 4 int main(int argc, char *argv[])
 5 {
 6     QApplication a(argc, argv);
 7     Widget w;
 8     w.show();
 9 
10     return a.exec();
11 }

凤凰彩票app下载安装,mythread.cpp

  1 #include "mythread.h"
  2 #include<QPainter>
  3 #include<allparameter.h>
  4 #include<QBrush>
  5 #include<QPen>
  6 
  7 
  8 MyThread::MyThread(QObject *parent) : QObject(parent)
  9 {
 10 
 11 }
 12 
 13 void MyThread::DrawImage()
 14 {
 15     switch(state)
 16     {
 17     case 0:
 18         if(((rects[rects.size()-1]).left()==newrect.left())&&(rects[rects.size()-1].top()-g_width==newrect.top()))
 19             {
 20            rects.push_back(newrect);
 21            bool con=1;
 22             int a,b;
 23            while(con)
 24            {
 25                a=rand()%(g_rows);
 26               b=rand()%(g_lines);
 27               if(!(rects.contains(QRect(begin[0]+g_length*a,begin[1]+g_width*b,g_length,g_width)))&&!(rectsfight.contains(QRect(begin[0]+g_length*a,begin[1]+g_width*b,g_length,g_width))))
 28                   con=0;
 29            }
 30                  newrect=QRect(begin[0]+g_length*a,begin[1]+g_width*b,g_length,g_width);
 31            }
 32         rects.push_back(QRect((rects[rects.size()-1].left()),(rects[rects.size()-1].top())-g_width,g_length,g_width));
 33         break;
 34     case 1:
 35         if(((rects[rects.size()-1]).left()-g_length==newrect.left())&&(rects[rects.size()-1].top()==newrect.top()))
 36             {
 37            rects.push_back(newrect);
 38            bool con=1;
 39             int a,b;
 40            while(con)
 41            {
 42                a=rand()%(g_rows);
 43               b=rand()%(g_lines);
 44                if(!(rects.contains(QRect(begin[0]+g_length*a,begin[1]+g_width*b,g_length,g_width)))&&!(rectsfight.contains(QRect(begin[0]+g_length*a,begin[1]+g_width*b,g_length,g_width))))
 45                   con=0;
 46            }
 47                  newrect=QRect(begin[0]+g_length*a,begin[1]+g_width*b,g_length,g_width);
 48            }
 49         rects.push_back(QRect((rects[rects.size()-1].left())-g_length,(rects[rects.size()-1].top()),g_length,g_width));
 50         break;
 51     case 2:
 52         if(((rects[rects.size()-1]).left()==newrect.left())&&(rects[rects.size()-1].top()+g_width==newrect.top()))
 53             {
 54            rects.push_back(newrect);
 55            bool con=1;
 56             int a,b;
 57            while(con)
 58            {
 59                a=rand()%(g_rows);
 60               b=rand()%(g_lines);
 61               if(!(rects.contains(QRect(begin[0]+g_length*a,begin[1]+g_width*b,g_length,g_width)))&&!(rectsfight.contains(QRect(begin[0]+g_length*a,begin[1]+g_width*b,g_length,g_width))))
 62                   con=0;
 63            }
 64                  newrect=QRect(begin[0]+g_length*a,begin[1]+g_width*b,g_length,g_width);
 65            }
 66         rects.push_back(QRect((rects[rects.size()-1].left()),(rects[rects.size()-1].top())+g_width,g_length,g_width));
 67         break;
 68     case 3:
 69         if(((rects[rects.size()-1]).left()+g_length==newrect.left())&&(rects[rects.size()-1].top()==newrect.top()))
 70             {
 71            rects.push_back(newrect);
 72            bool con=1;
 73             int a,b;
 74            while(con)
 75            {
 76                a=rand()%(g_rows);
 77               b=rand()%(g_lines);
 78               if(!(rects.contains(QRect(begin[0]+g_length*a,begin[1]+g_width*b,g_length,g_width)))&&!(rectsfight.contains(QRect(begin[0]+g_length*a,begin[1]+g_width*b,g_length,g_width))))
 79                   con=0;
 80            }
 81                  newrect=QRect(begin[0]+g_length*a,begin[1]+g_width*b,g_length,g_width);
 82            }
 83         rects.push_back(QRect((rects[rects.size()-1].left())+g_length,(rects[rects.size()-1].top()),g_length,g_width));
 84         break;
 85 
 86 
 87 
 88 
 89 
 90      }
 91     rects.removeAt(0);
 92 
 93 
 94 
 95     switch(statefight)
 96          {
 97          case 0:
 98              if(((rectsfight[rectsfight.size()-1]).left()==newrect.left())&&(rectsfight[rectsfight.size()-1].top()-g_width==newrect.top()))
 99                  {
100                 rectsfight.push_back(newrect);
101                 bool con=1;
102                  int a,b;
103                 while(con)
104                 {
105                     a=rand()%(g_rows);
106                    b=rand()%(g_lines);
107                    if(!(rectsfight.contains(QRect(begin[0]+g_length*a,begin[1]+g_width*b,g_length,g_width)))&&!(rectsfight.contains(QRect(begin[0]+g_length*a,begin[1]+g_width*b,g_length,g_width))))
108                        con=0;
109                 }
110                       newrect=QRect(begin[0]+g_length*a,begin[1]+g_width*b,g_length,g_width);
111                 }
112              rectsfight.push_back(QRect((rectsfight[rectsfight.size()-1].left()),(rectsfight[rectsfight.size()-1].top())-g_width,g_length,g_width));
113              break;
114          case 1:
115              if(((rectsfight[rectsfight.size()-1]).left()-g_length==newrect.left())&&(rectsfight[rectsfight.size()-1].top()==newrect.top()))
116                  {
117                 rectsfight.push_back(newrect);
118                 bool con=1;
119                  int a,b;
120                 while(con)
121                 {
122                     a=rand()%(g_rows);
123                    b=rand()%(g_lines);
124                     if(!(rectsfight.contains(QRect(begin[0]+g_length*a,begin[1]+g_width*b,g_length,g_width)))&&!(rectsfight.contains(QRect(begin[0]+g_length*a,begin[1]+g_width*b,g_length,g_width))))
125                        con=0;
126                 }
127                       newrect=QRect(begin[0]+g_length*a,begin[1]+g_width*b,g_length,g_width);
128                 }
129              rectsfight.push_back(QRect((rectsfight[rectsfight.size()-1].left())-g_length,(rectsfight[rectsfight.size()-1].top()),g_length,g_width));
130              break;
131          case 2:
132              if(((rectsfight[rectsfight.size()-1]).left()==newrect.left())&&(rectsfight[rectsfight.size()-1].top()+g_width==newrect.top()))
133                  {
134                 rectsfight.push_back(newrect);
135                 bool con=1;
136                  int a,b;
137                 while(con)
138                 {
139                     a=rand()%(g_rows);
140                    b=rand()%(g_lines);
141                    if(!(rectsfight.contains(QRect(begin[0]+g_length*a,begin[1]+g_width*b,g_length,g_width)))&&!(rectsfight.contains(QRect(begin[0]+g_length*a,begin[1]+g_width*b,g_length,g_width))))
142                        con=0;
143                 }
144                       newrect=QRect(begin[0]+g_length*a,begin[1]+g_width*b,g_length,g_width);
145                 }
146              rectsfight.push_back(QRect((rectsfight[rectsfight.size()-1].left()),(rectsfight[rectsfight.size()-1].top())+g_width,g_length,g_width));
147              break;
148          case 3:
149              if(((rectsfight[rectsfight.size()-1]).left()+g_length==newrect.left())&&(rectsfight[rectsfight.size()-1].top()==newrect.top()))
150                  {
151                 rectsfight.push_back(newrect);
152                 bool con=1;
153                  int a,b;
154                 while(con)
155                 {
156                     a=rand()%(g_rows);
157                    b=rand()%(g_lines);
158                    if(!(rectsfight.contains(QRect(begin[0]+g_length*a,begin[1]+g_width*b,g_length,g_width)))&&!(rectsfight.contains(QRect(begin[0]+g_length*a,begin[1]+g_width*b,g_length,g_width))))
159                        con=0;
160                 }
161                       newrect=QRect(begin[0]+g_length*a,begin[1]+g_width*b,g_length,g_width);
162                 }
163              rectsfight.push_back(QRect((rectsfight[rectsfight.size()-1].left())+g_length,(rectsfight[rectsfight.size()-1].top()),g_length,g_width));
164              break;
165           }
166          rectsfight.removeAt(0);
167 
168          if(
169                  ((rects[rects.size()-1]).left()==begin[0]-g_length)
170                  ||((rects[rects.size()-1]).left()==(begin[0]+g_rows*g_length))
171                  ||((rects[rects.size()-1]).top()==begin[1]-g_width)
172                  ||((rects[rects.size()-1]).top()==(begin[1]+g_width*g_lines))
173                  ||((rectsfight[rectsfight.size()-1]).left()==begin[0]-g_length)
174                  ||((rectsfight[rectsfight.size()-1]).left()==(begin[0]+g_rows*g_length))
175                  ||((rectsfight[rectsfight.size()-1]).top()==begin[1]-g_width)
176                  ||((rectsfight[rectsfight.size()-1]).top()==(begin[1]+g_width*g_lines))
177               )
178          {
179 
180             // time_1->stop();
181             emit Touch();
182              rects.clear();
183              rectsfight.clear();
184              rects.push_back(QRect(begin[0]+g_length*(g_rows/4),begin[1]+g_width*(g_lines/4),g_length,g_width));
185              rectsfight.push_back(QRect(begin[0]+g_length*(g_rows*3/4),begin[1]+g_width*(g_lines*3/4),g_length,g_width));
186 
187              bool con=1;
188               int a,b;
189              while(con)
190              {
191                  a=rand()%(g_rows);
192                 b=rand()%(g_lines);
193                 if(!(rects.contains(QRect(begin[0]+g_length*a,begin[1]+g_width*b,g_length,g_width))))
194                     con=0;
195              }
196               newrect=QRect(begin[0]+g_length*a,begin[1]+g_width*b,g_length,g_width);
197 
198         }
199 
200 
201 
202 
203     image=QImage(begin[0]*2+g_length*g_rows,begin[1]+(g_width+3)*g_lines,QImage::Format_ARGB32);
204     QPainter p(&image);
205 
206     QPen pen;
207 
208 
209     p.setPen(pen);
210     QVector<QLine>  lines;
211 
212     for(int i=0;i<=g_lines;i++)
213       {
214         lines.push_back(QLine(begin[0],begin[1]+g_width*i,begin[0]+g_rows*g_length,begin[1]+g_width*i));
215     }
216     for(int i=0;i<=g_rows;i++)
217       {
218         lines.push_back(QLine(begin[0]+g_length*i,begin[1],begin[0]+g_length*i,begin[1]+g_width*g_lines));
219     }
220 
221     p.drawLines(lines);
222     QBrush brush;
223     brush.setColor(Qt::black);
224     brush.setStyle(Qt::SolidPattern);
225     p.setBrush(brush);
226     p.drawRects(rects);
227     brush.setColor(Qt::yellow);
228     p.setBrush(brush);
229     p.drawRects(rectsfight);
230     brush.setColor(Qt::red);
231     p.setBrush(brush);
232     p.drawRect(newrect);
233 
234     emit UpdateImage(image);
235 
236 }

widget.cpp

  1 #include "widget.h"
  2 #include "ui_widget.h"
  3 #include<QPainter>
  4 #include<QPen>
  5 #include<QBrush>
  6 #include<allparameter.h>
  7 #include<QMessageBox>
  8 
  9 QVector<QRect>  rects;
 10 QVector<QRect>  rectsfight;
 11 QRect newrect;
 12 
 13 int state=0,statefight=0;
 14 int begin[2]={20,20};
 15 int g_length=20,g_width=20,g_lines=21,g_rows=21;
 16 
 17 
 18 Widget::Widget(QWidget *parent) :
 19     QWidget(parent),
 20     ui(new Ui::Widget)
 21 {
 22     ui->setupUi(this);
 23 
 24     resize(begin[0]*2+g_length*g_rows,begin[1]+(g_width+3)*g_lines);
 25     ui->pushButton->move((begin[0]+g_length*(g_rows/2-1))-ui->pushButton->width()-ui->label->width(),begin[1]+(g_width+1)*g_lines);
 26     ui->label->move((begin[0]+g_length*(g_rows/2))-ui->label->width()-2,begin[1]+(g_width+1)*g_lines);
 27     ui->lineEdit->move((begin[0]+g_length*(g_rows/2)),begin[1]+(g_width+1)*g_lines+2);
 28     ui->pushButton_2->move((begin[0]+g_length*(g_rows/2+1))+ui->lineEdit->width(),begin[1]+(g_width+1)*g_lines);
 29 
 30 
 31 
 32     ui->lineEdit->setText("300");
 33     ui->lineEdit->resize(40,20);
 34 
 35     timeset=ui->lineEdit->text().toInt();
 36 
 37     srand((unsigned)time(NULL));
 38 
 39     setWindowFlags(Qt::FramelessWindowHint|windowFlags());
 40 
 41     rects.push_back(QRect(begin[0]+g_length*(g_rows/4),begin[1]+g_width*(g_lines/4),g_length,g_width));
 42     rectsfight.push_back(QRect(begin[0]+g_length*(g_rows*3/4),begin[1]+g_width*(g_lines*3/4),g_length,g_width));
 43 
 44 
 45     bool con=1;
 46      int a,b;
 47     while(con)
 48     {
 49         a=rand()%(g_rows);
 50        b=rand()%(g_lines);
 51        if(!(rects.contains(QRect(begin[0]+g_length*a,begin[1]+g_width*b,g_length,g_width))))
 52            con=0;
 53     }
 54      newrect=QRect(begin[0]+g_length*a,begin[1]+g_width*b,g_length,g_width);
 55 
 56 
 57 
 58 
 59 
 60     time_1=new QTimer(this);
 61 
 62 
 63     MyT=new MyThread;
 64     thread=new QThread(this);
 65     MyT->moveToThread(thread);
 66 
 67     thread->start();
 68 
 69     connect(time_1,&QTimer::timeout,MyT,&MyThread::DrawImage);
 70 
 71     connect(MyT,&MyThread::UpdateImage,this,&Widget::dealUpdateImage);
 72 
 73     connect(this,&Widget::destroyed,this,&Widget::closeThread);
 74 
 75     connect(MyT,&MyThread::Touch,this,
 76                         [=]()
 77                         {
 78                                 time_1->stop();
 79                                 QMessageBox message;
 80                                 message.information(this,"提示","你输了");
 81                         }
 82                     );
 83 
 84     Paint();
 85 
 86 
 87 
 88 }
 89 
 90 Widget::~Widget()
 91 {
 92     delete ui;
 93 }
 94 void Widget::closeThread()
 95 {
 96     thread->quit();
 97     thread->wait();
 98 }
 99 
100 
101 void Widget::on_pushButton_clicked()
102 {
103     timeset=ui->lineEdit->text().toInt();
104     if(time_1->isActive()==false)
105     {
106 
107         time_1->start(timeset);
108 
109     }
110     else
111     {
112         time_1->stop();
113     }
114 }
115 
116 void Widget::paintEvent(QPaintEvent *)
117 {
118     QPainter p(this);
119     p.drawImage(0,0,image);
120 
121 }
122 
123 void Widget::dealUpdateImage(QImage temp)
124 {
125     image=temp;
126     update();
127 }
128 
129 
130 
131 void Widget::keyPressEvent(QKeyEvent *event)
132 {
133     switch(event->key())
134     {
135     case 87: state=0;break;
136     case 65: state=1;break;
137     case 83: state=2;break;
138     case 68: state=3;break;
139     case 73: statefight=0;break;
140     case 74: statefight=1;break;
141     case 75: statefight=2;break;
142     case 76: statefight=3;break;
143     }
144 }
145 
146 void Widget::on_pushButton_2_clicked()
147 {
148     closeThread();
149     close();
150 }
151 
152 void Widget::Paint()
153 {
154     image=QImage(500,500,QImage::Format_ARGB32);
155     QPainter p(&image);
156 
157 
158 
159     QPen pen;
160 
161 
162     p.setPen(pen);
163     QVector<QLine>  lines;
164 
165     for(int i=0;i<=g_lines;i++)
166       {
167         lines.push_back(QLine(begin[0],begin[1]+g_width*i,begin[0]+g_rows*g_length,begin[1]+g_width*i));
168     }
169     for(int i=0;i<=g_rows;i++)
170       {
171         lines.push_back(QLine(begin[0]+g_length*i,begin[1],begin[0]+g_length*i,begin[1]+g_width*g_lines));
172     }
173 
174     p.drawLines(lines);
175     QBrush brush;
176     brush.setColor(Qt::black);
177     brush.setStyle(Qt::SolidPattern);
178     p.setBrush(brush);
179     p.drawRects(rects);
180     brush.setColor(Qt::yellow);
181     p.setBrush(brush);
182     p.drawRects(rectsfight);
183     brush.setColor(Qt::red);
184     p.setBrush(brush);
185     p.drawRect(newrect);
186 
187     update();
188 }

 

界面控件

凤凰彩票app下载安装 1

 

最终效果

凤凰彩票app下载安装 2

 

Snake.pro文件 1 #------------------------------------------------- 2 # 3 # Project created by QtCreator 2017-12-11T22:59:40 4 # 5 #---...

 1 #------------------------------------------------- 2 # 3 # Project created by QtCreator 2017-12-11T22:59:40 4 # 5 #------------------------------------------------- 6  7 QT       += core gui 8  9 greaterThan(QT_MAJOR_VERSION, 4): QT += widgets10 11 TARGET = 02_Snake12 TEMPLATE = app13 14 # The following define makes your compiler emit warnings if you use15 # any feature of Qt which as been marked as deprecated (the exact warnings16 # depend on your compiler). Please consult the documentation of the17 # deprecated API in order to know how to port your code away from it.18 DEFINES += QT_DEPRECATED_WARNINGS19 20 # You can also make your code fail to compile if you use deprecated APIs.21 # In order to do so, uncomment the following line.22 # You can also select to disable deprecated APIs only up to a certain version of Qt.23 #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.024 25 26 SOURCES += main.cpp27         widget.cpp 28     mythread.cpp29 30 HEADERS  += widget.h 31     mythread.h 32     allparameter.h33 34 FORMS    += widget.ui35 36 CONFIG +=C++11

头文件

allparameter.h

 1 #ifndef ALLPARAMETER_H 2 #define ALLPARAMETER_H 3  4  5 extern QVector<QRect>  rects; 6 extern QVector<QRect>  rectsfight; 7 extern QRect newrect; 8  9 extern int state,statefight;10 extern int begin[2];11 extern int g_length,g_width,g_lines,g_rows;12 13 14 15 #endif // ALLPARAMETER_H

mythread.h

 1 #ifndef MYTHREAD_H 2 #define MYTHREAD_H 3  4 #include <QObject> 5 #include<QImage> 6  7 class MyThread : public QObject 8 { 9     Q_OBJECT10 public:11     explicit MyThread(QObject *parent = 0);12     void DrawImage();13 14 signals:15     void UpdateImage(QImage temp);16     void Touch();17 18 public slots:19 20 private:21     QImage image;22 23 };24 25 #endif // MYTHREAD_H

widget.h

#ifndef WIDGET_H#define WIDGET_H#include <QWidget>#include<QThread>#include<QTimer>#include"mythread.h"#include<QPaintEvent>namespace Ui {class Widget;}class Widget : public QWidget{    Q_OBJECTpublic:    explicit Widget(QWidget *parent = 0);    ~Widget();protected:    void paintEvent(QPaintEvent *event);    void dealUpdateImage;    void closeThread();    void keyPressEvent(QKeyEvent *event);    void Paint();private slots:    void on_pushButton_clicked();    void on_pushButton_2_clicked();private:    Ui::Widget *ui;    MyThread *MyT;    QThread *thread;    QTimer *time_1;    QImage image;    int timeset;};#endif // WIDGET_H

源文件

main.cpp

 1 #include "widget.h" 2 #include <QApplication> 3  4 int main(int argc, char *argv[]) 5 { 6     QApplication a(argc, argv); 7     Widget w; 8     w.show(); 9 10     return a.exec();11 }

mythread.cpp

  1 #include "mythread.h"  2 #include<QPainter>  3 #include<allparameter.h>  4 #include<QBrush>  5 #include<QPen>  6   7   8 MyThread::MyThread(QObject *parent) : QObject  9 { 10  11 } 12  13 void MyThread::DrawImage() 14 { 15     switch 16     { 17     case 0: 18         if(((rects[rects.size.left()==newrect.left&&(rects[rects.size()-1].top()-g_width==newrect.top 19             { 20            rects.push_back; 21            bool con=1; 22             int a,b; 23            while 24            { 25                a=rand()%; 26               b=rand()%; 27               if(!(rects.contains(QRect(begin[0]+g_length*a,begin[1]+g_width*b,g_length,g_width)))&&!(rectsfight.contains(QRect(begin[0]+g_length*a,begin[1]+g_width*b,g_length,g_width)))) 28                   con=0; 29            } 30                  newrect=QRect(begin[0]+g_length*a,begin[1]+g_width*b,g_length,g_width); 31            } 32         rects.push_back(QRect((rects[rects.size()-1].left,(rects[rects.size()-1].top-g_width,g_length,g_width)); 33         break; 34     case 1: 35         if(((rects[rects.size.left()-g_length==newrect.left&&(rects[rects.size()-1].top()==newrect.top 36             { 37            rects.push_back; 38            bool con=1; 39             int a,b; 40            while 41            { 42                a=rand()%; 43               b=rand()%; 44                if(!(rects.contains(QRect(begin[0]+g_length*a,begin[1]+g_width*b,g_length,g_width)))&&!(rectsfight.contains(QRect(begin[0]+g_length*a,begin[1]+g_width*b,g_length,g_width)))) 45                   con=0; 46            } 47                  newrect=QRect(begin[0]+g_length*a,begin[1]+g_width*b,g_length,g_width); 48            } 49         rects.push_back(QRect((rects[rects.size()-1].left-g_length,(rects[rects.size()-1].top,g_length,g_width)); 50         break; 51     case 2: 52         if(((rects[rects.size.left()==newrect.left&&(rects[rects.size()-1].top()+g_width==newrect.top 53             { 54            rects.push_back; 55            bool con=1; 56             int a,b; 57            while 58            { 59                a=rand()%; 60               b=rand()%; 61               if(!(rects.contains(QRect(begin[0]+g_length*a,begin[1]+g_width*b,g_length,g_width)))&&!(rectsfight.contains(QRect(begin[0]+g_length*a,begin[1]+g_width*b,g_length,g_width)))) 62                   con=0; 63            } 64                  newrect=QRect(begin[0]+g_length*a,begin[1]+g_width*b,g_length,g_width); 65            } 66         rects.push_back(QRect((rects[rects.size()-1].left,(rects[rects.size()-1].top+g_width,g_length,g_width)); 67         break; 68     case 3: 69         if(((rects[rects.size.left()+g_length==newrect.left&&(rects[rects.size()-1].top()==newrect.top 70             { 71            rects.push_back; 72            bool con=1; 73             int a,b; 74            while 75            { 76                a=rand()%; 77               b=rand()%; 78               if(!(rects.contains(QRect(begin[0]+g_length*a,begin[1]+g_width*b,g_length,g_width)))&&!(rectsfight.contains(QRect(begin[0]+g_length*a,begin[1]+g_width*b,g_length,g_width)))) 79                   con=0; 80            } 81                  newrect=QRect(begin[0]+g_length*a,begin[1]+g_width*b,g_length,g_width); 82            } 83         rects.push_back(QRect((rects[rects.size()-1].left+g_length,(rects[rects.size()-1].top,g_length,g_width)); 84         break; 85  86  87  88  89  90      } 91     rects.removeAt; 92  93  94  95     switch(statefight) 96          { 97          case 0: 98              if(((rectsfight[rectsfight.size.left()==newrect.left&&(rectsfight[rectsfight.size()-1].top()-g_width==newrect.top 99                  {100                 rectsfight.push_back;101                 bool con=1;102                  int a,b;103                 while104                 {105                     a=rand()%;106                    b=rand()%;107                    if(!(rectsfight.contains(QRect(begin[0]+g_length*a,begin[1]+g_width*b,g_length,g_width)))&&!(rectsfight.contains(QRect(begin[0]+g_length*a,begin[1]+g_width*b,g_length,g_width))))108                        con=0;109                 }110                       newrect=QRect(begin[0]+g_length*a,begin[1]+g_width*b,g_length,g_width);111                 }112              rectsfight.push_back(QRect((rectsfight[rectsfight.size()-1].left,(rectsfight[rectsfight.size()-1].top-g_width,g_length,g_width));113              break;114          case 1:115              if(((rectsfight[rectsfight.size.left()-g_length==newrect.left&&(rectsfight[rectsfight.size()-1].top()==newrect.top116                  {117                 rectsfight.push_back;118                 bool con=1;119                  int a,b;120                 while121                 {122                     a=rand()%;123                    b=rand()%;124                     if(!(rectsfight.contains(QRect(begin[0]+g_length*a,begin[1]+g_width*b,g_length,g_width)))&&!(rectsfight.contains(QRect(begin[0]+g_length*a,begin[1]+g_width*b,g_length,g_width))))125                        con=0;126                 }127                       newrect=QRect(begin[0]+g_length*a,begin[1]+g_width*b,g_length,g_width);128                 }129              rectsfight.push_back(QRect((rectsfight[rectsfight.size()-1].left-g_length,(rectsfight[rectsfight.size()-1].top,g_length,g_width));130              break;131          case 2:132              if(((rectsfight[rectsfight.size.left()==newrect.left&&(rectsfight[rectsfight.size()-1].top()+g_width==newrect.top133                  {134                 rectsfight.push_back;135                 bool con=1;136                  int a,b;137                 while138                 {139                     a=rand()%;140                    b=rand()%;141                    if(!(rectsfight.contains(QRect(begin[0]+g_length*a,begin[1]+g_width*b,g_length,g_width)))&&!(rectsfight.contains(QRect(begin[0]+g_length*a,begin[1]+g_width*b,g_length,g_width))))142                        con=0;143                 }144                       newrect=QRect(begin[0]+g_length*a,begin[1]+g_width*b,g_length,g_width);145                 }146              rectsfight.push_back(QRect((rectsfight[rectsfight.size()-1].left,(rectsfight[rectsfight.size()-1].top+g_width,g_length,g_width));147              break;148          case 3:149              if(((rectsfight[rectsfight.size.left()+g_length==newrect.left&&(rectsfight[rectsfight.size()-1].top()==newrect.top150                  {151                 rectsfight.push_back;152                 bool con=1;153                  int a,b;154                 while155                 {156                     a=rand()%;157                    b=rand()%;158                    if(!(rectsfight.contains(QRect(begin[0]+g_length*a,begin[1]+g_width*b,g_length,g_width)))&&!(rectsfight.contains(QRect(begin[0]+g_length*a,begin[1]+g_width*b,g_length,g_width))))159                        con=0;160                 }161                       newrect=QRect(begin[0]+g_length*a,begin[1]+g_width*b,g_length,g_width);162                 }163              rectsfight.push_back(QRect((rectsfight[rectsfight.size()-1].left+g_length,(rectsfight[rectsfight.size()-1].top,g_length,g_width));164              break;165           }166          rectsfight.removeAt;167 168          if(169                  ((rects[rects.size.left()==begin[0]-g_length)170                  ||((rects[rects.size.left()==(begin[0]+g_rows*g_length))171                  ||((rects[rects.size.top()==begin[1]-g_width)172                  ||((rects[rects.size.top()==(begin[1]+g_width*g_lines))173                  ||((rectsfight[rectsfight.size.left()==begin[0]-g_length)174                  ||((rectsfight[rectsfight.size.left()==(begin[0]+g_rows*g_length))175                  ||((rectsfight[rectsfight.size.top()==begin[1]-g_width)176                  ||((rectsfight[rectsfight.size.top()==(begin[1]+g_width*g_lines))177               )178          {179 180             // time_1->stop();181             emit Touch();182              rects.clear();183              rectsfight.clear();184              rects.push_back(QRect(begin[0]+g_length*,begin[1]+g_width*(g_lines/4),g_length,g_width));185              rectsfight.push_back(QRect(begin[0]+g_length*(g_rows*3/4),begin[1]+g_width*(g_lines*3/4),g_length,g_width));186 187              bool con=1;188               int a,b;189              while190              {191                  a=rand()%;192                 b=rand()%;193                 if(!(rects.contains(QRect(begin[0]+g_length*a,begin[1]+g_width*b,g_length,g_width))))194                     con=0;195              }196               newrect=QRect(begin[0]+g_length*a,begin[1]+g_width*b,g_length,g_width);197 198         }199 200 201 202 203     image=QImage(begin[0]*2+g_length*g_rows,begin[1]+(g_width+3)*g_lines,QImage::Format_ARGB32);204     QPainter p(&image);205 206     QPen pen;207 208 209     p.setPen;210     QVector<QLine>  lines;211 212     for(int i=0;i<=g_lines;i++)213       {214         lines.push_back(QLine(begin[0],begin[1]+g_width*i,begin[0]+g_rows*g_length,begin[1]+g_width*i));215     }216     for(int i=0;i<=g_rows;i++)217       {218         lines.push_back(QLine(begin[0]+g_length*i,begin[1],begin[0]+g_length*i,begin[1]+g_width*g_lines));219     }220 221     p.drawLines;222     QBrush brush;223     brush.setColor(Qt::black);224     brush.setStyle(Qt::SolidPattern);225     p.setBrush;226     p.drawRects;227     brush.setColor(Qt::yellow);228     p.setBrush;229     p.drawRects(rectsfight);230     brush.setColor;231     p.setBrush;232     p.drawRect;233 234     emit UpdateImage;235 236 }

widget.cpp

  1 #include "widget.h"  2 #include "ui_widget.h"  3 #include<QPainter>  4 #include<QPen>  5 #include<QBrush>  6 #include<allparameter.h>  7 #include<QMessageBox>  8   9 QVector<QRect>  rects; 10 QVector<QRect>  rectsfight; 11 QRect newrect; 12  13 int state=0,statefight=0; 14 int begin[2]={20,20}; 15 int g_length=20,g_width=20,g_lines=21,g_rows=21; 16  17  18 Widget::Widget(QWidget *parent) : 19     QWidget, 20     ui(new Ui::Widget) 21 { 22     ui->setupUi(this); 23  24     resize(begin[0]*2+g_length*g_rows,begin[1]+(g_width+3)*g_lines); 25     ui->pushButton->move((begin[0]+g_length*(g_rows/2-1))-ui->pushButton->width()-ui->label->width(),begin[1]+(g_width+1)*g_lines); 26     ui->label->move((begin[0]+g_length*)-ui->label->width()-2,begin[1]+(g_width+1)*g_lines); 27     ui->lineEdit->move((begin[0]+g_length*),begin[1]+(g_width+1)*g_lines+2); 28     ui->pushButton_2->move((begin[0]+g_length*(g_rows/2+1))+ui->lineEdit->width(),begin[1]+(g_width+1)*g_lines); 29  30  31  32     ui->lineEdit->setText("300"); 33     ui->lineEdit->resize(40,20); 34  35     timeset=ui->lineEdit->text; 36  37     srand(time; 38  39     setWindowFlags(Qt::FramelessWindowHint|windowFlags; 40  41     rects.push_back(QRect(begin[0]+g_length*,begin[1]+g_width*(g_lines/4),g_length,g_width)); 42     rectsfight.push_back(QRect(begin[0]+g_length*(g_rows*3/4),begin[1]+g_width*(g_lines*3/4),g_length,g_width)); 43  44  45     bool con=1; 46      int a,b; 47     while 48     { 49         a=rand()%; 50        b=rand()%; 51        if(!(rects.contains(QRect(begin[0]+g_length*a,begin[1]+g_width*b,g_length,g_width)))) 52            con=0; 53     } 54      newrect=QRect(begin[0]+g_length*a,begin[1]+g_width*b,g_length,g_width); 55  56  57  58  59  60     time_1=new QTimer(this); 61  62  63     MyT=new MyThread; 64     thread=new QThread(this); 65     MyT->moveToThread; 66  67     thread->start(); 68  69     connect(time_1,&QTimer::timeout,MyT,&MyThread::DrawImage); 70  71     connect(MyT,&MyThread::UpdateImage,this,&Widget::dealUpdateImage); 72  73     connect(this,&Widget::destroyed,this,&Widget::closeThread); 74  75     connect(MyT,&MyThread::Touch,this, 76                         [=]() 77                         { 78                                 time_1->stop(); 79                                 QMessageBox message; 80                                 message.information(this,"提示","你输了"); 81                         } 82                     ); 83  84     Paint(); 85  86  87  88 } 89  90 Widget::~Widget() 91 { 92     delete ui; 93 } 94 void Widget::closeThread() 95 { 96     thread->quit(); 97     thread->wait(); 98 } 99 100 101 void Widget::on_pushButton_clicked()102 {103     timeset=ui->lineEdit->text;104     if(time_1->isActive()==false)105     {106 107         time_1->start;108 109     }110     else111     {112         time_1->stop();113     }114 }115 116 void Widget::paintEvent(QPaintEvent *)117 {118     QPainter p(this);119     p.drawImage(0,0,image);120 121 }122 123 void Widget::dealUpdateImage(QImage temp)124 {125     image=temp;126     update();127 }128 129 130 131 void Widget::keyPressEvent(QKeyEvent *event)132 {133     switch(event->key134     {135     case 87: state=0;break;136     case 65: state=1;break;137     case 83: state=2;break;138     case 68: state=3;break;139     case 73: statefight=0;break;140     case 74: statefight=1;break;141     case 75: statefight=2;break;142     case 76: statefight=3;break;143     }144 }145 146 void Widget::on_pushButton_2_clicked()147 {148     closeThread();149     close();150 }151 152 void Widget::Paint()153 {154     image=QImage(500,500,QImage::Format_ARGB32);155     QPainter p(&image);156 157 158 159     QPen pen;160 161 162     p.setPen;163     QVector<QLine>  lines;164 165     for(int i=0;i<=g_lines;i++)166       {167         lines.push_back(QLine(begin[0],begin[1]+g_width*i,begin[0]+g_rows*g_length,begin[1]+g_width*i));168     }169     for(int i=0;i<=g_rows;i++)170       {171         lines.push_back(QLine(begin[0]+g_length*i,begin[1],begin[0]+g_length*i,begin[1]+g_width*g_lines));172     }173 174     p.drawLines;175     QBrush brush;176     brush.setColor(Qt::black);177     brush.setStyle(Qt::SolidPattern);178     p.setBrush;179     p.drawRects;180     brush.setColor(Qt::yellow);181     p.setBrush;182     p.drawRects(rectsfight);183     brush.setColor;184     p.setBrush;185     p.drawRect;186 187     update();188 }

界面控件

凤凰彩票app下载安装 3

最终效果

凤凰彩票app下载安装 4

本文由凤凰平台注册开户发布于编程知识,转载请注明出处:QT贯彻两条贪吃蛇

关键词: