#include "panel.h"
#include "ui_panel.h"
#include "enumHeader.h"
#include <QDebug>
#include <QGraphicsBlurEffect>
#include <QPropertyAnimation>
#include <QMessageBox>
#include <QMediaContent>
Panel::Panel(QWidget *parent) :
QWidget(parent),
ui(new Ui::Panel)
{
ui->setupUi(this);
m_isPauseGame = false;
m_isGameOver = false;
m_mediaPlayer = new QMediaPlayer;
m_mediaPlayer->setMedia(QUrl::fromLocalFile(QString("clearRow.mp3")));
m_mediaPlayer->setVolume(100);
//设置背景
setbackground(LV0);
initControlWidget();
initGraphicsViewWidget();
}
Panel::~Panel()
{
delete ui;
}
void Panel::setbackground(const int &Lv)
{
setAutoFillBackground(true);
switch (Lv) {
case LV0:
m_currentLv = LV0;
m_currentLVSpeed = LV0_SPEED;
ui->label_gameLevel->setText("俄罗斯方块");
m_pixmap.load(":/background/Image/background/background.png");
break;
case LV1:
m_currentLv = LV1;
m_currentLVSpeed = LV1_SPEED;
ui->label_gameLevel->setText("第一关");
m_pixmap.load(":/background/Image/background/background01.png");
break;
case LV2:
m_currentLv = LV2;
m_currentLVSpeed = LV2_SPEED;
ui->label_gameLevel->setText("第二关");
m_pixmap.load(":/background/Image/background/background02.png");
break;
case LV3:
m_currentLv = LV3;
m_currentLVSpeed = LV3_SPEED;
ui->label_gameLevel->setText("第三关");
m_pixmap.load(":/background/Image/background/background03.png");
break;
case LV4:
m_currentLv = LV4;
m_currentLVSpeed = LV4_SPEED;
ui->label_gameLevel->setText("第四关");
m_pixmap.load(":/background/Image/background/background04.png");
break;
case LV5:
m_currentLv = LV5;
m_currentLVSpeed = LV5_SPEED;
ui->label_gameLevel->setText("第五关");
m_pixmap.load(":/background/Image/background/background05.png");
break;
default:
break;
}
m_Palette.setBrush(QPalette::Window, QBrush(m_pixmap.scaled(size(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation)));
setPalette(m_Palette);
}
void Panel::initControlWidget()
{
switch (m_currentLv) {
case LV0:
ui->pbt_startGame->setEnabled(true);
ui->pbt_pauseGame->setEnabled(false);
ui->pbt_restart->setEnabled(false);
ui->pbt_stopGame->setEnabled(false);
break;
case LV1:
case LV2:
case LV3:
case LV4:
case LV5:
ui->pbt_startGame->setEnabled(false);
ui->pbt_pauseGame->setEnabled(true);
ui->pbt_restart->setEnabled(true);
ui->pbt_stopGame->setEnabled(true);
break;
default:
break;
}
}
void Panel::initGraphicsViewWidget()
{
//使用抗锯齿渲染
ui->GraphicsView->setRenderHint(QPainter::Antialiasing);
//设置缓冲背景,加速渲染
ui->GraphicsView->setCacheMode(QGraphicsView::CacheBackground);
m_scene = new QGraphicsScene(this);
m_scene->setSceneRect(30,30,310,410);
ui->GraphicsView->setScene(m_scene);
//方块可移动的四条线
//向外扩展3像素,这样可以使方块组到达边界的时候再移动就会发生碰撞
m_topLine = m_scene->addLine(32,32,238,32);
m_leftLine = m_scene->addLine(32,32,32,438);
m_buttomLine = m_scene->addLine(32,438,238,438);
m_rightLine = m_scene->addLine(238,32,238,438);
m_topLine->setPen(QPen(QColor(255,255,255)));
m_leftLine->setPen(QPen(QColor(255,255,255)));
m_buttomLine->setPen(QPen(QColor(255,255,255)));
m_rightLine->setPen(QPen(QColor(255,255,255)));
m_currentBox = new pieceBox;
m_nextBox = new pieceBox;
connect(m_currentBox,SIGNAL(signal_needNewBox()),this,SLOT(slot_clearFullRows()));
connect(m_currentBox,SIGNAL(signal_gameOver()),this,SLOT(slot_gameOver()));
m_scene->addItem(m_currentBox);
m_scene->addItem(m_nextBox);
}
void Panel::startGame()
{
m_currentBox->createBox(QPointF(135,55));
m_nextBox->createBox(QPointF(290,55));
//将键盘焦点给当前的方块组
m_currentBox->setFocus();
m_currentBox->slot_startTimer(m_currentLVSpeed);
}
void Panel::updateScore(const int fullRows)
{
int score = ui->LCDNum_Score->value() + fullRows*ROWSCORE;
ui->LCDNum_Score->display(score);
if(score < 1000) //第一关
{
}else if(score < 2000) //第二关
{
setbackground(LV2);
initControlWidget();
m_currentBox->slot_stopTimer();
m_currentBox->slot_startTimer(m_currentLVSpeed);
}else if(score < 4000) //第三关
{
setbackground(LV3);
initControlWidget();
m_currentBox->slot_stopTimer();
m_currentBox->slot_startTimer(m_currentLVSpeed);
}else if(score < 8000) //第四关
{
setbackground(LV4);
initControlWidget();
m_currentBox->slot_stopTimer();
m_currentBox->slot_startTimer(m_currentLVSpeed);
}else if(score < 16000) //第五关
{
setbackground(LV5);
initControlWidget();
m_currentBox->slot_stopTimer();
m_currentBox->slot_startTimer(m_currentLVSpeed);
}else //从第一关重新开始
{
setbackground(LV1);
initControlWidget();
m_currentBox->slot_stopTimer();
m_currentBox->slot_startTimer(m_currentLVSpeed);
}
}
/**
* 开始游戏
* @brief Panel::on_pbt_startGame_clicked
*/
void Panel::on_pbt_startGame_clicked()
{
m_isGameOver = false;
if(m_isPauseGame)
{
ui->pbt_startGame->setEnabled(false);
m_isPauseGame = false;
m_currentBox->slot_startTimer(m_currentLVSpeed);
return;
}
//默认等级为LV1
setbackground(LV1);
initControlWidget();
startGame();
}
/**
* 暂停游戏
* @brief Panel::on_pbt_pauseGame_clicked
*/
void Panel::on_pbt_pauseGame_clicked()
{
if(m_isPauseGame)
{
return;
}
m_isPauseGame = true;
m_currentBox->slot_stopTimer();
ui->pbt_startGame->setEnabled(true);
QMessageBox::information(this,"提示","暂停游戏!",QMessageBox::Yes);
}
/**
* 重新开始游戏
* @brief Panel::on_pbt_restart_clicked
*/
void Panel::on_pbt_restart_clicked()
{
m_currentBox->slot_stopTimer();
m_currentBox->clearBoxGroup();
m_nextBox->clearBoxGroup(true);
//先将当前的小正方形组移出游戏框,防止下面的清除item将该方块组清除了
m_currentBox->setPos(290,55);
ui->LCDNum_Score->display(0);
//清空视图中所有的小方块
foreach (QGraphicsItem *item, m_scene->items(34, 34, 204, 404, Qt::ContainsItemShape,Qt::AscendingOrder)) {
// 先从场景中移除小方块,因为使用deleteLater()是在返回主事件循环后才销毁
// 小方块的,为了在出现新的方块组时不发生碰撞,所以需要先从场景中移除小方块
m_scene->removeItem(item);
onePiece *piece = (onePiece*) item;
piece->deleteLater();
}
m_isPauseGame = false;
on_pbt_startGame_clicked();
}
/**
* 停止游戏
* @brief Panel::on_pbt_stopGame_clicked
*/
void Panel::on_pbt_stopGame_clicked()
{
m_currentBox->slot_stopTimer();
slot_gameOver();
}
/**清除满行的小方块
* @brief Panel::slot_clearFullRows
*/
void Panel::slot_clearFullRows()
{
m_rowList.clear();
//获取比一行方格多行的所有小方块,不包含最高的一行
for(int i = 414;i > 35; i-=20)
{
//返回可视区域(202*22)内所有的完全可视的item
QList<QGraphicsItem *> itemList = m_scene->items(34,i,204,22,Qt::ContainsItemShape,Qt::A
基于Qt5图形视图框架的俄罗斯方块
5星 · 超过95%的资源 需积分: 50 83 浏览量
2016-01-29
14:53:20
上传
评论 6
收藏 4.4MB ZIP 举报
sxpsxp12
- 粉丝: 90
- 资源: 18
最新资源
- 基于spark和kafka的新闻案例+源代码+文档说明
- greenplum-cc-web-6.2.0-gp6-rhel7-x86-64.7z
- 电机控制霍尔传感器和反电动势的关系分析
- 计算机生产实习:OA后台管理(web前端+Java后端)压缩文件包
- greenplum6.10安装时缺失的fpm包
- 缺陷检测-轻量化PCB表面缺陷检测算法实现-工业视觉+六大缺陷检出-优质项目实战.zip
- JAVA 中的Spring框架介绍包括起源、体系结构、核心部分、特点等
- 2024年小米汽车产业链分析及新品上市全景洞察报告
- 基于Qt和C++实现的偏3D风格的异型窗体界面操作+源码(期末大作业&课设&项目开发)
- 基于yolov8的花卉分类系统,包含训练好的权重和推理代码,GUI界面,支持图片、视频、摄像头输入,支持检测结果导出
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
- 1
- 2
前往页