#include "blockpanel.h"
#include "ui_blockpanel.h"
#include<QPaintEvent>
#include<QKeyEvent>
#include<QPen>
#include<QIcon>
#include<QBrush>
using namespace std;
BlockPanel::BlockPanel(QWidget *parent)
: QWidget(parent), ui(new Ui::BlockPanel)
{
ui->setupUi(this);
QIcon icon(QString(":/icon/teterix+.png"));
this->setWindowIcon(icon);
this->isStart=false;
this->havePlay=false;
this->isLive=true;
this->score=0;
this->lastY=BlockPanelLogicHeight - 1; //从最下面一行开始搜索满行的行
this->setWindowFlags(Qt::Dialog);
this->setWindowTitle(QString("Teterix+"));
for(int i=0;i!=BlockPanelLogicHeight;i++){
for(int j=0;j!=BlockPanelLogicWidth;j++){
this->allPieces[i][j][0]=0;
this->allPieces[i][j][1]=0;
}
}
this->setFocusPolicy(Qt::StrongFocus); //设置窗体接受键盘事件
QObject::connect(this->ui->startBtn,SIGNAL(clicked()),this,SLOT(startOrPause()));
QObject::connect(&(this->timer),SIGNAL(timeout()),this,SLOT(go()));
}
void BlockPanel::go(){
if(this->tryMove(Normal)){
this->curBlock.move(Normal);
}else{
int max=-1;
for(int i=0;i!=4;i++){
if(this->logicPos[i][1] > max)
max=this->logicPos[i][1];
}
this->lastY=max;
this->clearFullLine();
this->curBlock.setRandomShape();
}
this->update();
}
BlockPanel::~BlockPanel()
{
delete ui;
}
void BlockPanel::startOrPause(){
if(!this->isStart){
if(!this->havePlay){ //第一次按Start才要生成一个Block
this->curBlock.setRandomShape();
this->score=0;
this->ui->scoreLabel->setText(QString("%1").arg(this->score));
this->update();
}
this->timer.start(TimerStep);
this->isStart=true;
this->havePlay=true; //设置已经开始玩过了,即已经按过Start按钮了
this->ui->startBtn->setText(QString("Pause"));
this->ui->label->setText(QString("Welcome"));
}else{
this->timer.stop();
this->isStart=false;
this->update();
this->ui->startBtn->setText(QString("Start"));
}
}
void BlockPanel::keyPressEvent(QKeyEvent* e){
if(!this->isStart){
return ;
}
if(e->key() == Qt::Key_Left){
if(this->tryMove(Left)){
this->curBlock.move(Left);
this->update();
}
}else if(e->key() == Qt::Key_Right){
if(this->tryMove(Right)){
this->curBlock.move(Right);
this->update();
}
}else if(e->key() == Qt::Key_Up){
if(this->tryRoate()){
this->curBlock.roate();
this->update();
}
}else if(e->key() == Qt::Key_Down){
this->go();
this->go();
this->update();
this->go();
this->go();
this->update();
this->go();
this->go();
this->update();
this->go();
this->go();
this->update();
}
}
void BlockPanel::store(){ //用于保存不能下落的方块
for(int i=0;i!=4;i++){
int x = this->logicPos[i][0];
int y = this->logicPos[i][1];
this->allPieces[ y ]
[ x ]
[ 0 ] = 1; //把对应的位置设置为1,即有块占领
this->allPieces[ y ]
[ x ]
[ 1 ]=this->curBlock.curBaseShape; //设置对应的颜色
}
this->checkLive();
}
bool BlockPanel::tryMove(Direction dir){ //判断当前Block能否向dir方向运动,能则返回true,不能则返回false
bool can=true;
this->setLogicPos(); //设置当前Block的逻辑坐标
if(dir == Normal){
int maxY=-5;
int l=0;
for(int i=0;i!=4;i++){
l=this->logicPos[i][1];
if(l>maxY)
maxY=l;
}
if(maxY == BlockPanelLogicHeight - 1){
can=false;
this->store();
}else{
for(int i=0;i!=4;i++){
if( this->logicPos[i][1] >=0 && //y轴逻辑坐标不小于0才开始判断
this->allPieces[ this->logicPos[i][1] +1]
[ this->logicPos[i][0] ]
[0]==1 )
{
can=false;
this->store();
break;
}
}
}
}else if(dir == Left){
for(int i=0;i!=4;i++){
if( this->allPieces[ this->logicPos[i][1] ]
[ this->logicPos[i][0] - 1 ]
[0] == 1
|| this->logicPos[i][0] == 0
){
can=false;
break;
}
}
}else if(dir == Right){
for(int i=0;i!=4;i++){
if( this->allPieces[ this->logicPos[i][1] ]
[ this->logicPos[i][0] + 1 ]
[0] == 1
|| this->logicPos[i][0] == 13
){
can=false;
break;
}
}
}else if(dir == Up){
if(this->tryRoate()){
this->curBlock.roate();
this->update();
}
}
return can;
}
bool BlockPanel::tryRoate(){ //判断当前Block能否顺时针转动,能则返回true
bool r=true;
this->setCheckRoate(); //把检验用的Block的相关属性设置为curBlock一样
this->checkRoate.curShape=this->curBlock.curShape;
this->checkRoate.roate();
this->setCheckLogicPos();
for(int i=0;i!=4;i++){
if(this->checkLogicPos[i][0] < 0 ||
this->checkLogicPos[i][0] >13 ||
this->checkLogicPos[i][1] >19 ){
r=false;
return r;
}
if(this->allPieces[ this->checkLogicPos[i][1] ]
[ this->checkLogicPos[i][0] ]
[0] == 1 ){
r=false;
break;
}
}
return r;
}
void BlockPanel::paintEvent(QPaintEvent* e){
QPainter p(this);
QPen bkPen=p.pen();
QColor redC(Qt::red);
QPen pen(redC);
p.setPen(pen);
p.drawRect(BlockPanelOriginX,BlockPanelOriginY,
BlockPanelWidth,BlockPanelHeight);
p.setPen(bkPen);
if(this->havePlay){
QPen bkPen=p.pen();
QPen newPen;
QColor newColor;
QBrush bsh;
for(int i=0;i!=BlockPanelLogicHeight;i++){
for(int j=0;j!=BlockPanelLogicWidth;j++){
if(this->allPieces[i][j][0] == 1){
//为不同的块设置颜色
newColor.setRgb(colors[this->allPieces[i][j][1]][0],
colors[this->allPieces[i][j][1]][1],
colors[this->allPieces[i][j][1]][2]);
newPen.setColor(newColor);
bsh.setColor(newColor);
bsh.setStyle((Qt::BrushStyle)BrushStyle);
p.setBrush(bsh);
p.setPen(newPen);
int y = i * BlockPieceHeight + BlockPanelOriginY;
int x = j * BlockPieceWidth;
p.drawRect( x + BlockPanelOriginX,y,BlockPieceWidth,BlockPieceHeight);
}
}
}
this->curBlock.draw(p);
}
}
void BlockPanel::setLogicPos(){
for(int i=0;i!=4;i++){
int lx=(this->curBlock.pieces[i][0] * BlockPieceWidth + this->curBlock.x -BlockPanelOriginX)
/ BlockPieceWidth ;
int ly=(this->curBlock.pieces[i][1] * BlockPieceHeight + this->curBlock.y -BlockPanelOriginY)
/ BlockPieceHeight ;
this->logicPos[i][0]=lx;
this->logicPos[i][1]=ly;
}
}
void Bl
没有合适的资源?快使用搜索试试~ 我知道了~
Teterix+ 源码 就是之前我发的那个qt写的东西的源码
共17个文件
h:4个
cpp:3个
pro:1个
4星 · 超过85%的资源 需积分: 3 79 下载量 15 浏览量
2010-03-15
11:55:58
上传
评论 1
收藏 42KB RAR 举报
温馨提示
Teterix+ 源码 就是之前我发的那个qt写的东西的源码 应坛友的要求 现在发上来
资源推荐
资源详情
资源评论
收起资源包目录
Teterix+ source.rar (17个子文件)
Teterix+
block.cpp 3KB
Makefile 6KB
global.h 2KB
block.h 466B
ui_blockpanel.h 3KB
icons.qrc 96B
Makefile.Debug 7KB
icon.rc 33B
Teterix+.pro 443B
blockpanel.h 1KB
Teterix+.pro.user 8KB
blockpanel.cpp 10KB
blockpanel.ui 2KB
teterix+.ico 133KB
main.cpp 229B
Makefile.Release 7KB
teterix+.png 9KB
共 17 条
- 1
资源评论
- Sphinx2012-03-16原来QT自带的示例程序中有俄罗斯方块,不过还是很感谢
pywepe
- 粉丝: 71
- 资源: 14
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功