前言
好久没更新了,新工作转做QT开发,周末写个小游戏练练手,大佬略过,开发环境Qt5.14.2 !
一、效果图
功能很简单,不到200行代码,直接上图
二、源码
代码如下,注释很详细,就不再说明
2.1 头文件
#ifndef SNAKEGAME_H
#define SNAKEGAME_H
#include <QMainWindow>
#include <QLabel>
#include <QList>
#include <QTimer>
enum class Direction
{
Up = 1,
Down,
Left,
Right
};
class SnakeGame : public QMainWindow
{
Q_OBJECT
public:
explicit SnakeGame(QWidget *parent = 0);
~SnakeGame();
void keyPressEvent(QKeyEvent *event);
QLabel* getFood(void);
private slots:
void snakeMove(void);
private:
const int m_iFoodSize{ 30 };
QLabel* m_lbFood{ nullptr };
QList<QLabel*> m_lbListSnake{ };
QTimer* m_timerGame{ nullptr };
Direction m_eSnakeMoveDir{ Direction::Up };
};
#endif
2.2 源文件
#include "SnakeGame.h"
#include <QPalette>
#include <QTime>
#include <QPainter>
#include <QMessageBox>
SnakeGame::SnakeGame(QWidget *parent) :
QMainWindow(parent)
{
this->resize(800, 600);
qsrand(QTime::currentTime().msec());
m_lbListSnake.push_back(getFood());
m_lbListSnake[0]->setStyleSheet(QString("QLabel{background-color:red; border-radius:%1px;}").arg(m_iFoodSize / 2));
m_lbFood = getFood();
m_timerGame = new QTimer;
connect(m_timerGame, &QTimer::timeout, this, &SnakeGame::snakeMove);
m_timerGame->start(300);
}
SnakeGame::~SnakeGame()
{
}
void SnakeGame::keyPressEvent(QKeyEvent *event)
{
switch (event->key()) {
case Qt::Key_Up:
if(m_eSnakeMoveDir != Direction::Down)
{
m_eSnakeMoveDir = Direction::Up;
}
break;
case Qt::Key_Down:
if(m_eSnakeMoveDir != Direction::Up)
{
m_eSnakeMoveDir = Direction::Down;
}
break;
case Qt::Key_Left:
if(m_eSnakeMoveDir != Direction::Right)
{
m_eSnakeMoveDir = Direction::Left;
}
break;
case Qt::Key_Right:
if(m_eSnakeMoveDir != Direction::Left)
{
m_eSnakeMoveDir = Direction::Right;
}
break;
default:
break;
}
}
QLabel *SnakeGame::getFood()
{
QLabel* lbFood = new QLabel(this);
lbFood->resize(m_iFoodSize, m_iFoodSize);
lbFood->setAutoFillBackground(true);
lbFood->setPalette(QPalette(QColor(qrand() % 256, qrand() % 256, qrand() % 256)));
lbFood->setFrameShape(QFrame::Box);
int iPosX = ( qrand() % ( this->width() - m_iFoodSize) ) / m_iFoodSize * m_iFoodSize;
int iPosY = ( qrand() % ( this->height() - m_iFoodSize) ) / m_iFoodSize * m_iFoodSize;
lbFood->move(iPosX, iPosY);
lbFood->show();
return lbFood;
}
void SnakeGame::snakeMove()
{
int iSnakeHeadX = m_lbListSnake[0]->x();
int iSnakeHeadY = m_lbListSnake[0]->y();
if(iSnakeHeadX < 0 || iSnakeHeadY < 0 ||
iSnakeHeadX > this->width() || iSnakeHeadY > this->height())
{
QMessageBox::warning(this, "Tips", "game over!");
this->close();
}
int iSnakeLen = m_lbListSnake.length();
for(auto i=1; i<iSnakeLen; i++)
{
if(iSnakeHeadX == m_lbListSnake[i]->x() && iSnakeHeadY == m_lbListSnake[i]->y())
{
QMessageBox::warning(this, "Tips", "game over!");
this->close();
}
}
if(iSnakeHeadX == m_lbFood->x() && iSnakeHeadY == m_lbFood->y())
{
m_lbListSnake.push_back(m_lbFood);
m_lbFood = getFood();
}
for(auto i=iSnakeLen-1; i>0; --i)
{
m_lbListSnake[i]->move(m_lbListSnake[i-1]->pos());
}
switch (m_eSnakeMoveDir) {
case Direction::Up:
iSnakeHeadY -= m_iFoodSize;
break;
case Direction::Down:
iSnakeHeadY += m_iFoodSize;
break;
case Direction::Left:
iSnakeHeadX -= m_iFoodSize;
break;
case Direction::Right:
iSnakeHeadX += m_iFoodSize;
break;
default:
break;
}
m_lbListSnake[0]->move(iSnakeHeadX, iSnakeHeadY);
}
2.3 main函数
#include "SnakeGame.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
SnakeGame w;
w.show();
return a.exec();
}
|