一、概述
主要介绍打滑稽游戏项目开发的目的和意义。
1.1项目意义
以Qt Creater设计平台作为背景,使用C++编程语言,开发一款休闲娱乐的小游戏,使用户达到轻松娱乐的目的。
1.2开发环境
windows10操作系统,Qt Creater设计平台。
二、游戏玩法
打开游戏界面
点击开始游戏
用鼠标点击出现的滑稽脸
点到旁边的哆啦A梦五次,游戏失败
点击设置按键,可调整游戏难度
三、代码实现
不部分主要展示源文件包(Sources)下的代码实现(完整版源代码请见源代码文件)
dialog.cpp:
#include "dialog.h"
#include "ui_dialog.h"
/**
*游戏设置的小窗口
* 刷新间隔
* 最大滑稽(地鼠)数
*/
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
}
Dialog::~Dialog()
{
delete ui;
}
void Dialog::mousePressEvent(QMouseEvent *e)
{
if(maxforhuaji != ui->huajinumber->value())
maxforhuaji = ui->huajinumber->value();
if(timetonew != ui->newtime->value())
timetonew=ui->newtime->value();
//设置完成后,点击空间外任意地方,该函数生效
}
main.cpp:
#include "widget.h"
#include "startworld.h"
#include <QApplication>
#include <QTextCodec>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.setWindowTitle("打滑稽");
w.show();
return a.exec();
}
startworld.cpp:
#include "startworld.h"
#include "ui_startworld.h"
int score=0;
int errortime=0;
double timetonew=1;
int maxforhuaji=3;
void startworld::setbutton(QPushButton * bt)
{
bt->setStyleSheet("background: rgb(255, 255, 255)");
bt->setFlat(1);
// ui->stopButton->setIcon(QIcon(":/photo/picture/stopgame.jpg"));
// ui->stopButton->setIconSize(QSize(ui->stopButton->width(),ui->stopButton->height()));
}
startworld::startworld(QWidget *parent) :
QWidget(parent),
ui(new Ui::startworld)
{
ui->setupUi(this);
this->setWindowIcon(QIcon(":/photo/picture/Anmi.png"));
QPalette palette;
palette.setBrush(QPalette::Background,QBrush(QPixmap(":/photo/picture/Anmi.png")));
this->setStyleSheet("background-image:url(:/photo/picture/Anmi.png)");
this->setPalette(palette);
//************主界面
setbutton(ui->StartButton);
setbutton(ui->stopButton);
setbutton(ui->ExitButton);
m_pTimer = new QTimer(this);
connect(m_pTimer, SIGNAL(timeout()), this, SLOT(handleTimeout()));
ui->rgroupBox->setStyleSheet("background: rgb(231, 219, 255)");
ui->rgroupBox->setBackgroundRole(QPalette::ColorRole::Highlight);
ui->groupBox->resize(800,600);
//设置记分板
ui->getnumber->setSegmentStyle(QLCDNumber::Flat);
ui->losenumber->setSegmentStyle(QLCDNumber::Flat);
//设置背景
setbackground1();
//鼠标点击
keyPressEvent();
m_pTimer->start(timetonew*1000);
//changebackground1();
//time1 = startTimer(2000);
}
startworld::~startworld()
{
delete ui;
}
void startworld::mousePressEvent(QMouseEvent *e)
{
QObjectList list = ui->groupBox->children();
QLabel *b;
int i=0;
foreach (QObject *obj, list) {
b = qobject_cast<QLabel*>(obj);
if(b){
if(b->geometry().contains(this->mapFromGlobal(QCursor::pos()))&&play==1){
if(flag[i]==1){
changebackground2(b);//点击正确将爱酱换成滑稽
getscore(); //得分
flag[i]=0;
}
else{
getmistake();
lose();
}
}
}
i++;
}
}
void startworld::setbackground1() //刷新全图爱酱,紧接刷新滑稽
{
QObjectList list = ui->groupBox->children();
QLabel *b;
QImage *img = new QImage;
img->load(":/photo/picture/aijaing3.jpg");
int i=0;
foreach (QObject *obj, list) {
b = qobject_cast<QLabel*>(obj);
if(b){
//b->resize(img->width(),img->height());
b->setPixmap(QPixmap::fromImage(*img));
flag[i]=0; //不能被点
}
i++;
}
}
void startworld::changebackground1() //刷新滑稽(地鼠)
{
qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
QLabel *b;
QObjectList list = ui->groupBox->children();
QImage *img = new QImage;
img->load(":/photo/picture/huaji.jpg");
//qDebug()<<maxforhuaji; 测试数据
int number = qrand()%maxforhuaji+1;
qDebug()<<number;
for(int i=0; i<number; i++)
{
int test =qrand()%17;
b=qobject_cast<QLabel*>(list.value(test));
if(b){
b->setPixmap(QPixmap::fromImage(*img));
flag[test]=1; //可以被点
}
}
}
void startworld::changebackground2(QLabel *label) //当滑稽被点换成大哭
{
QImage *img = new QImage;
img->load(":/photo/picture/cry.jpg");
label->setPixmap(QPixmap::fromImage(*img));
}
void startworld::timerEvent(QTimerEvent *event) //计时,刷新全图爱酱
{
if (event->timerId() == time1) {
setbackground1();
}
}
void startworld::handleTimeout()
{
//判断是否结束
setbackground1();
changebackground1();
/*if(m_pTimer->isActive()){
m_pTimer->stop();
}*/
}
void startworld::getscore() //点对得分
{
score= score +10;
ui->getnumber->display(score);
}
void startworld::getmistake(){ //若点错
errortime++;
ui->losenumber->display(errortime);
//音效添加
QSoundEffect *login=new QSoundEffect(this);
login->setSource(QUrl::fromLocalFile(":/listeb/music/huaq.wav"));
login->play();
}
void startworld::lose() //超过五个失误,游戏结束+弹窗
{
if(errortime>5){
//****游戏结束,返回主菜单还是重新开始
if( QMessageBox::question(this,
tr("fail"),
tr("you have losed,do you want to try again?"),
QMessageBox::Yes, QMessageBox::No )
== QMessageBox::Yes){
//e->accept();//不会将事件传递给组件的父组件
//继续
score =0; //重置游戏计数
errortime=-1;
}else
{
//返回
score =0;
errortime=-1;
this->close();
}
}
}
void startworld::on_stopButton_clicked() //暂停
{
m_pTimer->stop();
play = 0;
}
void startworld::on_StartButton_clicked() //继续
{
m_pTimer->start(timetonew*1000);
play = 1;
}
void startworld::on_ExitButton_clicked() //退出
{
if( QMessageBox::question(this,
tr("Quit"),
tr("Are you sure to quit this application?"),
QMessageBox::Yes, QMessageBox::No )
== QMessageBox::Yes){
this->close();
delete this;
}
else{
}
}
void startworld::keyPressEvent() //更改鼠标样式
{
//本来是要按键变换的所以叫keyPressEvent,后来懒得写了2333
QCursor my(QPixmap(":/photo/picture/chanshizhe.png"));
this->setCursor(my);
}
widget.cpp:
#include "widget.h"
#include "ui_widget.h"
//全局变量
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
//设置背景,一般就这三句,大多控件也适用
QPalette palette;
palette.setBrush(QPalette::Background,QBrush(QPixmap(":/photo/picture/firstbeijing.jpg")));
this->setPalette(palette);
//设置背景音乐
QSoundEffect *login=new QSoundEffect(this);
login->setSource(QUrl::fromLocalFile(":/listeb/music/Schnappi.wav"));
login->setLoopCount(QSoundEffect::Infinite); //参数-循环播放
login->play();
QImage Image;
Image.load(":/photo/picture/shezhi.jpg");
QPixmap pixmap = QPixmap::fromImage(Image);
int with = ui->setbt->width();
int height = ui->setbt->height();
QPixmap fitpixmap = pixmap.scaled(with, height, Qt::IgnoreAspectRatio, Qt::SmoothTransformation); // 饱满填充
//QPixmap fitpixmap = pixmap.scaled(with, height, Qt::KeepAspectRatio, Qt::SmoothTransformation); // 按比例缩放
ui->setbt->setIcon(fitpixmap);
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_startbt_clicked() //开始游戏
{
start=new startworld(this);
start->show();
}
void Widget::on_exitbt_clicked() //退出游戏
{
exit(0);
}
void Widget::on_setbt_clicked() //设置
{
shezhi = new Dialog(this);
shezhi->show();
}
完整源码请见:https://download.csdn.net/download/weixin_43372169/60723161