实例介绍
【实例截图】
【核心代码】
#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;
}
标签: 贪吃蛇
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明


网友评论
我要评论