main.cpp
#include "mainwindow.h"
#include <QApplication>
#include <login.h>
#include <QMessageBox>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow *w = new MainWindow;
w->show();
return a.exec();
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <login.h>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
void paintEvent(QPaintEvent*event);
void keyPressEvent(QKeyEvent*event);
void keyReleaseEvent(QKeyEvent *event);
void timerEvent(QTimerEvent *event);
void move_judg(QString snake);
int get_random_number(int min,int max);
void set_food();
void gameove_snake();
void init_snake();
void restar_snake();
public:
int snake_max = 1000;
int speed = 10;
int timerate = 150;
int maxview_x = 450;
int maxview_y = 600;
QString snakedirection = "down";
QString login_temp = "test";
login *lo = new login;
private slots:
void receiverdata(QString);
void on_pushButton_clicked();
void on_pushButton_2_clicked();
void on_pushButton_3_clicked();
void on_pushButton_4_clicked();
void on_pushButton_5_clicked();
private:
Ui::MainWindow *ui;
int snake_x[1000] =
{
50,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0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0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
};
int time_ID = 0;
int fast_time_ID = 0;
int random_x = 0;
int random_y = 0;
double lcd_num = 10;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QPainter>
#include <QKeyEvent>
#include <QTimer>
#include <QTime>
#include <QMessageBox>
#include <QtCore/QTextStream>
#include <QtCore/QFile>
#include <login.h>
//#include <QtGlobal>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
set_food();//投食;
lo -> show();
lo->setWindowFlags(lo->windowFlags() | Qt::WindowStaysOnTopHint);
lo->showNormal();
connect(lo,SIGNAL(send_login(QString)),this,SLOT(receiverdata(QString)));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::paintEvent(QPaintEvent *)//画最大界面以及蛇头
{
QPainter painter(this);
painter.setPen(QPen(Qt::darkBlue, 1));
painter.drawEllipse(snake_x[0], snake_y[0], 10, 10);
painter.setPen(QPen(Qt::darkGreen, 1));
for(int i=1; i < snake_max; i ++)
painter.drawEllipse(snake_x[i], snake_y[i], 10, 10);
painter.setPen(QPen(Qt::black, 1));
painter.drawRect(10, 20, maxview_x, maxview_y);
painter.setPen(QPen(Qt::red, 1));
painter.drawRect(random_x, random_y, 5, 5);
}
void MainWindow::keyPressEvent(QKeyEvent *event)//短按
{
switch (event->key())
{
case Qt::Key_Left:
if(snakedirection == "right" && snake_x[1] != 0)
{
gameove_snake();break;
}
snakedirection = "left"; break;
case Qt::Key_Right:
if(snakedirection == "left" && snake_x[1] != 0)
{
gameove_snake();break;
}
snakedirection = "right"; break;
case Qt::Key_Up:
if(snakedirection == "down" && snake_x[1] != 0)
{
gameove_snake();break;
}
snakedirection = "up"; break;
case Qt::Key_Down:
if(snakedirection == "up" && snake_x[1] != 0)
{
gameove_snake();break;
}
snakedirection = "down"; break;
default:
break;
}
repaint();
QWidget::keyPressEvent(event);
}
void MainWindow::keyReleaseEvent(QKeyEvent *event)//检测松开
{
switch (event->key())
{
case Qt::Key_Left:
case Qt::Key_Right:
case Qt::Key_Up:
case Qt::Key_Down:
if (event->isAutoRepeat())
{
killTimer(time_ID);
killTimer(fast_time_ID);
fast_time_ID = this->startTimer(0.1 * timerate);//定时器启动
}
else
{
killTimer(time_ID);
killTimer(fast_time_ID);
time_ID = this->startTimer(timerate);
}
break;
default:
break;
}
QWidget::keyReleaseEvent(event);
}
void MainWindow::timerEvent(QTimerEvent *event)//定时器
{
if(event->timerId() == time_ID || event->timerId() == fast_time_ID)
move_judg(snakedirection);
}
void MainWindow::move_judg(QString snake)//移动,以及判断撞墙
{
int temp_x = 0;
int temp_y = 0;
for(int i = (snake_max - 1); i >= 0; i--)
{
if(snake_x[i] != 0)
{
temp_x = snake_x[i];
temp_y = snake_y[i];
if(i != 0)
{
snake_x[i] = snake_x[i-1];
snake_y[i] = snake_y[i-1];
}
}
}
if(snake == "left")
snake_x[0] -= speed;
if(snake == "right")
snake_x[0] += speed;
if(snake == "up")
snake_y[0] -= speed;
if(snake == "down")
snake_y[0] += speed;
if(snake_x[0] >= 10 && snake_x[0] <= maxview_x && snake_y[0] >= 20 && snake_y[0] <= (maxview_y + 10))
{
int i = 0;
if((snake_x[0] - random_x) >= -10
&& (snake_x[0] - random_x) <= 5
&& (snake_y[0] - random_y) >= -10
&& (snake_y[0] - random_y) <= 5)//吃到食物的情况下
{
for(i = (snake_max - 1); i >= 0; i--)
{
if(snake_x[i] != 0)
{
snake_x[i+1] = temp_x;
snake_y[i+1] = temp_y;
break;
}
}
lcd_num += 10;
ui->lcdNumber->display(lcd_num);
set_food();
}
for(i = 1; i < snake_max; i++)
{
if((snake_x[0] - snake_x[i]) > -10
&& (snake_x[0] - snake_x[i]) < 10
&& (snake_y[0] - snake_y[i]) > -10
&& (snake_y[0] - snake_y[i]) < 10)
{
gameove_snake();
break;
}
if(snake_x[i] == 0 && snake_y[i] == 0)
break;
}
}
else
{
gameove_snake();
}
repaint();
}
int MainWindow::get_random_number(int min,int max)//随机生成min到max的随机数
{
int temp = 0;
//qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));//定义随机数种子
temp = qrand() % (max - min);
return temp + min;
}
void MainWindow::set_food()
{
random_x = get_random_number(10,maxview_x);
random_y = get_random_number(20,maxview_y + 10);
if((snake_x[0] - random_x) >= -10
&& (snake_x[0] - random_x) <= 5
&& (snake_y[0] - random_y) >= -10
&& (snake_y[0] - random_y) <= 5)
set_food();
for(int i = 0; i < snake_max; i++ )
if(random_x == snake_x[i] && random_y == snake_y[i] )
set_food();
repaint();
}
void MainWindow::gameove_snake()
{
killTimer(time_ID);
killTimer(fast_time_ID);
QMessageBox::StandardButton reply;
reply = QMessageBox::information(this, tr("GAME OVER"), "Game have been over? Restart?", QMessageBox::Ok | QMessageBox::No);
if (reply == QMessageBox::Ok)
{
init_snake();
}
if (reply == QMessageBox::No)
{
restar_snake();
}
}
void MainWindow::init_snake()//重新开始
{
for(int i = (snake_max - 1); i > 0; i--)
{
snake_x[i] = 0;
snake_y[i] = 0;
}
snake_x[0] = 50;
snake_y[0] = 30;
snakedirection = "down";
lcd_num = 10;
ui->lcdNumber->display(lcd_num);
}
void MainWindow::restar_snake()//复活
{
for(int i = (snake_max - 1); i >= 0; i--)
{
if(snake_x[i] != 0)
{
snake_x[i] = 50;
snake_y[i] = 30;
}
}
snakedirection = "down";
}
void MainWindow::on_pushButton_clicked()
{
killTimer(time_ID);
killTimer(fast_time_ID);
QFile f("D:\\"+login_temp+".txt");
if(!f.open(QIODevice::WriteOnly | QIODevice::Text))
{
QMessageBox::warning(this, tr("Faild"), "can't open!!!!", QMessageBox::Ok);
}
else
{
QTextStream txtOutput(&f);
txtOutput << snakedirection << endl;
txtOutput << lcd_num << endl;
for(int i = 0; i < snake_max; i++)
{
if(snake_x[i] != 0)
{
txtOutput << snake_x[i] << "-" << snake_y[i] << endl;
}
}
f.close();
QMessageBox::information(this, tr("successed"), " write successed!!!!", QMessageBox::Ok);
}
}
void MainWindow::on_pushButton_2_clicked()
{
killTimer(time_ID);
killTimer(fast_time_ID);
int i = 0;
QFile f("D:\\"+login_temp+".txt");
if(!f.open(QIODevice::ReadOnly | QIODevice::Text))
{
QMessageBox::warning(this, tr("Faild"), "can't open!!!!", QMessageBox::Ok);
}
else
{
QTextStream txtInput(&f);
snakedirection = txtInput.readLine();
lcd_num = txtInput.readLine().toInt();
ui->lcdNumber->display(lcd_num);
while(!txtInput.atEnd())
{
QString str = txtInput.readLine();
QStringList list = str.split("-");
snake_x[i] = list[0].toInt();
snake_y[i] = list[1].toInt();
i++;
}
for( i; i < snake_max; i++)
{
snake_x[i] = 0;
snake_y[i] = 0;
}
repaint();
f.close();
QMessageBox::information(this, tr("successed"), " read successed!!!!", QMessageBox::Ok);
}
}
void MainWindow::on_pushButton_3_clicked()
{
killTimer(time_ID);
killTimer(fast_time_ID);
}
void MainWindow::on_pushButton_4_clicked()
{
killTimer(time_ID);
killTimer(fast_time_ID);
init_snake();
repaint();
}
void MainWindow::on_pushButton_5_clicked()
{
this->close();
}
void MainWindow::receiverdata(QString data)
{
login_temp = data;
lo->setWindowFlags(lo->windowFlags() & ~Qt::WindowStaysOnTopHint);
lo->showNormal();
}
login.h
#ifndef LOGIN_H
#define LOGIN_H
#include <QDialog>
namespace Ui {
class login;
}
class login : public QDialog
{
Q_OBJECT
public:
explicit login(QWidget *parent = 0);
~login();
QString login_temp = NULL;
signals:
void send_login(QString data);
private slots:
void on_pushButton_2_clicked();
void on_pushButton_clicked();
private:
Ui::login *ui;
};
#endif // LOGIN_H
login.cpp
#include "login.h"
#include "ui_login.h"
login::login(QWidget *parent) :
QDialog(parent),
ui(new Ui::login)
{
ui->setupUi(this);
}
login::~login()
{
delete ui;
}
void login::on_pushButton_clicked()
{
login_temp = ui->textBrowser->toPlainText();
emit send_login(login_temp);
this->close();
}
void login::on_pushButton_2_clicked()
{
this->close();
}
mainwindow.ui
login.ui