///////////////////////////////////////////////////////////////
/**
* @file gamewindow.cpp
*
* @brief 主窗口
*
* @author MollyNeko
*
* @date 2018-08-01
*
**/
///////////////////////////////////////////////////////////////
#include <QPainter>
#include <QMessageBox>
#include <QTime>
#include "gamewindow.h"
#define MAP_COLOR Qt::GlobalColor
bool isLock();
void lock();
void unlock();
/* 构造类函数 */
GameWindow::GameWindow(QWidget *parent):
m_iMapSize(20),
QWidget(parent)
{
m_timer = new QTimer(this);
SetMapSize(m_iMapSize);
StartGame(300);
}
/* 控制函数 */
///////////////////////////////////////////////////////////////
/**
* @brief 设置地图尺寸
*
* @param size 要设置的尺寸
**/
///////////////////////////////////////////////////////////////
void GameWindow::SetMapSize(qint32 size)
{
m_iMapSize = size;
setFixedSize(30*m_iMapSize, 30*m_iMapSize);
// 分配空间
m_tMap = new t_MapStatus*[m_iMapSize];
for(qint32 row = 0; row < m_iMapSize; row++)
{
m_tMap[row] = new t_MapStatus[m_iMapSize];
}
// 初始化值
for(qint32 row = 0; row < m_iMapSize; row++)
{
for(qint32 col = 0; col < m_iMapSize; col++)
{
m_tMap[row][col] = t_MapStatus::STATUS_FIELD;
}
}
}
///////////////////////////////////////////////////////////////
/**
* @brief 把长条猫和食物放到地图上
**/
///////////////////////////////////////////////////////////////
void GameWindow::PlaceScene()
{
t_CatNode tCatHead = m_LongCat.GetCatHead();
SetMapNode(tCatHead, t_MapStatus::STATUS_HEAD);
for(int itor = 0; itor < m_LongCat.GetCatLength() - 1; itor++)
{
t_CatNode tCatBody = m_LongCat.GetCatBody(itor);
SetMapNode(tCatBody, t_MapStatus::STATUS_BODY);
}
SetMapNode(m_CurFood, t_MapStatus::STATUS_FOOD);
}
void GameWindow::OverGame()
{
m_timer->stop();
}
void GameWindow::BloomFood()
{
qint32 posX, posY;
qsrand((QTime(0, 0, 0).secsTo(QTime::currentTime())));
posX = qrand() % m_iMapSize;
posY = qrand() % m_iMapSize;
t_CatNode tCatNode(posX, posY);
m_CurFood = tCatNode;
if(m_LongCat.isContains(tCatNode))
{
BloomFood();
}
}
/* 事件函数 */
void GameWindow::paintEvent(QPaintEvent *event)
{
Q_UNUSED(event);
// 开始绘制
QPainter painter(this);
painter.setPen(MAP_COLOR::black);
for(qint32 row = 0; row < m_iMapSize; row++)
{
for(qint32 col = 0; col < m_iMapSize; col++)
{
SwitchColor(painter, m_tMap[row][col]);
painter.drawRect(30*row, 30*col, 30, 30);
}
}
}
void GameWindow::keyPressEvent(QKeyEvent *event)
{
switch(event->key())
{
case Qt::Key_Up:
{
m_LongCat.SetDirect(t_CatDirect::DIRECT_UP);
break;
}
case Qt::Key_Down:
{
m_LongCat.SetDirect(t_CatDirect::DIRECT_DOWN);
break;
}
case Qt::Key_Left:
{
m_LongCat.SetDirect(t_CatDirect::DIRECT_LEFT);
break;
}
case Qt::Key_Right:
{
m_LongCat.SetDirect(t_CatDirect::DIRECT_RIGHT);
break;
}
default:
{
break;
}
}
OnTimeOut();
}
/* 私用函数 */
///////////////////////////////////////////////////////////////
/**
* @brief 建立信号和槽
**/
///////////////////////////////////////////////////////////////
void GameWindow::SetConnections()
{
connect(m_timer, SIGNAL(timeout()),
this, SLOT(OnTimeOut()));
}
void GameWindow::SetMapNode(t_CatNode node, t_MapStatus status)
{
qint32 posX = node.posX, posY = node.posY;
m_tMap[posX][posY] = status;
}
/* 分流函数 */
///////////////////////////////////////////////////////////////
/**
* @brief 根据地图该点状态为画笔设置Brush属性
*
* @param painter 引用的画笔对象
*
* @param status 当前点状态
**/
///////////////////////////////////////////////////////////////
void GameWindow::SwitchColor(QPainter &painter, t_MapStatus status)
{
switch (status)
{
case t_MapStatus::STATUS_FIELD:
{
painter.setBrush(MAP_COLOR::gray);
break;
}
case t_MapStatus::STATUS_FOOD:
{
painter.setBrush(MAP_COLOR::darkYellow);
break;
}
case t_MapStatus::STATUS_BODY:
{
painter.setBrush(MAP_COLOR::green);
break;
}
case t_MapStatus::STATUS_HEAD:
{
painter.setBrush(MAP_COLOR::red);
break;
}
default:
{
painter.setBrush(MAP_COLOR::gray);
break;
}
}
}
/* 槽函数 */
void GameWindow::OnTimeOut()
{
if(isLock())
{
return;
}
lock();
// 清理尾部
t_CatNode tCatTail = m_LongCat.GetCatTail();
SetMapNode(tCatTail, t_MapStatus::STATUS_FIELD);
// 逻辑判断及刷新
m_LongCat.TimeOut();
bool isDead = m_LongCat.isOutLimit() || m_LongCat.isEatSelf();
if(isDead)
{
OverGame();
QMessageBox::critical(this, "You are loser", "Game Over!");
return;
}
// 判断长条猫是否吃到食物
if(m_LongCat.GetCatHead() == m_CurFood)
{
m_LongCat.AddHead(m_CurFood);
SetMapNode(m_CurFood, t_MapStatus::STATUS_HEAD);
BloomFood();
}
// 更新图像
PlaceScene();
update();
unlock();
}
void GameWindow::StartGame(const qint32 speed)
{
BloomFood();
PlaceScene();
SetConnections();
m_timer->start(speed);
}
void GameWindow::StartAi()
{
}
C++ Qt5贪吃蛇源码
需积分: 47 107 浏览量
2019-06-12
09:10:20
上传
评论
收藏 7KB 7Z 举报
MollyNeko
- 粉丝: 5
- 资源: 2
最新资源
- 基于安国AU6438读卡器PADS9.5设计硬件(原理图+PCB)文件.zip
- 基于python+flask+mysql实现的豆瓣电影可视化系统源代码+数据库(高分项目).zip
- 基于pytorch卷积神经网络算法识别猫狗图片项目源码+模型+数据集+使用说明.zip
- 基于C++ QT实现的学生信息管理系统源码+项目说明.zip
- IMG20240425152027.jpg
- 点这里下载真实好友等1个文件.rar.zip
- 基于tensorflow同态加密的联邦学习安全聚合系统源码+运行说明.zip
- 七彩祥云丶看的的好运连连
- 土石方工程采用“南方cass”软件计算工程量的.doc
- mmexport1922918815783c336136a3e10a1bae88_1715378948463.png
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈