马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
一.项目先容
贪吃蛇游戏是⼀款休闲益智类游戏。它通过控制蛇头方向吃食品,从而使得蛇变得越来越长。在本游戏中设置了上下左右四个⽅向键来控制蛇的移动方向。⻝物的产⽣是随机⽣成的,当蛇每吃⼀次⻝物就会增长⼀节身材,同时游戏积分也会相应的加⼀。在本游戏的筹划中,蛇的⾝相识越吃越长,身材越长对应的难度就越⼤,由于⼀旦蛇头和⾝体相交游戏就会竣事。
贪吃蛇项目地点
界面一:游戏大厅
当用户点击 “开始游戏” 按钮之后,就会进⼊到关卡选择界面。
界面二:关卡选择界面
在关卡选择界面上设置了三个游戏模式按钮,分别是:简单模式、正常模式、困难模式;⼀个 “汗青战绩” 按钮;⼀个返回游戏大厅界⾯的按钮。
界面三:游戏界面
在游戏界⾯,如果想要开始游戏,⾸先点击 “开始” 按钮,此时蛇就会移动而且尚有配景⾳效。如果想要停息游戏,那么点击 “停息” 按钮即可。当我们在游戏时,可以通过右边控制地区的上下左右⽅向键来控制蛇的移动。当蛇每吃⼀次⻝物时,陪同有吃⻝物的⾳效,蛇⾝会增长⼀节⻓度,而且分数
积分也会相应的加⼀。末了在控制地区的右下⻆结构了⼀个 “退出” 按钮,当点击 “退出” 按钮时,就会有⼀个弹窗提⽰,如下
如果我们点击 “Ok” ,则会退出到游戏关卡选择界⾯,此时我们可以重新选择游戏模式。如果点击“Cancel” ,则不会退出游戏,我们还是处在当前游戏房间界⾯。
二.项目实现
2.1 游戏大厅
游戏大厅界面比力简单, 只有⼀个配景图和⼀个按钮
- void GameHall::paintEvent(QPaintEvent *event)
- {
-
-
- //实例化画家对象
- QPainter painter(this);
- //实例化绘图设备
- QPixmap pix(":res/game_hall.png");
- //绘画
- painter.drawPixmap(0,0,this->width(),this->height(),pix);
- }
复制代码
- GameHall::GameHall(QWidget *parent)
- : QWidget(parent)
- , ui(new Ui::GameHall)
- {
-
-
- ui->setupUi(this);
- this->setFixedSize(1500,1000);//设置窗口大小
- this->setWindowTitle(QString("贪吃蛇游戏"));
- this->setWindowIcon(QIcon(":res/ico.png"));//设置窗口图标
- //
- QFont font("华文行楷",20);
- QPushButton * strbutton = new QPushButton(this);
- strbutton->setText("开始游戏");
- strbutton->setFont(font);
- strbutton->move(650,700);
- strbutton->setShortcut(QKeySequence(Qt::Key_Space));
- strbutton->setStyleSheet("QPushButton{border:0px}");//给按钮设置样式 去除边框
- GameSelect * gameSelect = new GameSelect;
- connect(strbutton,&QPushButton::clicked,[=](){
-
-
- this->close();//当前窗口关闭
- gameSelect->setGeometry(this->geometry());//设置第二个窗口于当前窗口一致
- gameSelect->show();//新的窗口打开
- QSound::play(":res/clicked.wav");
- });
- }
复制代码
- 这里的Qfound类,利用必要在.pro 的文件中加上 QT += core gui multimedia
- 这里利用strbutton->setShortcut(QKeySequence(Qt::Key_Space));给按钮添加了快捷键
- 利用strbutton->setStyleSheet("QPushButton{border:0px}");去除按钮边框样式
- 通过槽函数跳转到新的窗口 ,在槽函数中利用了Lambda 表达式
通过QSound创建音频对象,使点击按钮有声音
2.2关卡选择界面
关卡选择界⾯包罗⼀个配景图和五个按钮,配景图的绘制和游戏⼤厅配景图绘制⼀样,同样利用的是Qt中的绘图变乱,下面依次先容。
- void GameHall::paintEvent(QPaintEvent *event)
- {
-
-
- //实例化画家对象
- QPainter painter(this);
- //实例化绘图设备
- QPixmap pix(":res/game_hall.png");
- //绘画
- painter.drawPixmap(0,0,this->width(),this->height(),pix);
- }
复制代码
- #include "gameselect.h"
- #include"gameroom.h"
- #include<QIcon>
- #include<QPushButton>
- #include"gamehall.h"
- #include<QSound>
- #include<QPainter>
- GameSelect::GameSelect(QWidget *parent) : QWidget(parent)
- {
-
-
- this->setFixedSize(1500,1000);//设置窗口大小
- this->setWindowIcon(QIcon(":res/ico.png"));
- this->setWindowTitle("游戏关卡选择");
- QPushButton* backBtn = new QPushButton(this);//返回按钮
- backBtn->move(1400,900);
- backBtn->setIcon(QIcon(":res/back.png"));
- backBtn->setShortcut(QKeySequence(Qt::Key_Escape));
- connect(backBtn,&QPushButton::clicked,[=](){
-
-
- this->close();
- GameHall * gameHall = new GameHall;
- gameHall->show();
- QSound::play(":res/clicked.wav");
- });
- QFont font("华文行楷",20);
- gameroom*gameRoom = new gameroom;
- QPushButton* simpleBtn = new QPushButton(this);
- simpleBtn->move(650,300);
- simpleBtn->setFont(font);
- simpleBtn->setText("简单模式");
- connect(simpleBtn,&QPushButton::clicked,[=](){
-
-
- this->close();
- gameRoom->setGeometry(this->geometry());
- gameRoom->show();
- });
- QPushButton*normalBtn = new QPushButton(this);
- normalBtn->move(650,400);
- normalBtn->setFont(font);
- normalBtn->setText("正常模式");
- connect(normalBtn,&QPushButton::clicked,[=](){
-
-
- this->close();
- gameRoom->setGeometry(this->geometry());
- gameRoom->show();
- });
- QPushButton*hardBtn = new QPushButton(this);
- hardBtn->move(650,500);
- hardBtn->setFont(font);
- hardBtn->setText("困难模式");
- connect(hardBtn,&QPushButton::clicked,[=]{
-
-
- this->close();
- gameRoom->setGeometry(this->geometry());
- gameRoom->show();
- });
- QPushButton*hisBtn = new QPushButton(this);
- hisBtn->move(650,600);
- hisBtn->setFont(font);
- hisBtn->setText("历史战绩");
- }
- void GameSelect::paintEvent(QPaintEvent *event)
- {
-
-
- QPainter painter(this);//实例化画家对象
- QPixmap pix(":res/game_select.png");
- painter.drawPixmap(0,0,this->width(),this->height(),pix);
- }
复制代码 connect(hardBtn,&QPushButton::clicked,[=]{ this->close(); gameRoom->setGeometry(this->geometry()); gameRoom->show(); });
通过关联信号槽,使得可以选择游戏模式,起首把当前窗口关闭,然后体现新的窗口,在设置与当前窗口的巨细一样
2.3 游戏房间
游戏房间界面包罗下面几个部分:
- 配景的绘制
- 蛇的绘制,蛇的移动,判定蛇是否会撞到自己
- 积分的累加和绘制
怎么让蛇动起来?
- 我们可以⽤⼀个链表表现贪吃蛇,⼀个小方块表现蛇的⼀个节点, 我们设置蛇的默认⻓度为3;
- 向上移动的逻辑就是在蛇的上方加⼊⼀个小方块, 然后把末了⼀个小⽅块删除即可
- 必要用到定时器Qtimer 每100 - 200ms 重新渲染
怎么判定蛇有没有吃到食品?
怎么控制蛇的移动?
- 借助QT的实践机制实现, 重写keyPressEvent即可, 在函数中监控
想要的键盘变乱即可
- 通过绘制四个按钮,使⽤信号和槽的机制控制蛇的上、下、左、右移动方向
2.3.1 封装贪吃蛇数据结构
- #ifndef GAMEROOM_H
- #define GAMEROOM_H
- #include <QWidget>
- #include<QSound>
- enum class SnakeDirect
- {
-
-
- UP=0,
- DOWN,
- LEFT,
- RIGHT
- };
- class gameroom : public QWidget
- {
-
-
- Q_OBJECT
- public:
- explicit gameroom(QWidget *parent = nullptr);
- //重写绘图事件
- void paintEvent(QPaintEvent *event);
- void moveUp();
- void moveDown();
- void moveLeft();
- void moveRight();
- bool checkFail();
- void createNewFood();
- void setTimeout(int timeout){
-
- moveTime=timeout;}
- private:
- const int KSnakeNodeWidth = 20;
- const int KSnakeNodeHeight = 20;
- const int KDefaultTimeout = 200;//移动速度
- QList<QRectF> snakeList;//表示链表
- QRectF foodRect;//食物
- SnakeDirect moveDirect = SnakeDirect::UP;//默认移动方向
- QTimer *timer;//定时器
- bool isGameStart = false;//游戏的开始
- QSound *sound;
- int moveTime = KDefaultTimeout;
- };
- #endif // GAMEROOM_H
复制代码 2.3.2 初始化游戏房间界面
设置窗口巨细、标题、图标等
- gameroom::gameroom(QWidget *parent) : QWidget(parent)
- {
-
-
- this->setFixedSize(1500,1000);
- this->setWindowIcon(QIcon(":res/ico.png"));
- this->setWindowTitle("游戏房间");
- }
复制代码 2.3.3 蛇的移动
蛇的移动方向为:上、下、左、右。通过在游戏房间中结构四个按钮来控制蛇的移动方向。
· 留意: 这里贪吃蛇不答应直接掉头, 比如当前是向上的, 不能直接修改为向下。
- QPushButton*up= new QPushButton(this);
- QPushButton*down= new QPushButton(this);
- QPushButton*left= new QPushButton(this);
- QPushButton*right= new QPushButton(this);
- up->move(1320,750);
- down->move(1320,900);
- left->move(1220,825);
- right->move(1420,825);
- up->setText("↑");
- // up->setShortcut(QKeySequence(Qt::Key_W));
- down->setText("↓");
- // down->setShortcut(QKeySequence(Qt::Key_S));
- left->setText("←");
- // left->setShortcut(QKeySequence(Qt::Key_A));
- right->setText("→");
- // left->setShortcut(QKeySequence(Qt::Key_D));
- up->setStyleSheet("QPushButton{border:0px}");
- down->setStyleSheet("QPushButton{border:0px}");
- left->setStyleSheet("QPushButton{border:0px}");
- right->setStyleSheet("QPushButton{border:0px}");
- QFont ft("宋体",36);
- up->setFont(ft);
- down->setFont(ft);
- right->setFont(ft);
- left->setFont(ft);
- connect(up,&QPushButton::clicked,[=](){
-
-
- if(moveDirect!= SnakeDirect::DOWN)
- {
-
-
- moveDirect = SnakeDirect::UP;
- }
- });
- connect(down,&QPushButton::clicked,[=](){
-
- <
复制代码 |