基本信息
源码名称:QT编写的贪吃蛇
源码大小:7.92KB
文件格式:.rar
开发语言:C/C++
更新时间:2019-01-22
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 3 元×
微信扫码支付:3 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
因为开始设计师数据类型没精确,所以吃的时候有点偏差
因为开始设计师数据类型没精确,所以吃的时候有点偏差
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMetaType>
#include <QMessageBox>
MainWindow::MainWindow(QWidget *parent) :
QWidget(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
qRegisterMetaType<QList<QPoint*>>("QList<QPoint*>");
InfoInit();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::InfoInit()
{
int startX=this->width()/2;
int startY=this->height()/2;
QPoint *p1=new QPoint();
p1->setX(startX);
p1->setY(startY);
QPoint *p2=new QPoint();
p2->setX(startX-rectW);
p2->setY(startY);
QPoint *p3=new QPoint();
p3->setX(startX rectW);
p3->setY(startY);
lengList.append(p3);
lengList.append(p1);
lengList.append(p2);
GetCurrentSize();
Direction=="Right";
operation=new OperationClass();
CreatT=new CreatTargetPoint();
OperationThread=new QThread();
thread=new QThread();
connect(this,SIGNAL(SendMsg(QString,QList<QPoint*>)),operation,SLOT(GetListPoint(QString,QList<QPoint*>)));
connect(operation,SIGNAL(SenReturnMsg(QList<QRect>)),this,SLOT(GetReturnMsg(QList<QRect>)));
connect(this,SIGNAL(sendIsStart(bool)),operation,SLOT(GetIsStart(bool)));
connect(this,SIGNAL(sendToCreatT(int,int)),CreatT,SLOT(CreatRandomSlot(int,int)));
connect(this,SIGNAL(clearSignal()),CreatT,SLOT(ClearList()));
connect(CreatT,SIGNAL(sendTargetRect(QList<QRect>)),this,SLOT(GetTargetRect(QList<QRect>)));
operation->moveToThread(OperationThread);
OperationThread->start();
CreatT->moveToThread(thread);
thread->start();
emit SendMsg("",lengList);
emit sendToCreatT(currentW,currentH);
}
void MainWindow::GetCurrentSize()
{
currentW=this->width();
currentH=this->height();
}
void MainWindow::PaintSnaker()
{
CreatRandom();
QPainter p;
p.begin(this);
p.setPen(QPen(Qt::red,1,Qt::SolidLine));
p.setBrush(Qt::red);
for(int j=0;j<targetRectTest.count();j )
{
p.drawRect(targetRectTest[j]);
}
for(int i=0;i<DrawRectList.count();i )
{
p.drawRect(DrawRectList[i]);
}
int x=targetRectTest[0].x() (targetRectTest[0].width()/2);
int y=targetRectTest[0].y() (targetRectTest[0].height()/2);
qDebug()<<"KKLLLLJJJJ::"<<DrawRectList[0].x()<<"||"<<DrawRectList[0].y()<<"||"<<targetRectTest[0].x()<<targetRectTest[0].y()<<targetRectTest[0].width()<<targetRectTest[0].height();
if((x>DrawRectList[0].x())&&(x<=DrawRectList[0].x() DrawRectList[0].width())&&(y>=DrawRectList[0].y())&&(y<=DrawRectList[0].y() DrawRectList[0].height()))
{
AddItem();
qDebug()<<"LLLLLLLLL:::"<<lengList.count();
}
if(DrawRectList[0].y() DrawRectList[0].height()<0)
{
OperationClass::iscanUpRun=false;
StopGame();
}
else if((DrawRectList[0].y())>=this->height())
{
OperationClass::iscanDownRun=false;
StopGame();
}else if((DrawRectList[0].x() DrawRectList[0].width())>=this->width())
{
OperationClass::iscanRightRun=false;
OperationClass::isinitRightRun=false;
StopGame();
}
else if(DrawRectList[0].x()<=0)
{
OperationClass::iscanLeftRun=false;
StopGame();
}
}
void MainWindow::PaintInitSnaker()
{
for(int i=0;i<lengList.count();i )
{
QRect rect;
rect.setX(lengList[i]->x());
rect.setY(lengList[i]->y());
rect.setWidth(rectW);
rect.setHeight(rectH);
QPainter p;
p.begin(this);
p.setPen(QPen(Qt::red,1,Qt::SolidLine));
p.setBrush(Qt::red);
p.drawRect(rect);
}
}
void MainWindow::StopGame()
{
OperationClass::iscanLeftRun=false;
OperationClass::iscanRightRun=false;
OperationClass::iscanUpRun=false;
OperationClass::isinitRightRun=false;
OperationClass::iscanDownRun=false;
QWidget MBwidget;
MBwidget.setWindowFlags(Qt::WindowStaysOnTopHint);
QMessageBox::information(&MBwidget, tr("Waring"), tr("GameOver!"));
OperationThread->exit(0);
delete operation;
delete OperationThread;
thread->exit(0);
delete CreatT;
delete thread;
this->close();
}
void MainWindow::AddItem()
{
CreatTargetPoint::isflag=false;
QPoint *p=new QPoint();
p->setX(0);
p->setY(0);
lengList.append(p);
targetRectTest.clear();
emit clearSignal();
emit SendMsg(Direction,lengList);
return;
if(Direction=="Left")
{
}
if(Direction=="Right"||Direction=="")
{
// rect.setX(lengList.last()->x()-currentW);
// rect.setY(lengList.last()->y());
// rect.setWidth(currentW);
// rect.setHeight(currentH);
// lengList.append(rect);
}
if(Direction=="Up")
{
}
if(Direction=="Bottom")
{
}
}
void MainWindow::CreatRandom()
{
qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
int PointW;
int PointH;
for(int i=0;i<currentW;i )
{
PointW=qrand()%currentW;
}
for(int j=0;j<currentH;j )
{
PointH=qrand()%currentH;
}
QPoint point;
point.setX(PointW);
point.setY(PointH);
QRect rect;
rect.setX(PointW);
rect.setY(PointH);
rect.setWidth(rectW);
rect.setHeight(rectH);
if(targetRectTest.count()==0)
{
targetRectTest.append(rect);
}
}
void MainWindow::paintEvent(QPaintEvent *event)
{
PaintSnaker();
}
void MainWindow::keyPressEvent(QKeyEvent *event)
{
if(event->key()==Qt::Key_Left)
{
if(Direction=="Right"||Direction=="")
{
return;
}
Direction="Left";
OperationClass::iscanLeftRun=true;
OperationClass::iscanRightRun=false;
OperationClass::iscanUpRun=false;
OperationClass::isinitRightRun=false;
OperationClass::iscanDownRun=false;
emit SendMsg(Direction,lengList);
qDebug()<<"current Operation is Key_Left";
}
else if(event->key()==Qt::Key_Right)
{
if(Direction=="Left")
{
return;
}
Direction="Right";
OperationClass::iscanLeftRun=false;
OperationClass::iscanRightRun=true;
OperationClass::iscanUpRun=false;
OperationClass::isinitRightRun=false;
OperationClass::iscanDownRun=false;
emit SendMsg(Direction,lengList);
qDebug()<<"current Operation is Key_Right";
}
else if(event->key()==Qt::Key_Up)
{
if(Direction=="Down")
{
return;
}
Direction="Up";
OperationClass::iscanUpRun=true;
OperationClass::iscanRightRun=false;
OperationClass::iscanLeftRun=false;
OperationClass::iscanDownRun=false;
OperationClass::isinitRightRun=false;
emit SendMsg(Direction,lengList);
qDebug()<<"current Operation is Key_Up";
}
else if(event->key()==Qt::Key_Down)
{
if(Direction=="Up")
{
return;
}
Direction="Down";
OperationClass::iscanUpRun=false;
OperationClass::iscanRightRun=false;
OperationClass::iscanLeftRun=false;
OperationClass::isinitRightRun=false;
OperationClass::iscanDownRun=true;
emit SendMsg(Direction,lengList);
qDebug()<<"current Operation is Key_Down";
}
}
void MainWindow::GetReturnMsg(QList<QRect> rectList)
{
//qDebug()<<"Current get Rect is :::"<<rect;
DrawRectList.clear();
DrawRectList=rectList;
this->update();
}
void MainWindow::GetTargetRect(QList<QRect> rectList)
{
targetRect.clear();
targetRect=rectList;
}