IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 游戏开发 -> 基于Linux、QT、C++的“别踩白块儿”小游戏 -> 正文阅读

[游戏开发]基于Linux、QT、C++的“别踩白块儿”小游戏

基于Linux、QT、C++的“别踩白块儿”小游戏

一、功能实现

完善的游戏界面、游戏倒计时、得分记录、历史最高分显示

二、功能描述

1、界面为4*4,一行中只有一个黑块,使用qrand函数,采用时间种子保证
每次产生是随机数不同,在将随机数对4取余来作为黑块的位置。
2、初始时间设定值为30,通过定时器每100ms发出一次信号,刷新时间。
3、通过工厂模式,完成对黑块和白块的生产,并采用queue容器来储存块。
4、当玩家点击黑块时,delete队头的4个块并pop弹出,在加入4个新块,
最后将队列中所有的块Y坐标增加。

三、界面展示

请添加图片描述

请添加图片描述
请添加图片描述
在这里插入图片描述

四、代码展示

在这里插入图片描述
1、block.h

#ifndef BLOCK_H
#define BLOCK_H

#include <QWidget>

//抽象产品
class Block : public QWidget
{
public:
    void down(){
        move(pos().x(),pos().y() + 120);
    }
    Block(QWidget *parent = nullptr);
    virtual bool isBlack() = 0;
};

//具体产品 蓝块
class BlueBlock : public Block
{
    public:
    bool isBlack(){ return true;}
    BlueBlock(QWidget *parent = nullptr);
};
//具体产品 白块
class WhiteBlock : public Block
{
    public:
      bool isBlack(){ return false;}
    WhiteBlock(QWidget *parent = nullptr);
};

//抽象工厂
class Factory
{
public :
    virtual Block* CreateBlock(QWidget *parent = nullptr) = 0;
};

//具体产品工厂 蓝块工厂
class BlueBlockFactory : public Factory
{
public :
    Block* CreateBlock(QWidget *parent = nullptr)
    {
        Block* blueBlock = new BlueBlock(parent);
        return blueBlock;
    }
};

//具体产品工厂 白块工厂
class WhiteBlockFactory : public Factory
{
public :
    Block* CreateBlock(QWidget *parent = nullptr)
    {
        Block* whiteBlock = new WhiteBlock(parent);
        return whiteBlock;
    }
};

#endif // BLOCK_H

2、gemesetting.h

#ifndef GAMESETTING_H
#define GAMESETTING_H

#include <QWidget>

class GameSetting //记录最高分
{
private:
    GameSetting();
public:
    int highscore = 0;
    static GameSetting* getInstance()
    {
        //是一个static的对象
        static GameSetting* m_instance;
        if(m_instance == NULL)
        {
            m_instance = new GameSetting;
        }
        return m_instance;
    }
};

#endif // GAMESETTING_H

3、gameview.h

#ifndef GAMEVIEW_H
#define GAMEVIEW_H

#include <QWidget>
#include "block.h"
#include <deque>
#include <QTime>
#include <QTimer>
#include <QDebug>
#include <QMouseEvent>
#include <QMessageBox>
#include "gamesetting.h"
using namespace std;

namespace Ui {
class gameview;
}

class gameview : public QWidget
{
    Q_OBJECT
signals:
    void gameoversignal();
public:
    explicit gameview(QWidget *parent = 0);
    ~gameview();
    void initgameview();
    void mousePressEvent(QMouseEvent *event);
    void updategameview();
    void resetgame();
    void gameover();
private slots:
//    void on_scoreLcd_overflow();

//    void on_GameView_destroyed();

private:
    float lasttime = 30;
    int score = 0;
    //定时器
    void timeoutslot();
    QTimer* timer;
    Ui::gameview *ui;
    BlueBlockFactory blueFactory;
    WhiteBlockFactory whiteFacyory;
    //队列 存放块
    deque<Block*> blockdeque;
    int generateRandomNumber();
};

#endif // GAMEVIEW_H

4、widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QWidget>
#include <QMessageBox>
#include <QPainter>
#include "gamesetting.h"
#include "gameview.h"
namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = 0);
    void paintEvent(QPaintEvent*event);
    ~Widget();

private slots:
    void on_pushButton_2_clicked();

    void on_pushButton_clicked();

    void on_pushButton_3_clicked();
    void gameoverslot();
private:
    gameview* gameVc;
    Ui::Widget *ui;
};

#endif // WIDGET_H

5、block.cpp

#include "block.h"

Block::Block(QWidget *parent) : QWidget(parent)
{
    resize(80,120);
}
WhiteBlock::WhiteBlock(QWidget *parent) : Block(parent)
{
     this->setStyleSheet("background-color:white;");
    //或者使用重绘事件  画背景图
}
BlueBlock::BlueBlock(QWidget *parent) : Block(parent)
{
     this->setStyleSheet("background-color:black;");
}

6、gamesettingc.cpp

#include "gamesetting.h"

GameSetting::GameSetting()
{

}

7、gameview.cpp

#include "gameview.h"
#include "ui_gameview.h"

gameview::gameview(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::gameview)
{
    ui->setupUi(this);
    setFixedSize(320,480);
    this->setWindowTitle(QStringLiteral("开始游戏"));
    ui->timeLcd->setStyleSheet("background-color:red;");
    ui->scoreLcd->setStyleSheet("background-color:yellow;");
    timer = new QTimer(this);
    connect(timer,&QTimer::timeout,this,&gameview::timeoutslot);
}

//初始化界面
void gameview::initgameview()
{
    timer->start(100);//开始

    for(int i = 0; i < 4; i++)//行号
    {
        int idex = generateRandomNumber();
        for(int j = 0; j < 4; j++)//列
        {
            Block* block; //块
            if(idex == j)//点击了黑块
            {
                block = blueFactory.CreateBlock(this);
            }
            else
            {
                block = whiteFacyory.CreateBlock(this);
            }
            block->move(j*80,i*120);
            block->stackUnder(ui->topbarWidget);
            block->show();//显示块
            blockdeque.push_back(block);
        }
    }

}

//获得鼠标点击事件  判断坐标点位置
void gameview::mousePressEvent(QMouseEvent *event)
{
    //根据坐标点 从队列获取块
    int row = event->y()/120;
    int col = event->x()/80;
    if(blockdeque.at(row*4 + col)->isBlack())
    {
        if(row == 3)
        {
            updategameview();//点击到蓝块,进行处理
        }

    }
    else
    {
        gameover();
    }
}

void gameview::updategameview()//更新分数
{
    //更新得分
    score ++;
    if(GameSetting::getInstance()->highscore < score)
    {
        GameSetting::getInstance()->highscore = score;
    }
    ui->scoreLcd->display(score);

    //清除最下面一行色块
    for(int i = 0; i < 4; i++)
    {
        delete blockdeque.back();
        blockdeque.pop_back();
    }

    //增加4个色块
    int idex = generateRandomNumber();
    for(int i = 0; i < 4; i++)
    {
        Block* block;
        if(idex == i)
        {
            block = blueFactory.CreateBlock(this);
        }
        else
        {
            block = whiteFacyory.CreateBlock(this);
        }
        block->move(i*80,-120);
        block->stackUnder(ui->topbarWidget);
        block->show();//显示色块
        blockdeque.insert(blockdeque.begin()+i,block);
    }

    for(int i = 0; i < 16; i++)
    {
        blockdeque.at(i)->down();
    }
}

//倒计时
void gameview::timeoutslot()
{
    lasttime -= 0.1;
    ui->timeLcd->display(lasttime);
    if(lasttime <= 0)
    {
        gameover();
    }
}

//游戏结束
void gameview::gameover()
{
    timer->stop();
    QString str = QString("很厉害啊!\n得分: %1 \n 是否再玩一次?").arg(score);
    int ret = QMessageBox::question(this,"游戏结束",str);
    if(ret == QMessageBox::Yes)//继续游戏
    {
        resetgame();
        initgameview();
    }
    else
    {
        resetgame();
        emit gameoversignal();
    }
}

void gameview::resetgame()//重置
{
    lasttime = 10;
    score = 0;
    ui->timeLcd->display(lasttime);
    ui->scoreLcd->display(0);
    for(int i =  0; i < 16; i++)
    {
       delete blockdeque.at(i);
    }
    blockdeque.clear();
}

int gameview::generateRandomNumber() //生成随机数
{
    int randn;
    QTime time = QTime::currentTime();
    qsrand(time.msec()*qrand()*qrand()*qrand()*qrand());//初始化随机数种子
    randn = qrand()%4; //生成0 - 3 之间的随机数
    return randn;
}

gameview::~gameview()
{
    delete ui;
}

8、main.cpp

#include "widget.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();

    return a.exec();
}

9、widget.cpp

#include "widget.h"

#include "ui_widget.h"
Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    setFixedSize(320,480);
    this->setWindowTitle(QStringLiteral("别踩白块"));
    GameSetting::getInstance();
    gameVc = new gameview;
    gameVc->hide();
    connect(gameVc,&gameview::gameoversignal,this,&Widget::gameoverslot);
}

void Widget::paintEvent(QPaintEvent *)
{
    QPainter paint(this);
    paint.drawPixmap(0,0,width(),height(),QPixmap(":/resources/img_bg_level_2.jpg"));
}

Widget::~Widget()
{
    delete ui;
}
void Widget::on_pushButton_3_clicked()
{
    gameVc->show();
    hide();
    gameVc->initgameview();
//    this->hide();//隐藏本窗口
//     gameview *child = new gameview();//进入游戏界面
//    // child->move(QPoint(100,100));//窗口位置
    //     child->show();
}

void Widget::gameoverslot()
{
    gameVc->hide();
    show();
}

void Widget::on_pushButton_2_clicked()
{
    QMessageBox::about(this,"玩法","在30秒内尽量多的点击黑块!\n如果点击到白块,游戏立刻结束!");
}

void Widget::on_pushButton_clicked()
{
    QString str = QString("历史最高分:\n\n      %1").arg(GameSetting::getInstance()->highscore);
    QMessageBox::about(this,"最高分",str);
}



9、gameview.ui
在这里插入图片描述
10、widget.ui
在这里插入图片描述

  游戏开发 最新文章
6、英飞凌-AURIX-TC3XX: PWM实验之使用 GT
泛型自动装箱
CubeMax添加Rtthread操作系统 组件STM32F10
python多线程编程:如何优雅地关闭线程
数据类型隐式转换导致的阻塞
WebAPi实现多文件上传,并附带参数
from origin ‘null‘ has been blocked by
UE4 蓝图调用C++函数(附带项目工程)
Unity学习笔记(一)结构体的简单理解与应用
【Memory As a Programming Concept in C a
上一篇文章      下一篇文章      查看所有文章
加:2021-08-16 12:04:02  更:2021-08-16 12:04:19 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/20 17:27:28-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码