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 }

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 }

界面控件

最终效果

QT实现两条贪吃蛇的更多相关文章

  1. JavaScript版—贪吃蛇小组件

    最近在学习JavaScript,利用2周的时间看完了<JavaScript高级编程>,了解了Js是一门面向原型编程的语言,没有像C#语言中的class,也没有私有.公有.保护等访问限制的级 ...

  2. 用Python写一个贪吃蛇

    最近在学Python,想做点什么来练练手,命令行的贪吃蛇一般是C的练手项目,但是一时之间找不到别的,就先做个贪吃蛇来练练简单的语法. 由于Python监听键盘很麻烦,没有C语言的kbhit(),所以这 ...

  3. 【python】10分钟教你用python打造贪吃蛇超详细教程

    10分钟教你用python打造贪吃蛇超详细教程 在家闲着没妹子约, 刚好最近又学了一下python,听说pygame挺好玩的.今天就在家研究一下, 弄了个贪吃蛇出来.希望大家喜欢. 先看程序效果: 0 ...

  4. 基于spec探路者团队贪吃蛇作品的评论

    1 运动功能 由以上两图贪吃蛇的位置不同可知,运动功能实现.并且我能够通过使用键盘上的上下左右方位键控制蛇的移动方向,蛇在控制的方向上进行直线前进. 2 吃食物功能 以上两图可知吃食物功能实现.当界面 ...

  5. Qt 学习之路 2(33):贪吃蛇游戏(3)

    Qt 学习之路 2(33):贪吃蛇游戏(3) 豆子 2012年12月29日 Qt 学习之路 2 16条评论 继续前面一章的内容.上次我们讲完了有关蛇的静态部分,也就是绘制部分.现在,我们开始添加游戏控 ...

  6. Qt 学习之路 2(32):贪吃蛇游戏(2)

    Qt 学习之路 2(32):贪吃蛇游戏(2) 豆子 2012年12月27日 Qt 学习之路 2 55条评论 下面我们继续上一章的内容.在上一章中,我们已经完成了地图的设计,当然是相当简单的.在我们的游 ...

  7. Qt 学习之路 2(34):贪吃蛇游戏(4)

    Qt 学习之路 2(34):贪吃蛇游戏(4) 豆子 2012年12月30日 Qt 学习之路 2 73条评论 这将是我们这个稍大一些的示例程序的最后一部分.在本章中,我们将完成GameControlle ...

  8. Qt 学习之路 2(31):贪吃蛇游戏(1)

    Qt 学习之路 2(31):贪吃蛇游戏(1) 豆子 2012年12月18日 Qt 学习之路 2 41条评论 经过前面一段时间的学习,我们已经了解到有关 Qt 相当多的知识.现在,我们将把前面所讲过的知 ...

  9. QT实现贪吃蛇

    board.h #ifndef BOARD_H #define BOARD_H #define MAX_X 40 #define MAX_Y 30 #define NORMAL_LABEL 0//普通 ...

随机推荐

  1. Apache RocketMQ 的过去、现在和未来 原创: DataPipeline DataPipeline数见科技 前天

    Apache RocketMQ 的过去.现在和未来 原创: DataPipeline DataPipeline数见科技 前天

  2. OpenSL ES: 利用OpenSL ES播放一个存在于SDcard上的PCM文件

    native-lib.cpp #include <jni.h> #include <string> #include <SLES/OpenSLES.h> #incl ...

  3. sql中union,union all没有兼顾到的内容

    今日遇到一个问题,两张表联合取交集去重,但是需要把某一字段相同的也给去掉 union all : 联合,没有取交集 union :联合取交集(仅针对所有字段相同的去重) 解决方案:将联合的数据作为一个 ...

  4. 创建Bitmap之Bitmap静态方法使用示例

    package com.loaderman.customviewdemo; import android.app.Activity; import android.content.Intent; im ...

  5. Tomcat重启Shell脚本

    --测试通过 vim restart_tomcat.sh#!/bin/bash# Filename: restart_tomcat.sh# Description: Restart tomcat# D ...

  6. Linux 7 关闭、禁用防火墙服务

    1 查看防火墙状态 [root@lvxinghao ~]# systemctl status firewalld 2 查看开机是否启动防火墙服务 [root@lvxinghao ~]# systemc ...

  7. Python与C/C++相互调用(python2 调c++那个试了ok)

    一.问题 Python模块和C/C++的动态库间相互调用在实际的应用中会有所涉及,在此作一总结. 二.Python调用C/C++ 1.Python调用C动态链接库 Python调用C库比较简单,不经过 ...

  8. (十六)Centos之安装mysql

    第一步:获取mysql YUM源 进入mysql官网获取RPM包下载地址 https://dev.mysql.com/downloads/repo/yum/ 点击 下载 右击 复制链接地址 https ...

  9. (十一)会话跟踪技术之作用域(request、session、servletContext)

    一.作用域范围 Request       保存的键值仅在下一个request对象中可以得到,作用于两个有请求关系的页面 Session        它是一个会话范围,相当于一个局部变量,从Sess ...

  10. 十六章 综合实例——《跟我学Shiro》

    目录贴:跟我学Shiro目录贴 简单的实体关系图 简单数据字典 用户(sys_user) 名称 类型 长度 描述 id bigint 编号 主键 username varchar 100 用户名 pa ...