/********************************************************************************
* File Name: gamearea.cpp
* Description: 第14章实例训练
* Reference book:《Linux环境下Qt4图形界面与MySQL编程》,机械工业出版社.2012.1
* E_mail: openlinux2011@gmail.com
*
********************************************************************************/
#include "gamearea.h"
#include <QKeyEvent>
#include <QSound>
#include <QMessageBox>
#include "input.h"
//constructor and destructor below
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->moveSpeed = speed;
this->gameStart();
}
GameArea::~GameArea()
{
}
//core functions
//item down one step
void GameArea::moveOneStep()
{
startY += step;
}
//display the map after an item went down
void GameArea::do_MoveNext()
{
this->currentItem_to_currentMap();
this->draw_gameArea();
}
void GameArea::nextItem()
{//clear rows after copying the map
copy_Map(currentMap,copyMap,map_row,map_col);
this->clearRow();
}
//if the game is over
bool GameArea::isGame_Over()
{
if(this->isGameOver) return true;
else return false;
}
//get num of the row which is full
int GameArea::getFullRowNum()
{
return fullRowNum;
}
//set item's color or backgroud's pic
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;
}
//move down 1 step or move to the end
void GameArea::setKey_Down_Move_oneStep(bool Bool)
{
this->isKey_Down_Move_OneStep = Bool;
}
//if display grid
void GameArea::setDrawGrid(bool Bool)
{
isDrawGrid = Bool;
}
//if display the next item which will occur soon
void GameArea::setDrawNextItem(bool Bool)
{
isDrawNextItem = Bool;
}
//add item
void GameArea::setbox(int row,int col)
{
*(copyMap+row*map_col+col) = 1;
}
//core functions below
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)
//init gamearea,init area where shows the next item,frame width,frame height,gameareawidth,gameareaheight,boxstep,startx,starty
{
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;
//init gamearea
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 =true;
//default options
this->isDrawGrid = true;
this->isDrawNextItem=true;
this->is_draw_box_picture = false;
//default color sets
this->gameAreaColor = Qt::white;
this->boxBrushColor = Qt::yellow;
this->boxPenColor = Qt::green;
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() //init variable
{
this->init_Map(currentMap,map_row,map_col);
this->init_Map(copyMap,map_row,map_col);
this->currentItem = this->currentMap; //init pointer
this->theNextItem = this->currentMap; //pointer->null
isFirstItem = true;
isGameOver = false;
fullRowNum = 0;
this->draw_gameArea();
this->score=0;
}
void GameArea::gameStart()
{
this->startX = this->init_startX;
this->startY = this->init_startY;
fullRowNum = 0; //clear rows which has been fullfilled by items when the new item showed
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)
//set array 0
{
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
{
*(initMap+i*col+j)= 0;
}
}
}
void GameArea::draw_gameArea() //draw gamearea
{
this->pix_gameArea->fill(gameAreaColor);
if(this->isDrawGrid)
{
draw_Grid();
}
draw_currentMap();
update();
}
void GameArea::draw_Grid() //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() //draw current map
{
QPainter painter(this->pix_gameArea);
painter.setPen(this->boxPenColor);
painter.setBrush(this->boxBrushColor);
for(int i=0;i<map_row;i++) //draw the item in grid to the map
{
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()
//draw current item to current map
{
copy_Map(copyMap,currentMap,map_row,map_col);//use copy map
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));//change coordinate
}
}
}
void GameArea::copy_Map(unsigned char *theMap,unsigned char *toMap,int row,int col)
//copy the map
{
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
{
*(toMap+i*col+j)= *(theMap+i*col+j);
}
}
}
void GameArea::draw_nextItem() //display next item
{
QPainter painter(this);
painter.drawRect(gameArea_width+20,10,4*step,4*step);
painter.setBrush(this->boxBrushColor);
for(int i=0;i<4;i++)
for(int j=0;j<4;j++)
{
if(*(theNextItem + i*4 +j)==1)
{
if(this->is_draw_box_picture)
{
QPixmap pix;
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);
}
}
}
void GameArea::paintEvent(QPaintEvent *e) //repaint
{
QPainter painter(this);
painter.drawPixmap(QPoint(pix_X,pix_Y),*pix_gameArea);
if(this->isDrawNextItem) //if display next item
{
draw_nextItem();
}
}
void GameArea::keyPressEvent(QKeyEvent *event) //keyboard event
{
switch(event->key())
{
case Qt::Key_Left : //press left
{
startX=startX - step;
QT.rar_qt 俄罗斯_qt4
版权申诉
9 浏览量
2022-09-23
12:24:09
上传
评论
收藏 15KB RAR 举报
JaniceLu
- 粉丝: 78
- 资源: 1万+
最新资源
- 嵌入式基础学习资料0000
- content_1714465331721.xls
- HM3401-VB一款P-Channel沟道SOT23的MOSFET晶体管参数介绍与应用说明
- Chainbase+Web3+APi+Data warehouse
- HM3401D-VB一款P-Channel沟道SOT23的MOSFET晶体管参数介绍与应用说明
- GEE案例-1990-2023年计算某个区域的NDVI时序变化轨迹(以徐州市为例)含全部代码.pdf
- HM3401C-VB一款P-Channel沟道SOT23的MOSFET晶体管参数介绍与应用说明
- 沈丘盛世龙门图纸符合规范化
- 基于51单片机带LCD1602显示+温度补偿的超声波测距仪设计软硬设计+仿真+文档资料.zip
- 一、K210是什么? K210全称为堪智K210,是嘉楠科技自主研发的一款采用RISC-V处理器架构,具备视听一体、自主IP内核
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈