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 rects; 6 extern QVector 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
5 #include
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 #include #include #include"mythread.h" #include 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 #include3 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 #include3 #include 4 #include 5 #include 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_|((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 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 #include4 #include 5 #include 6 #include 7 #include 8 9 QVector rects; 10 QVector rectsfight; 11 QRect newrect; 12 13 int state=0,statefight=0; 14 int begin[2]={20,20}; 15 int g_length=20,g_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 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 }
界面控件
最终效果