主要功能已完成

This commit is contained in:
2021-02-09 01:10:53 +08:00
parent 633cbc0428
commit 36cc78b047
13 changed files with 160 additions and 387 deletions
+2 -4
View File
@@ -17,12 +17,10 @@ DEFINES += QT_DEPRECATED_WARNINGS
SOURCES += \
main.cpp \
widget.cpp \
workthread.cpp
widget.cpp
HEADERS += \
widget.h \
workthread.h
widget.h
FORMS += \
widget.ui
+73 -34
View File
@@ -5,35 +5,62 @@
#include <QBrush>
#include <QTimer>
#include <QDebug>
#include "WorkThread.h"
extern care car[5][5];
extern struct touch t;
care car[5][5];
int car_num=0;
void car_init()
{
int car_rand_buff[26];
int car_rand_int=0;
int car_rand=0;
int while_buff=0;
car_num=0;
for(int y=0;y<5;y++)
{
for(int x=0;x<5;x++)
{
do
{
while_buff=0;
car_rand=(rand()%25)+1;
for(int z=0;z<car_rand_int;z++)
{
if(car_rand_buff[z]==car_rand)
{
while_buff=1;
}
}
}while(while_buff);
car[y][x].num=car_rand;
car[y][x].color=0xffffff;
car_rand_buff[car_rand_int]=car_rand;
car_rand_int++;
}
}
}
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
QTimer *mainrun;
//QTimer *mainrun;
srand(time(NULL));
car_init();
for(int y=0;y<10;y++)
{
for(int x=0;x<10;x++)
{
car[y][x].color=0xffffff;
car[y][x].num=0;
}
}
/*
mainrun = new QTimer(this);
connect(mainrun,&QTimer::timeout,this,&Widget::mainrun_timeout);
mainrun->start(1);
WorkThread *workthread = new WorkThread;
workthread->start();
*/
}
Widget::~Widget()
@@ -44,8 +71,6 @@ Widget::~Widget()
void Widget::paintEvent(QPaintEvent *)
{
//QPainter p(this); //方法1 创建画家对象 并指定绘图设备
/*
*方法2 创建画家对象
*通过bggin指定绘图设备
@@ -60,23 +85,11 @@ void Widget::paintEvent(QPaintEvent *)
//定义画笔
QPen pen;
QBrush brush;//创建笔刷
pen.setWidth(1);//设置线宽
pen.setColor(QColor(28,28,28)); //常用颜色
pen.setStyle(Qt::SolidLine); //设置风格
p.setPen(pen); //将画笔给画家
brush.setStyle(Qt::SolidPattern);//设定笔刷样式
for(int y=0;y<5;y++)
{
for(int x=0;x<5;x++)
@@ -100,17 +113,43 @@ void Widget::paintEvent(QPaintEvent *)
void Widget::mousePressEvent(QMouseEvent *event)
{
int y,x;
if (event->button() == Qt::LeftButton)
{
t.flag=1;
t.x=event->x();
t.y=event->y();
if(event->x()>car_size*5||event->y()>car_size*5)
{
}else
{
y=(event->y()/car_size);
x=(event->x()/car_size);
if(car[y][x].num==car_num+1)
{
car[y][x].color=0x00db00;
car_num++;
}else
{
if(car[y][x].color==0xffffff)
{
car[y][x].color=0xea0000;
}
}
}
}
Widget::update();
}
void Widget::mainrun_timeout()
{
//Widget::update();
}
void Widget::on_pushButton_clicked()
{
car_init();
Widget::update();
}
+14 -1
View File
@@ -6,7 +6,7 @@
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
#define car_size 110
class Widget : public QWidget
{
Q_OBJECT
@@ -21,7 +21,20 @@ protected:
//绘图事件内部会自动调用,窗口需要重绘的时候(状态改变)
void paintEvent(QPaintEvent *);
void mousePressEvent(QMouseEvent *event);//------------------鼠标按下事件
private slots:
void on_pushButton_clicked();
private:
Ui::Widget *ui;
};
//****************************************************c
struct care
{
int num;
long color;
};
#endif // WIDGET_H
+13
View File
@@ -13,6 +13,19 @@
<property name="windowTitle">
<string>Widget</string>
</property>
<widget class="QPushButton" name="pushButton">
<property name="geometry">
<rect>
<x>584</x>
<y>40</y>
<width>181</width>
<height>121</height>
</rect>
</property>
<property name="text">
<string>Reset</string>
</property>
</widget>
</widget>
<resources/>
<connections/>
-72
View File
@@ -1,72 +0,0 @@
#include "workthread.h"
#include <time.h>
#include <QDebug>
care car[5][5];
struct touch t;
void car_init()
{
int car_rand_buff[26];
int car_rand_int=0;
int car_rand=0;
int while_buff=0;
for(int y=0;y<5;y++)
{
for(int x=0;x<5;x++)
{
do
{
while_buff=0;
car_rand=(rand()%25)+1;
for(int z=0;z<car_rand_int;z++)
{
if(car_rand_buff[z]==car_rand)
{
while_buff=1;
}
}
}while(while_buff);
car[y][x].num=car_rand;
car_rand_buff[car_rand_int]=car_rand;
car_rand_int++;
}
}
}
int chack_car(int y,int x)
{
int a,b,c=0;
a=(x/car_size);
b=((y/car_size)*5);
c=a+b+1;
return c;
}
void w()
{
int t_car_flag=0;
srand(time(NULL));
car_init();
while(1)
{
if(t.flag)
{
t.flag=0;
t_car_flag=chack_car(t.y,t.x);
qDebug()<<t.y<<" "<<t.x<<" "<<t_car_flag;
}else{t_car_flag=0;}
}
}
void WorkThread::run()
{
w();
}
-34
View File
@@ -1,34 +0,0 @@
#ifndef WORKTHREAD_H
#define WORKTHREAD_H
#include <QWidget>
#include <QThread>
#define car_size 110
class WorkThread : public QThread
{
protected:
void run();
};
//****************************************************c
struct care
{
int num;
long color;
};
struct touch
{
char flag;
int y;
int x;
};
#endif // WORKTHREAD_H