Qt开发源码俄罗斯方块Word文档下载推荐.docx
- 文档编号:18565293
- 上传时间:2022-12-28
- 格式:DOCX
- 页数:22
- 大小:22.49KB
Qt开发源码俄罗斯方块Word文档下载推荐.docx
《Qt开发源码俄罗斯方块Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《Qt开发源码俄罗斯方块Word文档下载推荐.docx(22页珍藏版)》请在冰豆网上搜索。
QFrame>
QSound>
//前向声明
classQLabel;
classTetrixBoard:
publicQFrame
Q_OBJECT
public:
TetrixBoard(QWidget*parent=0);
voidsetNextPieceLabel(QLabel*label);
QSizesizeHint()const;
//最适合大小
QSizeminimumSizeHint()const;
//最小限制
publicslots:
//公有槽
voidstart();
voidpause();
signals:
//信号:
只需声明,根据参数变化来判断
voidscoreChanged(intscore);
voidlevelChanged(intlevel);
voidlinesRemovedChanged(intnumLines);
protected:
//着色、键盘、计时事件:
其中着色事件随着update()不断触发
voidpaintEvent(QPaintEvent*event);
voidkeyPressEvent(QKeyEvent*event);
voidtimerEvent(QTimerEvent*event);
private:
enum{BoardWidth=10,BoardHeight=22};
//把主游戏区宽分成10等份,高分成22等份,也就是说每行有10小矩形,总共有22行
TetrixShape&
shapeAt(intx,inty){returnboard[(y*BoardWidth)+x];
}
inttimeoutTime(){return1000/(1+level);
//contentsRect():
返回当前布局(QLayout)的矩形,可访问其长、宽(详见API)
//conntentsRect().width()/BoardWidth把游戏区矩形的宽分成了BoardWidth份
intsquareWidth(){returncontentsRect().width()/BoardWidth;
//同上,把高分成了BoardHeight份
intsquareHeight(){returncontentsRect().height()/BoardHeight;
//此时squareWidth(),squareHeight()分别是分割后的小矩形宽和高
voidclearBoard();
//清屏
voiddropDown();
//下落事件
voidoneLineDown();
//下落一行
voidpieceDropped(intdropHeight);
voidremoveFullLines();
//移除填满的行
voidnewPiece();
//新方块
voidshowNextPiece();
//显示下一个方块
booltryMove(constTetrixPiece&
newPiece,intnewX,intnewY);
//判断方块是否可以移动
voiddrawSquare(QPainter&
painter,intx,inty,TetrixShapeshape);
//着色
QBasicTimertimer;
//相当于QLabel*nextPieceLabel(QPointer详见API)
QPointer<
QLabel>
nextPieceLabel;
boolisStarted;
boolisPaused;
boolisWaitingAfterLine;
TetrixPiececurPiece;
//当前方块
TetrixPiecenextPiece;
//下一个方块
intcurX;
intcurY;
intnumLinesRemoved;
intnumPiecesDropped;
intscore;
intlevel;
TetrixShapeboard[BoardWidth*BoardHeight];
};
#endif//TETRIXBOARD_H
Tetrixboard.cpp程序代码:
tetrixboard.h"
TetrixBoard:
TetrixBoard(QWidget*parent)
:
QFrame(parent)
//设置游戏区框架风格:
内浮雕
setFrameStyle(QFrame:
Panel|QFrame:
Sunken);
//增加游戏区键盘鼠标等事件的焦点集中
setFocusPolicy(Qt:
StrongFocus);
isStarted=false;
//初始化:
未开始状态
isPaused=false;
clearBoard();
//初始清屏
nextPiece.setRandomShape();
//下一方块获得一个随机形状
//tetrixpiece.h:
tetrixpiece.cpp中使用
voidTetrixBoard:
setNextPieceLabel(QLabel*label)
nextPieceLabel=label;
//游戏区合适大小
QSizeTetrixBoard:
sizeHint()const
returnQSize(BoardWidth*15+frameWidth()*2,
BoardHeight*15+frameWidth()*2);
//游戏区最小大小
minimumSizeHint()const
returnQSize(BoardWidth*5+frameWidth()*2,
BoardHeight*5+frameWidth()*2);
//开始事件:
slots
start()
//如果已暂停,则启动无效
if(isPaused)
return;
isStarted=true;
//标记已开始
isWaitingAfterLine=false;
//此参数为判断是否有方块正在下落,false为有方块正在下落中
//初始各参数
numLinesRemoved=0;
numPiecesDropped=0;
score=0;
level=1;
//emit信号发射:
触发对应信号槽内的函数(相关connect()在tetrixwindow.cpp中)
emitlinesRemovedChanged(numLinesRemoved);
emitscoreChanged(score);
emitlevelChanged(level);
newPiece();
//调用新方块
timer.start(timeoutTime(),this);
//游戏开始计时
//暂停事件:
pause()
//如果未开始,则暂停无效
if(!
isStarted)
//否则,若未暂停,则赋值为暂停,反之,取消暂停,继续游戏
isPaused=!
isPaused;
if(isPaused){
timer.stop();
//游戏计时停止
}else{
//否则继续计时
update();
//刷新窗口:
动态显示画面
//游戏区方块着色
//重定义绘图事件,当调用update()时进行重绘
paintEvent(QPaintEvent*event)
QFrame:
paintEvent(event);
QPainterpainter(this);
QRectrect=contentsRect();
//QRect定义了平面上的矩形(详见API),是主游戏区
//暂停的时候显示的信息
painter.drawText(rect,Qt:
AlignCenter,tr("
游戏暂停"
//BoardHeight*squareHeight()相当于contentsRect().Height(),是小网格的高
//因为squareHeight(){returncontentsRect().Width()/BoardWidth();
//见tetrixboard.h中的定义
intboardTop=rect.bottom()-BoardHeight*squareHeight();
for(inti=0;
i<
BoardHeight;
++i){
for(intj=0;
j<
BoardWidth;
++j){
//TetrixShape&
TetrixShapeshape=shapeAt(j,BoardHeight-i-1);
if(shape!
=NoShape)
//rect.left()返回游戏区矩形左边的x坐标,squareWidth()为小网格的宽度
drawSquare(painter,rect.left()+j*squareWidth(),
boardTop+i*squareHeight(),shape);
//绘图
if(curPiece.shape()!
=NoShape){
4;
intx=curX+curPiece.x(i);
inty=curY-curPiece.y(i);
drawSquare(painter,rect.left()+x*squareWidth(),
boardTop+(BoardHeight-y-1)*squareHeight(),
curPiece.shape());
//键盘事件
keyPressEvent(QKeyEvent*event)
isStarted||isPaused||curPiece.shape()==NoShape){
keyPressEvent(event);
switch(event->
key()){
caseQt:
Key_Left:
tryMove(curPiece,curX-1,curY);
//左移
break;
Key_Right:
tryMove(curPiece,curX+1,curY);
//右移
Key_Up:
tryMove(curPiece.rotatedLeft(),curX,curY);
//方块左转
Key_Down:
dropDown();
//快速下落
default:
//计时时间
timerEvent(QTimerEvent*event)
if(event->
timerId()==timer.timerId()){
//如果还有方块已下落完毕
if(isWaitingAfterLine){
//重标记为有方块正在下落
//添加新方块
(timeoutTime(),this);
oneLineDown();
//否则进行下落动作
timerEvent(event);
//清空游戏区所有绘图
clearBoard()
BoardHeight*BoardWidth;
++i)
board[i]=NoShape;
//直接快速下落操作
dropDown()
intdropHeight=0;
intnewY=curY;
//进行下落过程,并求得方块还能下落的最大高度
while(newY>
0){
tryMove(curPiece,curX,newY-1))
--newY;
++dropHeight;
//把下落高度传递给此函数
pieceDropped(dropHeight);
//正常下落操作
oneLineDown()
tryMove(curPiece,curX,curY-1))//如果能移动,则下落一行
pieceDropped(0);
//正常下落不几分
//进行方块下落后的行为,如绘图,加分等参数:
下落方块的高度
pieceDropped(intdropHeight)
shapeAt(x,y)=curPiece.shape();
++numPiecesDropped;
//等级划分,加快下落速度
if(numPiecesDropped%25==0){
++level;
//加速,游戏时间加快
//判断是否已有满行
removeFullLines();
isWaitingAfterLine)
//移除整行
removeFullLines()
intnumFullLines=0;
//循环判断有几行已满
for(inti=BoardHeight-1;
i>
=0;
--i){
boollineIsFull=true;
//判断是否已满一行
j<
BoardWidth;
if(shapeAt(j,i)==NoShape){
lineIsFull=false;
//上面所有行下移
if(lineIsFull){
++numFullLines;
for(intk=i;
k<
BoardHeight-1;
++k){
for(intj=0;
++j)
shapeAt(j,k)=shapeAt(j,k+1);
//整行清零
{
shapeAt(j,BoardHeight-1)=NoShape;
score+=numFullLines-1;
//如果已满行数大于0,则进行加分等操作,并更新窗口
if(numFullLines>
numLinesRemoved+=numFullLines;
score+=10*numFullLines;
//同时发送信号至相应的槽
(500,this);
isWaitingAfterLine=true;
curPiece.setShape(NoShape);
//新方块
newPiece()
curPiece=nextPiece;
//预先随机设置好一下块方块
showNextPiece();
//设置其初始下落的位置,在游戏区顶部中央
curX=BoardWidth/2+1;
curY=BoardHeight-1+curPiece.minY();
//判断其是否还能移动,如果不能,则停止游戏
tryMove(curPiece,curX,curY)){
//painter.drawText(rect,tr("
游戏结束"
//展示下一个方块
showNextPiece()
nextPieceLabel)
intdx=nextPiece.maxX()-nextPiece.minX()+1;
intdy=nextPiece.maxY()-nextPiece.minY()+1;
QPixmappixmap(dx*squareWidth(),dy*squareHeight());
//映射要显示方块像素
QPainterpainter(&
pixmap);
//开始绘制该方块
painter.fillRect(pixmap.rect(),nextPieceLabel->
palette().background());
//先绘制要显示方块的背景色
//再开始绘制方块本身
for(inti=0;
i<
4;
intx=nextPiece.x(i)-nextPiece.minX();
inty=nextPiece.y(i)-nextPiece.minY();
drawSquare(painter,x*squareWidth(),y*squareHeight(),
nextPiece.shape());
nextPieceLabel->
setPixmap(pixmap);
//最后加载它
//判断是否还能移动
boolTetrixBoard:
tryMove(constTetrixPiece&
newPiece,intnewX,intnewY)
intx=newX+newPiece.x(i);
inty=newY-newPiece.y(i);
if(x<
0||x>
=BoardWidth||y<
0||y>
=BoardHeight)
returnfalse;
if(shapeAt(x,y)!
=NoShape)//判断当前位置是否有其他方块
curPiece=newPiece;
curX=newX;
curY=newY;
returntrue;
//intsquareWidth(){returncontentsRect().width()/BoardWidth;
//intsquareHeight(){returncontentsRect().height()/BoardHeight;
drawSquare(QPainter&
painter,intx,inty,TetrixShapeshape)
//google色彩
staticconstQRgbcolorTable[8]={
0x000000,0x1851ce,0xc61800,0xefb
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Qt 开发 源码 俄罗斯方块