前言:在经历了前几篇文章的学习后,我们也是已经基本的掌握了Qt的基础语法与开发,为了巩固我们所学的知识,现在就一起动手来实现一个贪吃蛇小游戏吧。😄
游戏规则:
⭐️蛇可以在任意墙壁之间来回穿梭 ⭐️:
⭐️ 吃食物可以增加长度,同时记录分数 ⭐️:
💥当蛇的身体相撞游戏结束💥
本文目录
1.渲染游戏大厅
首先第一步我们需要准备被我们的资源并创建我们的项目,加载资源并且开始渲染贪吃蛇游戏的界面,我们的游戏主页面其实就是一张背景图与一个按钮,背景图的设置有多种方式:使用绘图事件,使用stylesheet都是可以的。同时为了增强代码编写能力,我们这里都是用纯代码的方式,就不用ui界面进行绘制了。
这里我们使用绘图事件,需要注意的是如果想使用stylesheet,主界面是不能直接修改背景图,而是需要在此基础上添加一个frame,让两者的大小一致,再frame中stylesheet设置图片。
设置鱼鳔,标题,窗口大小,设置按钮,绑定槽函数:
#include "gameball.h"
#include "ui_gameball.h"
GameBall::GameBall(QWidget *parent)
: QWidget(parent)
, ui(new Ui::GameBall)
{
//设置窗口大小
this->setFixedSize(800,600);
this->setWindowIcon(QIcon(":/res/ico.png"));
this->setWindowTitle(tr("贪吃蛇小游戏"));
//游戏开始按钮
this->startbutton=new QPushButton("开始游戏",this);
startbutton->setGeometry(300,450,180,50);//设置按钮位置
//设置字体
QFont font;
font.setFamily("华文行楷");
font.setPointSize(20);
startbutton->setFont(font);
//当然这里用样式表是一样的
//设置一下样式
startbutton->setStyleSheet("QPushButton{background-color:#344c93;border:0px}QPushButton:hover{background-color:rgb(0,100,100);border:1px,solida,balck}");
//当点击开始游戏按钮,此时跳转到游戏选择界面
connect(this->startbutton,&QPushButton::clicked,this,&GameBall::gameSelect);
}
GameBall::~GameBall()
{
delete ui;
}
//重写绘图事件
void GameBall::paintEvent(QPaintEvent *event)
{
//画手
QPainter painter(this);//画手
painter.drawPixmap(0,0,this->width(),this->height(),QPixmap(":/res/game_hall.png"));//绘制开始游戏背景图片
}
2.构建游戏选择大厅
改界面的实现,我们会新建一个类继承Qwidget,在鼠标点击信号产生时,构建新窗口,关闭就窗口,同时进行渲染,添加游戏开始的音乐。
以下是鼠标槽函数:
void GameBall::gameSelect()
{
// 产生点击音效
QSoundEffect soundEffect;//使用QSoundeffect对象实现
QUrl source(QStringLiteral("qrc:/res/clicked.wav")); //设置资源路径
soundEffect.setSource(source);
soundEffect.setLoopCount(1); // 只播放一次
soundEffect.setVolume(0.6);//音量50%
soundEffect.play();//播放
//此时新建一个窗口作为我们游戏选择选择界面
this->selectWindow=new GameSelect();
//同时隐藏我们的窗口
this->close();
soundEffect.deleteLater();//关闭也清理音乐资源
}
接下来就是游戏选择大厅的界面实现😎,游戏选择大厅主要有背景图和四个按钮,三个按钮表示游戏难度模式,一个按钮用来展示游戏的历史记录,当点击按钮使就会弹到对应的界面,所以这里主要是按钮槽函数的实现–打开对应窗口,完整的实现还需要先把历史剧路页面与游戏页面线实现出来,所以这里我先给出基本组建的实现:
#include "gameselect.h"
#include"gameball.h"
#include<QSoundEffect>
GameSelect::GameSelect(QWidget *parent)
: QWidget{
parent}
{
//设置窗口大小
this->setFixedSize(800,600);
this->setWindowIcon(QIcon(":/res/ico.png"));
this->setWindowTitle(tr("游戏模式选择"));
//设置按钮
backbutton=new QPushButton(this);//设置返回按钮
backbutton->setGeometry(700,530,50,40);
QIcon icon(QPixmap(":/res/back.png"));
backbutton->setIcon(icon);
backbutton->setIconSize(QSize(40,40));//设置图标的大小
backbutton->setStyleSheet("border-width:0px;");
backbutton->setFlat(true);//设置边框不可见,有bug,设置了就消失了
connect(backbutton,&QPushButton::clicked,this,[=](){
this->close();
GameBall * gameball=new GameBall();
gameball->show();
//添加音效
playmusic();//点击音乐
});
//音频
music =new QMediaPlayer(this);
output=new QAudioOutput(this);
//字体设置
QFont font;
font.setFamily("华文行楷");
font.setPointSize(20);
//简单模式按钮
this->easybutton=new QPushButton(this);
this->easybutton->setGeometry(300,100,140,40);
this->easybutton->setText("简单模式");
this->easybutton->setFont(font);
//中等模式按钮
this->midbutton=new QPushButton(this);
this->midbutton->setGeometry(300,160,140,40);
this->midbutton->setText("中等模式");
this->midbutton->setFont(font);
//困难模式按钮
this->hardbutton=new QPushButton(this);
this->hardbutton->setGeometry(300,220,140,40);
this->hardbutton->setText("困难模式");
this->hardbutton->setFont(font);
this->historybutton=new QPushButton(this);
this->historybutton->setGeometry(300,280,140,40);
this->historybutton->setText("历史记录");
this->historybutton->setFont(font);
//历史记录信号处理
connect(this->historybutton,&QPushButton::clicked,this,[=](){
playmusic();
this->historywindow=new HistoryWidget();
this->historywindow->show();
this->close();
});
//简单模式的游戏
connect(easybutton,&QPushButton::clicked,this,[=](){
//添加音乐
playmusic();//点击音乐
//创建出游戏界面
gameWidget=new GameRoom()