代码拉取完成,页面将自动刷新
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QTest>
#include<QAction>
#include<QMenuBar>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
resize(800,500);
dir=1;
connect(ui->menuBar,SIGNAL(triggered(QAction*)),this,SLOT(Triggering(QAction*)));
connect(ui->startbtn,&QPushButton::clicked,
this,&MainWindow::TG);
connect(ui->pausebtn,&QPushButton::clicked,
this,&MainWindow::Stopgame);
connect(ui->jixubtn,&QPushButton::clicked,
this,&MainWindow::Continuegame);
// connect(ui->closebtn,&QPushButton::clicked,[=](){
// this->close();
// });
connect(ui->closebtn,&QPushButton::clicked,
this,&MainWindow::Closegame);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::Stopgame(){
flage=0;
}
void MainWindow::Continuegame(){
flage=1;
int ms=300;
while(flage){
snake->move(dir,this);
if(snake->shead->getPos() == food->getPos()){
delete food;
food=new SnakeNode(this,(qrand()%50)*10,(qrand()%50)*10);
snake->eat(dir,this);
}
QPoint pos;
pos=snake->shead->getPos();
if(pos.x()<0 || pos.x()>500 || pos.y()<0 || pos.y()>500){
QMessageBox::critical(this,"游戏结束","跑出边界了!");
// snake->DeleteTaill();
break;
//exit(1);
}
if(!snake->Check()){
QMessageBox::critical(this,"游戏结束","吃到自己了!");
//exit(2);
}
QTest::qWait(ms);
}
}
void MainWindow::Closegame(){
this->close();
}
void MainWindow::TG(){
gamework();
}
void MainWindow::gamework(){
food=new SnakeNode(this,(qrand()%50)*10,(qrand()%50)*10);
snake = new SnakeDialog(this);
int ms=300;
while(flage){
snake->move(dir,this);
if(snake->shead->getPos() == food->getPos()){
delete food;
food=new SnakeNode(this,(qrand()%50)*10,(qrand()%50)*10);
snake->eat(dir,this);
}
QPoint pos;
pos=snake->shead->getPos();
if(pos.x()<0 || pos.x()>500 || pos.y()<0 || pos.y()>500){
QMessageBox::critical(this,"游戏结束","跑出边界了!");
// snake->DeleteTaill();
break;
//exit(1);
}
if(!snake->Check()){
QMessageBox::critical(this,"游戏结束","吃到自己了!");
//exit(2);
}
QTest::qWait(ms);
}
}
void MainWindow::Triggering(QAction *ac){
if(ui->startaction_2 == ac){
gamework();
}
}
void MainWindow::paintEvent(QPaintEvent *painevt){
QPainter paint(this);
paint.setPen(Qt::blue);
paint.drawLine(10,0,10,500);
for(int i=0;i<=500;i+=10){
paint.drawLine(0,i+10,510,i+10);
paint.drawLine(i+10,0,i+10,510);
}
}
void MainWindow::keyPressEvent(QKeyEvent* key){
predir=dir;
switch(key->key()){
case Qt::Key_W:
dir=1;
if(2==predir){
dir=predir;
return ;
}
break;
case Qt::Key_S:
dir=2;
if(1==predir){
dir=predir;
return ;
}
break;
case Qt::Key_A:
dir=3;
if(4==predir){
dir=predir;
return ;
}
break;
case Qt::Key_D:
dir=4;
if(3==predir){
dir=predir;
return ;
}
break;
}
snake->move(dir,this);
if(snake->shead->getPos() == food->getPos()){
delete food;
food=new SnakeNode(this,(qrand()%50)*10,(qrand()%50)*10);
snake->eat(dir,this);
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。