#include "gameview.h"
#include "ui_gameview.h"
GameView::GameView(QWidget *parent) :
QWidget(parent),
ui(new Ui::GameView)
{
ui->setupUi(this);
setFixedSize(320,480);
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 ++)//i 是行号
{
int idex = generateRandomNumber();
for(int j = 0 ; j < 4 ; j ++)//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)
{
//qDebug()<<event->x()<<event->y();
//根据坐标点 从队列获取块
int row = event->y()/120;
int col = event->x()/80;
//qDebug()<<event->x()<<event->y()<<(row*4 + col);
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);
//删除最底下4个
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("小伙子很棒,得分 %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;
}
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
项目展示:https://blog.csdn.net/G1842965496/article/details/119701111 一、功能实现 完善的游戏界面、游戏倒计时、得分记录、历史最高分显示 二、功能描述 1、界面为4*4,一行中只有一个黑块,使用qrand函数,采用时间种子保证 每次产生是随机数不同,在将随机数对4取余来作为黑块的位置。 2、初始时间设定值为30,通过定时器每100ms发出一次信号,刷新时间。 3、通过工厂模式,完成对黑块和白块的生产,并采用queue容器来储存块。 4、当玩家点击黑块时,delete队头的4个块并pop弹出,在加入4个新块, 最后将队列中所有的块Y坐标增加。
资源推荐
资源详情
资源评论
收起资源包目录
baikuaiGame完整版.zip (52个子文件)
baikuaiGame
baikuaiGame.pro.user 23KB
startwidget.ui 4KB
startwidget.cpp 2KB
startwidget.h 653B
gameview.cpp 4KB
mybutton.h 360B
main.cpp 185B
baikuaiGame.pro 1KB
gameview.ui 2KB
block.h 1KB
gameview.h 927B
images.qrc 2KB
mybutton.cpp 841B
resources
mall_close2.png 3KB
message_b_over1.png 3KB
message_b_chal2.png 3KB
pause_b02.png 5KB
pause_b31.png 5KB
pause_b12.png 5KB
running_bg.png 51KB
cover_b1.png 5KB
menu_Text.png 215B
message_b_chal4.png 3KB
message_b_chal1.png 3KB
cover_b0.png 6KB
b_resume.png 4KB
challenge_time_bg.png 2KB
pause_b01.png 6KB
message_b_no2.png 3KB
b_paused.png 3KB
lottery_b_1.png 4KB
message_b_over2.png 3KB
message_b_back1.png 3KB
message_b_chal3.png 3KB
pause_b42.png 5KB
pause_b32.png 4KB
img_bg_level_1.jpg 388KB
img_bg_level_2.jpg 190KB
message_b_back2.png 3KB
mall_buy2.png 2KB
mall_buy1.png 2KB
mall_close1.png 3KB
message_b_no1.png 3KB
pause_b21.png 5KB
cover_b2.png 5KB
pause_b22.png 4KB
pause_b11.png 6KB
challenge_time_line.png 916B
pause_b41.png 6KB
block.cpp 375B
gamesetting.cpp 64B
gamesetting.h 441B
共 52 条
- 1
@小高同学
- 粉丝: 239
- 资源: 7
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
- 4
前往页