mainwindow.h文件
-------------------------------------------------------------
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <Qtgui>
#include <QMainWindow>
#define MOVE_VALUE 16//蛇移动的步间距
#define LENTH 15//蛇身体的长度(胜利)
enum direction//定义蛇移动的四个方向
{
m_up,m_down,m_left,m_right
};
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
void keyPressEvent(QKeyEvent*);//键盘按键按下的处理函数
private:
Ui::MainWindow *ui;
QTimer* timer;//定时器(定时蛇移动的时间)
enum direction d;//蛇当前移动的方向
QLabel* next;//下一个目标
QLabel* label[LENTH];//蛇的身体(Qlabel的数组)
private slots:
void updata();//蛇移动的处理函数
};
#endif // MAINWINDOW_H
----------------------------------------------------
mainwindow.cpp文件
---------------------------------------------
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
label[0]= new QLabel(this);//初始化蛇头
label[0]->setAutoFillBackground(true);//设置背景颜色:允许设置背景颜色
label[0]->resize(QSize(16,16));//重新定义蛇头的大小
label[0]->setFrameShadow(QFrame::Raised);//设置边框
label[0]->setFrameShape(QFrame::Panel);//设置形状
for(int i = 1;i<LENTH;i++)//初始化蛇的身体
{
label = NULL;
}
d= m_right;//开始移动方向
next = new QLabel(this);//初始化目标
next->setAutoFillBackground(true);//填充目标的背景
next->setPalette(QPalette(QColor(qrand()%200,qrand()%150,qrand()%100)));
next->resize(QSize(16,16));//重新定义目标的大小
next->setFrameShadow(QFrame::Raised);//重定义边框和形状
next->setFrameShape(QFrame::Panel);
next->move(80,80);//将目标摆放到一个16的倍数的位置上
label[0]->move(QPoint(112,80));//将蛇头放到一个位置
this->setWindowTitle("Snake");//更改窗口的标题
timer = new QTimer(this);//初始化定时器
connect(timer,SIGNAL(timeout()),this,SLOT(updata()));//将定时器的溢出信号和自定义的槽函数连接起来
timer->start(250);//定义开启定时器 250毫秒
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::keyPressEvent(QKeyEvent*k)
{
if(k->key()==Qt::Key_Left)//判断用户按下的按键
{
d = m_left;//修改蛇的前进方向
}
else if(k->key()==Qt::Key_Right)
{
d = m_right;
}
else if(k->key()==Qt::Key_Up)
{
d= m_up;
}
else if(k->key()==Qt::Key_Down)
{
d=m_down;
}
}
void MainWindow::updata()
{
int x = 0,y = 0;//定义保存位置的变量
int x_new = 1, y_new = 0;//下一步蛇头的位置
x = label[0]->x();//获取蛇头的位置
y = label[0]->y();
if(x<=0||y<=16||x>=this->width()-16||y>=this->height())//检测当前位置是否已经碰见边框
{
QMessageBox box;//弹出消息对话框
box.setWindowTitle("OVER");
box.setIcon(QMessageBox::Warning);
box.setText("Game over!");
box.setStandardButtons(QMessageBox::Ok);
box.exec();
qApp->quit();
}
x_new = x;
y_new = y;
switch(d)//根据蛇头前进的方向,计算下一步的坐标
{
case 0://向上
y_new = y-MOVE_VALUE;
break;
case 1://向下
y_new = y+MOVE_VALUE;
break;
case 2://向左
x_new = x-MOVE_VALUE;
break;
case 3://向右
x_new = x+MOVE_VALUE;
break;
}
if(x_new==next->x()&&y_new==next->y())//碰撞检测
{
//qDebug("Ok...");
int i = LENTH;//将目标放到蛇的身体中
while(label[--i]==NULL);//找到合适的位置
if(i==LENTH-1) //如果蛇的长度达到Lenth通知用户赢得胜利
{
QMessageBox box;
box.setWindowTitle("YEAH");
box.setText("Success!");
box.setStandardButtons(QMessageBox::Ok);
box.exec();
qApp->quit();
}
while(i>=0&&i<LENTH-1)//将蛇的身体依次向后挪动一个位置
{
label[i+1] = label;
i--;
}
label[0]=next;//将目标设置为蛇头
next = new QLabel(this);
next->setAutoFillBackground(true);
next->setPalette(QPalette(QColor(qrand()%200,qrand()%150,qrand()%100)));
next->resize(16,16);
next->setFrameShadow(QFrame::Raised);
next->setFrameShape(QFrame::Panel );
int x_temp = qrand()%(this->width());//生成目标的随机位置
int y_temp = qrand()%(this->height());
x_temp = x_temp-x_temp%16;
if(x_temp<=16)x_temp+=32;//防止位置边缘化
else if(x_temp>=this->width()-16)x_temp-=32;
y_temp = y_temp-y_temp%16;
if(y_temp<=16)y_temp+=32;
else if(y_temp>=this->width()-16)y_temp-=32;
next->move(x_temp,y_temp);
next->show();
}
int i =LENTH;//移动蛇的身体
while(label[--i]==NULL);//找到蛇的最后一个位置
while(i>0)// 将对应的元素挪到上一个元素的位置
{
label->move(label[i-1]->x(),label[i-1]->y());//可能bug
i--;
}
label[0]->move(x_new,y_new);//移动蛇头到下一步的位置
}
------------------------------------------
main.cpp文件
--------------------------
#include <QtGui/QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}