版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、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
2、: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);
3、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:
4、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
5、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;/是否显示网格
6、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_
7、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 =
8、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-isPlaySoun
9、d_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(in
10、t 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 = hei
11、ght; 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); t
12、his-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 = Q
13、t: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 charmap_ro
14、w*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-
15、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-theNext
16、Item = 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();void GameAre
17、a: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.setBrush(thi
18、s-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(copyMap,
19、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,unsigned char *
20、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*step,step
21、,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); else p
22、ainter.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_currentMap(); thi
23、s-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:Key_Up :
24、/向上的按键 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 currentItem_to_curr
25、entMap(); 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:key
26、PressEvent(event); /接收其他按键 bool GameArea:isMoveEnd() /判断图形是否移动到了最下面或和别的图形重合了 int item_endRow = myItem-currentItem_endPos(currentItem,d); int itemRow = startY/step; /图形现在在网格的位置 int itemCol = startX/step;/*请注意下面的判断是因为上面的语句中已经让图形走了一步,然后经过这里的判断,而这时的图形的位置还没在窗口上显示。*/ if(itemRow+item_endRow=map_row)/如果图形到了
27、网格底部 return true; for(int i=item_endRow;i=0;i-) /是否跟已有的图形重合 for(int j=3;j=0;j-) if(*(copyMap+(itemRow+i)*map_col+itemCol+j) & *(currentItem+i*4+j)=1) if(startX=init_startX&startYcurrentItem_endPos(currentItem,l); int item_Col = startX/step; if(item_Col+item_endLeftcurrentItem_endPos(currentItem,r);
28、int item_Col = startX/step; if(item_Col+item_endRight = map_col) return true; if(isMoveEnd() return true; return false;void GameArea:do_itemChange()/对改变图形后超出窗口或和别的图形重合进行处理 unsigned char *tempItem; tempItem = currentItem; /先保存现在的图形 currentItem = myItem-itemChange(currentItem); /进行图形变化 if(startX gameA
29、rea_width) startX = gameArea_width - 4*step; /右超界,则让其右边靠右 if(isMoveEnd() /如果和别的图形重合,则不让其改变 currentItem = tempItem; void GameArea:clearRow() /清除已满的行 int temp1 = 1; for(int i=map_row-1;i0;i-) /从下向上扫描整张网格 for(int j=0;j0;row-) /让已满的行及其上面的各行均等于其上一行 for(int col = 0;colmoveOneStep(); if(this-isMoveEnd() if
30、(this-isGameOver) this-moveTimer-stop(); QMessageBox:warning(this,tr(warning),tr(Game Over!),QMessageBox:Yes); this-init_Game(); this-gameStart(); this-moveTimer-start(moveSpeed); else this-nextItem(); this-gameStart(); else this-do_MoveNext(); Gamearea.h/*版权声明*所有源代码由原创,我们遵循Qt开源版的所有相关条款和协议。您有权使用,传播和
31、更改我们的代码,但请保证代码的开源。以下源代码版权归所有,请不要用作商业用途。*/#ifndef GAMEAREA_H#define GAMEAREA_H#include #include myitem.h#include #include #include namespace gameArea class GameArea;class GameArea : public QFrame Q_OBJECTpublic: GameArea(QWidget *parent = 0); /不带定时器的构造函数 GameArea(int speed,QWidget *parent = 0); /带定时器
32、的构造函数 GameArea();public: /以下是核心功能控制函数 void init_gameArea(int X,int Y,int frame_width,int frame_height,int width,int height,int boxStep,int start_x,int start_y); void moveOneStep(); bool isMoveEnd(); bool isGame_Over(); void init_Game(); void gameStart(); void nextItem(); int getFullRowNum(); void do
33、_MoveNext(); void draw_gameArea(); /以下是设置颜色函数 void setGameAreaColor(QColor color=Qt:white); void setBoxBrushColor(QColor color=Qt:green); void setBoxPenColor(QColor color=Qt:black); void set_draw_box_picture(bool Bool=false); void setBoxPicture(QString fileName); /以下是设置声音函数 void setPlaySound_moveLef
34、t(QString fileName,bool Bool=false); void setPlaySound_moveRight(QString fileName,bool Bool=false); void setPlaySound_moveDown(QString fileName,bool Bool=false); void setPlaySound_itemChange(QString fileName,bool Bool=false); void setPlaySound(bool Bool=false); /以下是设置游戏区域旋转函数 void setRotate(bool Boo
35、l=false); void setGameAreaPixOrigin(int x,int y); void setGameAreaPix(int x,int y); void setRotateAngle(int angle); /以下是其他功能函数 void setKey_Down_Move_oneStep(bool Bool = false); void setDrawGrid(bool Bool = true); void setDrawNextItem(bool Bool =true); void setbox(int row,int col);private: void playS
36、ound(QString fileName); void init_Map(unsigned char *initMap,int row,int col); void paintEvent(QPaintEvent *); void keyPressEvent(QKeyEvent *); void draw_Grid(); void currentItem_to_currentMap(); void copy_Map(unsigned char *theMap,unsigned char *toMap,int row,int col); void draw_currentMap(); bool
37、isMoveLeft(); bool isMoveRight(); void draw_nextItem(); void do_itemChange(); void clearRow();private: int gameArea_width; int gameArea_height; int map_row; int map_col; int startX; int startY; int step; int init_startX; int init_startY; unsigned char *currentMap; unsigned char *copyMap; QPixmap *pix_gameArea; MyItem *myItem; unsigned char *currentItem; unsigned char *theNextItem; bool isFirstItem; bool isGameOver; int fullRowNum; QColor gameAreaColor; QColor boxPenColor; QColor boxBrushColor; bool isKey_Down_Move_On
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026年消防安全教育培训工作计划
- 2026年职业生涯规划书消防专业
- 2026年排长年终工作述职报告
- 2026年国庆期间安排部署活动
- 2026年医院护士长年终述职报告
- 2026年年度消防工作计划
- 基于成本管控的绩效团队建设
- 2026年幼儿园消防安全主题活动计划
- 2026年商场下半年促销计划方案
- 基于平衡计分卡的医院成本管控绩效评价
- 2026贵州省住房资金管理中心招聘工作人员1人备考题库含答案详解(考试直接用)
- 2026储能入市背景下的投资测算工具设计逻辑深度研究报告
- 2026四川省阿坝州州级事业单位考试调动37人重点基础提升(共500题)附带答案详解
- 2026湖北神农架林区公安局招聘辅警22人笔试参考题库及答案解析
- 2026学校规范教育收费自查整改报告
- 2026中华全国供销合作总社直属事业单位招聘27人考试参考题库及答案解析
- 事故隐患排查治理基本知识
- 煤矿防治水知识培训
- 科学学习方法小学主题班会课件
- 2026江铜铜箔科技股份有限公司第一批次春季校园招聘89人建设笔试参考题库及答案解析
- 2026年建安杯信息通信建设行业安全竞赛重点题库(新版)
评论
0/150
提交评论