#include <QtGui>
#include "gamearea.h"
#include <QKeyEvent>
#include <QSound>
#include <QMessageBox>
GameArea::GameArea(QWidget *parent) :
QFrame(parent)
{
this->init_gameArea(6,6,430,430,200,400,20,60,0);
this->init_Game();
}
GameArea::GameArea(int speed,QWidget *parent) :
QFrame(parent)
{
this->init_gameArea(6,6,430,430,200,400,20,60,0);
this->init_Game();
this->moveTimer = new QTimer(this);
connect(this->moveTimer,SIGNAL(timeout()),this,SLOT(moveTimer_upDate()));
this->moveSpeed = speed;
this->gameStart();
this->moveTimer->start(moveSpeed);
}
void GameArea::setNextPieceLabel(QLabel *label)
{
nextPieceLabel = label;
}
GameArea::~GameArea()
{
}
//方块下移一步
void GameArea::moveOneStep()
{
startY += step;
}
//显示下移一步后的界面
void GameArea::do_MoveNext()
{
this->currentItem_to_currentMap();
this->draw_gameArea();
}
void GameArea::nextItem()
{//先将现在的游戏区域备份再消行
copy_Map(currentMap,copyMap,map_row,map_col);
this->clearRow();
}
//游戏是否已经结束
bool GameArea::isGame_Over()
{
if(this->isGameOver) return true;
else return false;
}
//获取已满的行数
int GameArea::getFullRowNum()
{
return fullRowNum;
}
//设置方块颜色或贴图
void GameArea::setGameAreaColor(QColor color)
{
gameAreaColor = color;
}
void GameArea::setBoxBrushColor(QColor color)
{
boxBrushColor = color;
}
void GameArea::setBoxPenColor(QColor color)
{
boxPenColor = color;
}
void GameArea::set_draw_box_picture(bool Bool)
{
this->is_draw_box_picture = Bool;
}
void GameArea::setBoxPicture(QString fileName)
{
this->boxPicture = fileName;
}
//设置方块颜色或贴图
//是否下移一步,如果不是则一次下移到底
void GameArea::setKey_Down_Move_oneStep(bool Bool)
{
this->isKey_Down_Move_OneStep = Bool;
}
//是否显示网格
void GameArea::setDrawGrid(bool Bool)
{
isDrawGrid = Bool;
}
//是否显示下一个要出现的图形
void GameArea::setDrawNextItem(bool Bool)
{
isDrawNextItem = Bool;
}
//自己往游戏区添加方块
void GameArea::setbox(int row,int col)
{
*(copyMap+row*map_col+col) = 1;
}
//是否播放声音
void GameArea::setPlaySound_moveLeft(QString fileName,bool Bool)
{
this->isPlaySound_moveLeft = Bool;
this->sound_moveLeft = fileName;
}
void GameArea::setPlaySound_moveRight(QString fileName,bool Bool)
{
this->isPlaySound_moveRight = Bool;
this->sound_moveRight = fileName;
}
void GameArea::setPlaySound_moveDown(QString fileName,bool Bool)
{
this->isPlaySound_moveDown = Bool;
this->sound_moveDown = fileName;
}
void GameArea::setPlaySound_itemChange(QString fileName,bool Bool)
{
this->isPlaySound_itemChange = Bool;
this->sound_itemChange = fileName;
}
void GameArea::setPlaySound(bool Bool)
{
this->isPlaySound_moveLeft = Bool;
this->isPlaySound_moveRight = Bool;
this->isPlaySound_moveDown = Bool;
this->isPlaySound_itemChange = Bool;
}
//是否播放声音
void GameArea::setRotate(bool Bool) //开启旋转
{
this->isRotate = Bool;
}
void GameArea::setRotateAngle(int angle) //顺时针旋转角度
{
this->theAngle = angle;
}
void GameArea::setGameAreaPixOrigin(int x,int y) //新的坐标原点
{
this->gameArea_X = x;
this->gameArea_Y = y;
}
void GameArea::setGameAreaPix(int x,int y) //游戏区域的位置
{
this->pix_X = x;
this->pix_Y = y;
}
void GameArea::init_gameArea(int X,int Y,int frame_width,int frame_height,int width,int height,int boxStep,int start_x,int start_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 = new QPixmap(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 = new MyItem();
this->currentMap = new unsigned char[map_row*map_col];
this->copyMap = new unsigned char[map_row*map_col];
}
void GameArea::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();
}
void GameArea::gameStart() //游戏开始运行,而且每次出现新的方块都调用一次这个函数
{
this->startX = this->init_startX;
this->startY = this->init_startY;
fullRowNum = 0; //每次出现一个新的图形都将上次满的行数清0
if(isFirstItem)
{
this->currentItem = myItem->getItem();
isFirstItem = false;
}
else this->currentItem = this->theNextItem;
this->theNextItem = myItem->getItem();
this->currentItem_to_currentMap();
this->draw_gameArea();
}
void GameArea::init_Map(unsigned char *initMap,int row,int col)
//用于让一个数组中的数全部为0
{
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
{
*(initMap+i*col+j)= 0;
}
}
}
void GameArea::draw_gameArea() //绘制游戏区域,包括网格和游戏区的方块
{
this->pix_gameArea->fill(gameAreaColor);
if(this->isDrawGrid)
{
draw_Grid();
}
draw_currentMap();
update();
}
void GameArea::draw_Grid() //绘制游戏区域背景的网格
{
QPainter painter(this->pix_gameArea);
painter.setPen(Qt::DotLine);
for(int i=0; i<map_row; i++)
{
painter.drawLine(QPoint(0,i*step),QPoint(this->gameArea_width,i*step));
}
for(int j=0; j<map_col; j++)
{
painter.drawLine(QPoint(j*step,0),QPoint(j*step,this->gameArea_height));
}
}
void GameArea::draw_currentMap()
//绘制游戏区域现在已有的方块
{
QPainter painter(this->pix_gameArea);
painter.setPen(this->boxPenColor);
painter.setBrush(this->boxBrushColor);
for(int i=0;i<map_row;i++) //将网格上的图形显示到界面上
{
for(int j=0;j<map_col;j++)
{
if(*(currentMap+i*map_col+j)==1)
{
if(this->is_draw_box_picture)
{
QPixmap pix;
pix.load(this->boxPicture);
painter.drawPixmap(j*step,i*step,step,step,pix);
}
painter.drawRect(j*step,i*step,step,step);
}
}
}
}
void GameArea::currentItem_to_currentMap()
//将当前图形加入到游戏区域网格上
{
copy_Map(copyMap,currentMap,map_row,map_col);//使用备份的网格数组,作为当前数组,这样就避免了显示出图形以前的位置
int m_row = startY/step;
int m_col = startX/step;
for(int i=m_row;i<m_row+4;i++)
{
for(int j=m_col;j<m_col+4;j++)
{
*(currentMap+i*map_col+j)|=*(currentItem+(i-m_row)*4+(j-m_col));//坐标转换
}
}
}
void GameArea::copy_Map(unsigned char *theMap,unsigned char *
- 1
- 2
前往页