linux QT下的别踩白方块

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

部分示例代码:
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;");
}

block.h

#ifndef BLOCK_H
#define BLOCK_H#include //抽象产品
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

gamesetting.h

#ifndef GAMESETTING_H
#define GAMESETTING_H#include 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

gameview.h

#ifndef GAMEVIEW_H
#define GAMEVIEW_H#include 
#include "block.h"
#include 
#include 
#include 
#include 
#include 
#include 
#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

widget.h

#ifndef WIDGET_H
#define WIDGET_H#include 
#include 
#include 
#include 
#include "gamesetting.h"
#include "gameview.h"
namespace Ui {
class Widget;
}class Widget : public QWidget
{Q_OBJECTpublic: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

gamesetting.cpp

#include "gamesetting.h"GameSetting::GameSetting()
{}

wight.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);
}


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部