热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

QT实现两条贪吃蛇

Snake.pro文件1#-------------------------------------------------2#3#ProjectcreatedbyQ

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 #include 
 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
  3 #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 #include
  4 #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 }

 

界面控件

 

最终效果

 


推荐阅读
  • 本文讨论了在openwrt-17.01版本中,mt7628设备上初始化启动时eth0的mac地址总是随机生成的问题。每次随机生成的eth0的mac地址都会写到/sys/class/net/eth0/address目录下,而openwrt-17.01原版的SDK会根据随机生成的eth0的mac地址再生成eth0.1、eth0.2等,生成后的mac地址会保存在/etc/config/network下。 ... [详细]
  • IhaveconfiguredanactionforaremotenotificationwhenitarrivestomyiOsapp.Iwanttwodiff ... [详细]
  • 本文介绍了[从头学数学]中第101节关于比例的相关问题的研究和修炼过程。主要内容包括[机器小伟]和[工程师阿伟]一起研究比例的相关问题,并给出了一个求比例的函数scale的实现。 ... [详细]
  • 阿,里,云,物,联网,net,core,客户端,czgl,aliiotclient, ... [详细]
  • 本文介绍了Oracle数据库中tnsnames.ora文件的作用和配置方法。tnsnames.ora文件在数据库启动过程中会被读取,用于解析LOCAL_LISTENER,并且与侦听无关。文章还提供了配置LOCAL_LISTENER和1522端口的示例,并展示了listener.ora文件的内容。 ... [详细]
  • Spring特性实现接口多类的动态调用详解
    本文详细介绍了如何使用Spring特性实现接口多类的动态调用。通过对Spring IoC容器的基础类BeanFactory和ApplicationContext的介绍,以及getBeansOfType方法的应用,解决了在实际工作中遇到的接口及多个实现类的问题。同时,文章还提到了SPI使用的不便之处,并介绍了借助ApplicationContext实现需求的方法。阅读本文,你将了解到Spring特性的实现原理和实际应用方式。 ... [详细]
  • 本文介绍了三种方法来实现在Win7系统中显示桌面的快捷方式,包括使用任务栏快速启动栏、运行命令和自己创建快捷方式的方法。具体操作步骤详细说明,并提供了保存图标的路径,方便以后使用。 ... [详细]
  • Java学习笔记之面向对象编程(OOP)
    本文介绍了Java学习笔记中的面向对象编程(OOP)内容,包括OOP的三大特性(封装、继承、多态)和五大原则(单一职责原则、开放封闭原则、里式替换原则、依赖倒置原则)。通过学习OOP,可以提高代码复用性、拓展性和安全性。 ... [详细]
  • 在Xamarin XAML语言中如何在页面级别构建ControlTemplate控件模板
    本文介绍了在Xamarin XAML语言中如何在页面级别构建ControlTemplate控件模板的方法和步骤,包括将ResourceDictionary添加到页面中以及在ResourceDictionary中实现模板的构建。通过本文的阅读,读者可以了解到在Xamarin XAML语言中构建控件模板的具体操作步骤和语法形式。 ... [详细]
  • r2dbc配置多数据源
    R2dbc配置多数据源问题根据官网配置r2dbc连接mysql多数据源所遇到的问题pom配置可以参考官网,不过我这样配置会报错我并没有这样配置将以下内容添加到pom.xml文件d ... [详细]
  • 本文介绍了Python爬虫技术基础篇面向对象高级编程(中)中的多重继承概念。通过继承,子类可以扩展父类的功能。文章以动物类层次的设计为例,讨论了按照不同分类方式设计类层次的复杂性和多重继承的优势。最后给出了哺乳动物和鸟类的设计示例,以及能跑、能飞、宠物类和非宠物类的增加对类数量的影响。 ... [详细]
  • 本文介绍了如何清除Eclipse中SVN用户的设置。首先需要查看使用的SVN接口,然后根据接口类型找到相应的目录并删除相关文件。最后使用SVN更新或提交来应用更改。 ... [详细]
  • 模板引擎StringTemplate的使用方法和特点
    本文介绍了模板引擎StringTemplate的使用方法和特点,包括强制Model和View的分离、Lazy-Evaluation、Recursive enable等。同时,还介绍了StringTemplate语法中的属性和普通字符的使用方法,并提供了向模板填充属性的示例代码。 ... [详细]
  • 本文介绍了响应式页面的概念和实现方式,包括针对不同终端制作特定页面和制作一个页面适应不同终端的显示。分析了两种实现方式的优缺点,提出了选择方案的建议。同时,对于响应式页面的需求和背景进行了讨论,解释了为什么需要响应式页面。 ... [详细]
  • 欢乐的票圈重构之旅——RecyclerView的头尾布局增加
    项目重构的Git地址:https:github.comrazerdpFriendCircletreemain-dev项目同步更新的文集:http:www.jianshu.comno ... [详细]
author-avatar
我是雅小贱-
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有