# Qt练手项目之——贪吃蛇
最近在整理我之前做过的项目,翻到了大一时制作的贪吃蛇小项目。时间一晃而过,已经两年了。那会儿的我还是一个天天赶实验报告的小白……
这个项目实在学校实训的时候,匆匆制作的。还拿到了实训的“优”的评级,对于新手来说有一定的学习价值,对于大佬们来说就十分幼稚了。当时的我刚刚接触编程,对于C++还非常生疏,因此项目中有许多不符合软件开发规范的内容,还请诸位谅解。
未来如果有时间的话,我还会将这个项目进行重构优化。没有时间的话……就让它停留在这个最生涩的版本吧,毕竟这是我青春时鏖战了好几个晚上的成果,值得留恋。
如果本文对你有所帮助的话,请给我一个免费的赞,这会让更多的人看到这篇文章。
有任何意见或建议,欢迎评论或私信!
_我是希晨,希望给你带来快乐!_
> 关于作者:软件工程大学牲,喜欢尝试没有尝试过的事情,希望能永远行走在学习新知识的道路上!
>
> 有任何需求请添加本人微信,我会尽我所能帮助你,一起学习,共同进步!
>
> 微信号:`Xi_Chen66752878`,请备注`CSDN`
![微信二维码](https://img-blog.csdnimg.cn/7dafe66db89f4681bc268c829c11ab19.png#pic_center)
由于防盗外链图片的设置,README中部分图片将无法在Gitee中进行展示,可前往我的CSDN博客中进行详细的了解。
> CSDN博客:https://blog.csdn.net/Xi_Chen123/article/details/131928608
## 项目介绍
Qt实训时制作的小项目,贪吃蛇小游戏。很简单的练手,适合C++新手熟悉C++各种知识点与Qt编程。
![主页](https://img-blog.csdnimg.cn/89f532ec7e574088bc4bfa0f526de7c9.png#pic_center)
## 项目截图
![主页](https://img-blog.csdnimg.cn/89f532ec7e574088bc4bfa0f526de7c9.png#pic_center)
![模式选择](https://img-blog.csdnimg.cn/40ec6b1346b14a679309153723ca7aea.png#pic_center)
![游戏规则](https://img-blog.csdnimg.cn/354030a2fc1c4846ae2a1e22aedbe8c6.png#pic_center)
![经典模式](https://img-blog.csdnimg.cn/9f68fd21aecb4486a2c0dd1e785092d0.png#pic_center)
![双人模式](https://img-blog.csdnimg.cn/36c37c7a451e4d8181333a4b3125947c.png#pic_center)
![地狱模式](https://img-blog.csdnimg.cn/5bec53b1456c4ee28dd085cd0c3b3c3d.png#pic_center)
## 源码下载与发布版体验试玩
- 源码下载
1. 如您是软件开发者,想要阅读源码或对本项目进行自定义配置或修改,请选择master分支下载最新源码,请确保你拥有Qt开发环境与Qt Creator,或使用其他拥有Qt开发环境的编译器;
![下载源码](https://img-blog.csdnimg.cn/49145a19af61447d82623ffd26b59209.png#pic_center)
2. 解压下载好的zip包,或直接使用编译器插件进行clone;
3. 这里以Qt Creator为例,进入Qt Creator,点击左侧**打开项目**按钮,在文件资源选择器中选择`SnakeGame.pro`文件打开项目;
![打开项目](https://img-blog.csdnimg.cn/47f586bb9de44320b3712739ce82670c.png#pic_center)
![选择项目](https://img-blog.csdnimg.cn/579e99d2976242f4b7074e1cf8aca8ec.png#pic_center)
4. 待项目构建完成,控制台不输出相关错误时,点击项目左下角三角符号进行运行,或使用快捷键`Ctrl+R`,或右键项目父文件夹,在展开的选项卡中选择**运行**,即可在新增窗口中运行程序。
![运行项目](https://img-blog.csdnimg.cn/13f777ed16d7434bbb6fad84fc5ef2a8.png#pic_center)
![运行成功](https://img-blog.csdnimg.cn/bc9d1cb923124c9ab057222b2110293d.png#pic_center)
- 发布版体验试玩
1. 如您是游客,仅想体验一下本项目,请选择发行版下载最新发布版压缩包。无需拥有Qt开发环境
![在gitee仓库下载](https://img-blog.csdnimg.cn/73161c07d60644bcb6f0b68529c70193.png#pic_center)
![下载压缩包](https://img-blog.csdnimg.cn/5ef712c8f31648f889a34a15dac15962.png#pic_center)
2. 下载成功后,解压压缩包,点击文件夹中的`SnakeGame.exe`文件,即可进行游戏 ![运行程序](https://img-blog.csdnimg.cn/55fd6e37726649ff85b32560fc0e75d2.png#pic_center)
## 项目重点内容
- 按钮动画:
- 鼠标在按钮上点击,按钮高亮,鼠标松开后,恢复正常;若正常点击按钮,按钮跳动,动画结束后,实行按钮功能
![点击动画](https://img-blog.csdnimg.cn/9d45d5d1984d41498f3ef046dd18fff0.gif#pic_center)
- 实现过程:新建一个`MyPushButton`的类,继承`QPushButton`类。重写构造方法、鼠标点击事件、鼠标释放事件,并增添跳跃动画。
- 代码详解:
```c
// mypushbutton.h
#ifndef MYPUSHBUTTON_H
#define MYPUSHBUTTON_H
#include <QWidget>
#include <QPushButton>
#include <QPixmap>
#include <QPropertyAnimation>
class MyPushButton : public QPushButton
{
Q_OBJECT
public:
//explicit MyPushButton(QWidget *parent = nullptr);
//press image is empty path, if it is lack of the second path, there is no press animaltion
MyPushButton(QString normalImg, QString pressImg="");
QString normalImg;
QString pressImg;
void zoomDown();
void zoomUp();
//press event
void mousePressEvent(QMouseEvent* event);
//releaes event
void mouseReleaseEvent(QMouseEvent* event);
signals:
public slots:
};
#endif // MYPUSHBUTTON_H
```
```c
// mypushbutton.cpp
#include "mypushbutton.h"
//MyPushButton::MyPushButton(QWidget *parent) : QWidget(parent)
//{
//}
MyPushButton::MyPushButton(QString normalImg, QString pressImg)
{
//normal image path
this->normalImg=normalImg;
//press image path
this->pressImg=pressImg;
QPixmap pix;
//loag normalImg
pix.load(normalImg);
//set fixed size
this->setFixedSize(pix.width(), pix.height());
//set style sheet
this->setStyleSheet("QPushButton{border:0px;}");
//set icon
this->setIcon(pix);
//set icon size
this->setIconSize(QSize(pix.width(), pix.height()));
}
void MyPushButton::zoomDown()
{
//create animation
QPropertyAnimation* animation=new QPropertyAnimation(this, "geometry");
//set time gap
animation->setDuration(200);
//start
animation->setStartValue(QRect(this->x(), this->y(), this->width(), this->height()));
//end
animation->setEndValue(QRect(this->x(), this->y()+10, this->width(), this->height()));
//set animation rules
animation->setEasingCurve(QEasingCurve::OutBounce);
animation->start();
}
void MyPushButton::zoomUp()
{
//create animation
QPropertyAnimation* animation=new QPropertyAnimation(this, "geometry");
//set time gap
animation->setDuration(200);
//start
animation->setStartValue(QRect(this->x(), this->y()+10, this->width(), this->height()));
//end
animation->setEndValue(QRect(this->x(), this->y(), this->width(), this->height()));
//set animation rules
animation->setEasingCurve(QEasingCurve::OutBounce);
animation->start();
}
void MyPushButton::mousePressEvent(QMouseEvent *event)
{
QPixmap pix;
//load press img
pix.load(pressImg);
//set fixed size
this->setFixedSize(pix.width(), pix.height());
//set style sheet
this->setStyleSheet("QPushButton{border:0px;}");
//set icon
this->setIcon(pix);
//set icon size
this->setIconSize(QSize(pix.width(), pix.height()));
//let parent do other thing
return QPushButton::mousePressEvent(event);
}
void MyPushButton::mouseReleaseEvent(Q