QT方块游戏源代码.docx
《QT方块游戏源代码.docx》由会员分享,可在线阅读,更多相关《QT方块游戏源代码.docx(27页珍藏版)》请在冰豆网上搜索。
QT方块游戏源代码
Gamearea.cpp
/**********************版权声明****************************
所有源代码由原创,我们遵循Qt开源版的所有相关条款和协议。
您有权使用,传播和更改我们的代码,但请保证代码的开源。
以下源代码版权归所有,请不要用作商业用途。
**********************************************************/
//这个类中实现了游戏的所有功能
#include"gamearea.h"
#include
#include
#include
/****************************以下是构造函数和析构函数*********************************************************/
GameArea:
:
GameArea(QWidget*parent):
QFrame(parent)
{
this->init_gameArea(6,6,430,430,200,400,20,60,0);
this->init_Game();
}
GameArea:
:
GameArea(intspeed,QWidget*parent):
QFrame(parent)
{
this->init_gameArea(6,6,430,430,200,400,20,60,0);
this->init_Game();
this->moveTimer=newQTimer(this);
connect(this->moveTimer,SIGNAL(timeout()),this,SLOT(moveTimer_upDate()));
this->moveSpeed=speed;
this->gameStart();
this->moveTimer->start(moveSpeed);
}
GameArea:
:
~GameArea()
{
}
/****************************以下是主要功能函数*********************************************************/
//方块下移一步
voidGameArea:
:
moveOneStep()
{
startY+=step;
}
//显示下移一步后的界面
voidGameArea:
:
do_MoveNext()
{
this->currentItem_to_currentMap();
this->draw_gameArea();
}
voidGameArea:
:
nextItem()
{//先将现在的游戏区域备份再消行
copy_Map(currentMap,copyMap,map_row,map_col);
this->clearRow();
}
//游戏是否已经结束
boolGameArea:
:
isGame_Over()
{
if(this->isGameOver)returntrue;
elsereturnfalse;
}
//获取已满的行数
intGameArea:
:
getFullRowNum()
{
returnfullRowNum;
}
//设置方块颜色或贴图
voidGameArea:
:
setGameAreaColor(QColorcolor)
{
gameAreaColor=color;
}
voidGameArea:
:
setBoxBrushColor(QColorcolor)
{
boxBrushColor=color;
}
voidGameArea:
:
setBoxPenColor(QColorcolor)
{
boxPenColor=color;
}
voidGameArea:
:
set_draw_box_picture(boolBool)
{
this->is_draw_box_picture=Bool;
}
voidGameArea:
:
setBoxPicture(QStringfileName)
{
this->boxPicture=fileName;
}
//设置方块颜色或贴图
//是否下移一步,如果不是则一次下移到底
voidGameArea:
:
setKey_Down_Move_oneStep(boolBool)
{
this->isKey_Down_Move_OneStep=Bool;
}
//是否显示网格
voidGameArea:
:
setDrawGrid(boolBool)
{
isDrawGrid=Bool;
}
//是否显示下一个要出现的图形
voidGameArea:
:
setDrawNextItem(boolBool)
{
isDrawNextItem=Bool;
}
//自己往游戏区添加方块
voidGameArea:
:
setbox(introw,intcol)
{
*(copyMap+row*map_col+col)=1;
}
//是否播放声音
voidGameArea:
:
setPlaySound_moveLeft(QStringfileName,boolBool)
{
this->isPlaySound_moveLeft=Bool;
this->sound_moveLeft=fileName;
}
voidGameArea:
:
setPlaySound_moveRight(QStringfileName,boolBool)
{
this->isPlaySound_moveRight=Bool;
this->sound_moveRight=fileName;
}
voidGameArea:
:
setPlaySound_moveDown(QStringfileName,boolBool)
{
this->isPlaySound_moveDown=Bool;
this->sound_moveDown=fileName;
}
voidGameArea:
:
setPlaySound_itemChange(QStringfileName,boolBool)
{
this->isPlaySound_itemChange=Bool;
this->sound_itemChange=fileName;
}
voidGameArea:
:
setPlaySound(boolBool)
{
this->isPlaySound_moveLeft=Bool;
this->isPlaySound_moveRight=Bool;
this->isPlaySound_moveDown=Bool;
this->isPlaySound_itemChange=Bool;
}
//是否播放声音
/***设置游戏区域旋转****/
voidGameArea:
:
setRotate(boolBool)//开启旋转
{
this->isRotate=Bool;
}
voidGameArea:
:
setRotateAngle(intangle)//顺时针旋转角度
{
this->theAngle=angle;
}
voidGameArea:
:
setGameAreaPixOrigin(intx,inty)//新的坐标原点
{
this->gameArea_X=x;
this->gameArea_Y=y;
}
voidGameArea:
:
setGameAreaPix(intx,inty)//游戏区域的位置
{
this->pix_X=x;
this->pix_Y=y;
}
/*****设置旋转******/
/****************************以上是主要功能函数*********************************************************/
/****************************以下是核心功能函数*********************************************************/
voidGameArea:
:
init_gameArea(intX,intY,intframe_width,intframe_height,intwidth,intheight,intboxStep,intstart_x,intstart_y)
//初始化游戏区域,起始位置,宽,高,小方块边长,图形出现位置
{
this->gameArea_width=width;
this->gameArea_height=height;
this->step=boxStep;
this->init_startX=start_x;
this->init_startY=start_y;
this->map_row=gameArea_height/step;
this->map_col=gameArea_width/step;
this->resize(frame_width,frame_height);
//将游戏区域设置为较大的正方形
this->move(X,Y);
pix_gameArea=newQPixmap(this->gameArea_width,this->gameArea_height);
this->isKey_Down_Move_OneStep=false;
//默认状态设置
this->isDrawGrid=true;
this->isDrawNextItem=true;
this->isPlaySound_moveLeft=false;
this->isPlaySound_moveRight=false;
this->isPlaySound_moveDown=false;
this->isPlaySound_itemChange=false;
this->is_draw_box_picture=false;
//默认颜色设置
this->gameAreaColor=Qt:
:
white;
this->boxBrushColor=Qt:
:
green;
this->boxPenColor=Qt:
:
black;
this->isRotate=false;
this->theAngle=0;
this->gameArea_X=0;
this->gameArea_Y=0;
this->pix_X=10;
this->pix_Y=10;
myItem=newMyItem();
this->currentMap=newunsignedchar[map_row*map_col];
this->copyMap=newunsignedchar[map_row*map_col];
}
voidGameArea:
:
init_Game()//第一次进入游戏时对一些变量进行初始化
{
this->init_Map(currentMap,map_row,map_col);
this->init_Map(copyMap,map_row,map_col);
this->currentItem=this->currentMap;//对两个指针进行初始化
this->theNextItem=this->currentMap;//让两个指针均指向清空的数组
isFirstItem=true;
isGameOver=false;
fullRowNum=0;
this->draw_gameArea();
}
voidGameArea:
:
gameStart()//游戏开始运行,而且每次出现新的方块都调用一次这个函数
{
this->startX=this->init_startX;
this->startY=this->init_startY;
fullRowNum=0;//每次出现一个新的图形都将上次满的行数清0
if(isFirstItem)
{
this->currentItem=myItem->getItem();
isFirstItem=false;
}
elsethis->currentItem=this->theNextItem;
this->theNextItem=myItem->getItem();
this->currentItem_to_currentMap();
this->draw_gameArea();
}
voidGameArea:
:
init_Map(unsignedchar*initMap,introw,intcol)
//用于让一个数组中的数全部为0
{
for(inti=0;i{
for(intj=0;j
{
*(initMap+i*col+j)=0;
}
}
}
voidGameArea:
:
draw_gameArea()//绘制游戏区域,包括网格和游戏区的方块
{
this->pix_gameArea->fill(gameAreaColor);
if(this->isDrawGrid)
{
draw_Grid();
}
draw_currentMap();
update();
}
voidGameArea:
:
draw_Grid()//绘制游戏区域背景的网格
{
QPainterpainter(this->pix_gameArea);
painter.setPen(Qt:
:
DotLine);
for(inti=0;i{
painter.drawLine(QPoint(0,i*step),QPoint(this->gameArea_width,i*step));
}
for(intj=0;j{
painter.drawLine(QPoint(j*step,0),QPoint(j*step,this->gameArea_height));
}
}
voidGameArea:
:
draw_currentMap()
//绘制游戏区域现在已有的方块
{
QPainterpainter(this->pix_gameArea);
painter.setPen(this->boxPenColor);
painter.setBrush(this->boxBrushColor);
for(inti=0;i{
for(intj=0;j{
if(*(currentMap+i*map_col+j)==1)
{
if(this->is_draw_box_picture)
{
QPixmappix;
pix.load(this->boxPicture);
painter.drawPixmap(j*step,i*step,step,step,pix);
}
painter.drawRect(j*step,i*step,step,step);
}
}
}
}
voidGameArea:
:
currentItem_to_currentMap()
//将当前图形加入到游戏区域网格上
{
copy_Map(copyMap,currentMap,map_row,map_col);//使用备份的网格数组,作为当前数组,这样就避免了显示出图形以前的位置
intm_row=startY/step;
intm_col=startX/step;
for(inti=m_row;i{
for(intj=m_col;j{
*(currentMap+i*map_col+j)|=*(currentItem+(i-m_row)*4+(j-m_col));//坐标转换
}
}
}
voidGameArea:
:
copy_Map(unsignedchar*theMap,unsignedchar*toMap,introw,intcol)
//游戏区域备份
{
for(inti=0;i{
for(intj=0;j
{
*(toMap+i*col+j)=*(theMap+i*col+j);
}
}
}
voidGameArea:
:
draw_nextItem()//显示下一个要出现的图形
{
QPainterpainter(this);
painter.drawRect(gameArea_width+20,10,4*step,4*step);
painter.setBrush(this->boxBrushColor);
for(inti=0;i<4;i++)
for(intj=0;j<4;j++)
{
if(*(theNextItem+i*4+j)==1)
{
if(this->is_draw_box_picture)
{
QPixmappix;
pix.load(this->boxPicture);
painter.drawPixmap(gameArea_width+20+j*step,10+i*step,step,step,pix);
}
painter.drawRect(gameArea_width+20+j*step,10+i*step,step,step);
}
}
}
voidGameArea:
:
paintEvent(QPaintEvent*e)//重绘事件
{
QPainterpainter(this);
painter.setRenderHint(QPainter:
:
Antialiasing,true);//这样可以反锯齿
if(this->isRotate)//如果旋转游戏区域
{
painter.translate(gameArea_X,gameArea_Y);
painter.rotate(this->theAngle);
painter.drawPixmap(QPoint(pix_X,pix_Y),*pix_gameArea);
}
else
{
painter.drawPixmap(QPoint(pix_X,pix_Y),*pix_gameArea);
}
if(this->isDrawNextItem)//是否显示下一个图形
{
draw_nextItem();
}
}
voidGameArea:
:
keyPressEvent(QKeyEvent*event)//键盘处理
{
switch(event->key())
{
caseQt:
:
Key_Left:
//向左的按键
{
startX=startX-step;
if(isMoveLeft())startX=startX+step;
else
{
currentItem_to_currentMap();
this->draw_gameArea();
if(isPlaySound_moveLeft)
{
playSound(sound_moveLeft);
}
}
break;
}
caseQt:
:
Key_Right:
//向右的按键
{
startX=startX+step;
if(isMoveRight())startX=startX-step;
else
{
currentItem_to_currentMap();
this->draw_gameArea();
if(isPlaySound_moveRight)
{
playSound(sound_moveRight);
}
}
break;
}
caseQt:
:
Key_Up:
//向上的按键
{
do_itemChange();
currentItem_to_currentMap();
this->draw_gameArea();
if(isPlaySound_itemChange)
{
playSound(sound_itemChange);
}
break;
}
caseQt:
:
Key_Down:
//向下的按键
{
if(this->isKey_Down_Move_OneStep)//默认一次下移一步
{
startY+=step;
if(this->isMoveEnd())startY-=step;//在这里只做下移一步的操作,不做其他处理
else
{
currentItem_to_currentMap();
this->draw_gameArea();
if(isPlaySound_moveDown)
{
playSound(sound_moveDown);
}
}
}
else//一次下移到底
{
inti=0;
while(!
this->isMoveEnd())
{
startY+=step;
i++;
}
startY-=step;
if(i>0)
{
currentItem_to_currentMap();
this->draw_gameArea();
if(isPlaySound_moveDown)
{
playSound(sound_moveDown);
}
}
}
break;
}
default:
QFrame:
:
keyPressEvent(event);//接收其他按键
}
}
boolGameArea:
:
isMoveEnd()
//判断图形是否移动到了最下面或和别的图形重合了
{
intitem_endRow=myItem->currentItem_endPos(currentItem,'d');
inti
|
|