# 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
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
【项目资源】: 包含前端、后端、移动开发、操作系统、人工智能、物联网、信息化管理、数据库、硬件开发、大数据、课程资源、音视频、网站开发等各种技术项目的源码。 包括STM32、ESP8266、PHP、QT、Linux、iOS、C++、Java、python、web、C#、EDA、proteus、RTOS等项目的源码。 【项目质量】: 所有源码都经过严格测试,可以直接运行。 功能在确认正常工作后才上传。 【适用人群】: 适用于希望学习不同技术领域的小白或进阶学习者。 可作为毕设项目、课程设计、大作业、工程实训或初期项目立项。 【附加价值】: 项目具有较高的学习借鉴价值,也可直接拿来修改复刻。 对于有一定基础或热衷于研究的人来说,可以在这些基础代码上进行修改和扩展,实现其他功能。 【沟通交流】: 有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 鼓励下载和使用,并欢迎大家互相学习,共同进步。
资源推荐
资源详情
资源评论
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
收起资源包目录
![package](https://csdnimg.cn/release/downloadcmsfe/public/img/package.f3fc750b.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/GIF.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/RAR.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
共 90 条
- 1
资源评论
![avatar-default](https://csdnimg.cn/release/downloadcmsfe/public/img/lazyLogo2.1882d7f4.png)
![avatar](https://profile-avatar.csdnimg.cn/421fe3cf48564128aca47ee460992edd_m0_63168877.jpg!1)
![avatar-vip](https://csdnimg.cn/release/downloadcmsfe/public/img/user-vip.1c89f3c5.png)
妄北y
- 粉丝: 1w+
- 资源: 1万+
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![voice](https://csdnimg.cn/release/downloadcmsfe/public/img/voice.245cc511.png)
![center-task](https://csdnimg.cn/release/downloadcmsfe/public/img/center-task.c2eda91a.png)
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback-tip](https://img-home.csdnimg.cn/images/20220527035111.png)
安全验证
文档复制为VIP权益,开通VIP直接复制
![dialog-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/green-success.6a4acb44.png)