1、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()
2、;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(mo
3、veSpeed);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
4、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:setBoxPenColo
5、r(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 = Boo
6、l;/是否显示网格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-isP
7、laySound_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_mo
8、veDown = 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-i
9、sPlaySound_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:setGameA
10、reaPix(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_hei
11、ght = 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_h
12、eight); 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-gameArea
13、Color = 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 charmap_row*map_col; this-copyMap = new unsigned c
14、harmap_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 =
15、 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; thi
16、s-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;irow;i+) for(int j=0;jpix_gameArea-fill(gameAreaColor); if(this-isDrawGrid) draw_Grid(); draw_currentMap(); update();voi
17、d GameArea:draw_Grid() /绘制游戏区域背景的网格 QPainter painter(this-pix_gameArea); painter.setPen(Qt:DotLine); for(int i=0; igameArea_width,i*step); for(int j=0; jgameArea_height); void GameArea:draw_currentMap() /绘制游戏区域现在已有的方块 QPainter painter(this-pix_gameArea); painter.setPen(this-boxPenColor); painter.set
18、Brush(this-boxBrushColor); for(int i=0;imap_row;i+) /将网格上的图形显示到界面上 for(int j=0;jis_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
19、(copyMap,currentMap,map_row,map_col);/使用备份的网格数组,作为当前数组,这样就避免了显示出图形以前的位置 int m_row = startY/step; int m_col = startX/step; for(int i=m_row;im_row+4;i+) for(int j=m_col;jm_col+4;j+) *(currentMap+i*map_col+j)|=*(currentItem+(i-m_row)*4+(j-m_col);/坐标转换 void GameArea:copy_Map(unsigned char *theMap,unsign
20、ed char *toMap,int row,int col) /游戏区域备份 for(int i=0;irow;i+) for(int j=0;jboxBrushColor); for(int i=0;i4;i+) for(int j=0;jis_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*
21、step,step,step); void GameArea:paintEvent(QPaintEvent *e) /重绘事件 QPainter painter(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
22、); else painter.drawPixmap(QPoint(pix_X,pix_Y),*pix_gameArea); if(this-isDrawNextItem) /是否显示下一个图形 draw_nextItem(); void GameArea:keyPressEvent(QKeyEvent *event) /键盘处理 switch(event-key() case Qt:Key_Left : /向左的按键 startX=startX - step; if(isMoveLeft() startX = startX+step; else currentItem_to_currentM
23、ap(); this-draw_gameArea(); if(isPlaySound_moveLeft) playSound(sound_moveLeft); break; case Qt:Key_Right : /向右的按键 startX=startX + step; if(isMoveRight() startX = startX-step; else currentItem_to_currentMap(); this-draw_gameArea(); if(isPlaySound_moveRight) playSound(sound_moveRight); break; case Qt:
24、Key_Up : /向上的按键 do_itemChange(); currentItem_to_currentMap(); this-draw_gameArea(); if(isPlaySound_itemChange) playSound(sound_itemChange); break; case Qt:Key_Down : /向下的按键 if(this-isKey_Down_Move_OneStep) /默认一次下移一步 startY+=step; if(this-isMoveEnd() startY-=step; /在这里只做下移一步的操作,不做其他处理 else currentIte
25、m_to_currentMap(); this-draw_gameArea(); if(isPlaySound_moveDown) playSound(sound_moveDown); else /一次下移到底 int i=0; while(!this-isMoveEnd() startY +=step; i+; startY -=step; if(i0) currentItem_to_currentMap(); this-draw_gameArea(); if(isPlaySound_moveDown) playSound(sound_moveDown); break; default: QFrame:keyPressEvent(event); /接收其他按键 bool GameArea:isMoveEnd() /判断图形是否移动到了最下面或和别的图形重合了 int item_endRow = myItem-currentItem_endPos(currentItem,d); int i
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1