C++(qt)游戏实战项目:坦克大战(四)
2017-06-03 07:13
951 查看
屏幕上光有地图也不行,还需要我们的坦克加入其中
新建tank.h和tank.cpp
tank.h里加入
tank.cpp
main.h结构体Glo中加入选项Tank *player.
mainwindow.h里加入
mainwindow.cpp构造函数里加入
mainwindow.cpp paintEvent函数变更如下
mainwindow.cpp keyPressEvent变更如下
到此你将有了一个可以移动穿墙的tank,还可以移动到游戏外界
后面C++(qt)游戏实战项目:坦克大战(五)将用到之前基类的碰撞函数来约束坦克遇到墙停止
本文章为作者原创
转载请标明本系列文章地址:http://blog.csdn.net/qq_26046771/article/details/72643740
新建tank.h和tank.cpp
tank.h里加入
#ifndef TANK_H #define TANK_H #include"wanwu.h" #include"main.h" #include"gamemap.h" class Tank : public Wanwu { protected: int steps[8]={4,8,8,16,16,32,32,64}; float lifes[8]={200,400,600,800,900,1100,1300,1500}; float wulis[8]={20,40,80,160,160,320,320,640}; float fashus[8]={20,40,80,160,160,320,320,640}; float hujias[8]={50,70,90,110,130,150,170,190}; float mokangs[8]={50,70,90,110,130,150,170,190}; int gongjijianges[8]={8,7,6,5,4,3,2,2}; int group;//坦克所在组,同一组不能相互伤害 public: Tank(); Tank(int iIndex,int jIndex,Dir dir=UP,int style=0,int group=1); // 计算势力范围 virtual void CalculateSphere(); // 绘图 void Display(QPainter &paint); // 移动 void Move(); //设置移动状态为开 void startmove(){ismove=true;} //设置移动状态为关 void stopmove(){ismove=false;} // // void fire(); //void startfire(); //void stopfire(); //设置方向 void setdir(Dir dir){m_dir=dir;} private: int style; bool ismove; // bool isfire; int gongjijiange; }; #endif // TANK_H
tank.cpp
#include "tank.h" Tank::Tank() { this->m_pos.setX(10*CELLWIDTH+CELLWIDTH/2); this->m_pos.setY(8*CELLHEIGHT+CELLHEIGHT/2); this->wuli=200; this->group=0;//0玩家组,1敌人组 m_step=16; gongjijiange=3; this->style=0; ismove=false; m_dir=UP; //isfire=false; m_bDisappear=false; life=1000; CalculateSphere();//计算实力范围 } Tank::Tank(int iIndex,int jIndex,Dir dir,int style,int group){ this->m_pos.setX(jIndex*CELLWIDTH+CELLWIDTH/2); this->m_pos.setY(iIndex*CELLHEIGHT+CELLHEIGHT/2); this->m_dir=dir;z this->style=style; this->group=group; //isfire=false; wuli=wulis[style]; fashu=fashus[style]; hujia=hujias[style]; mokang=mokangs[style]; m_step=steps[style]; gongjijiange=gongjijianges[style]; m_bDisappear=false; life=lifes[style]; CalculateSphere(); } void Tank::Display(QPainter &paint){ QRect xm_rectSphere=m_rectSphere; if(m_bDisappear)return;//消失的不画 switch(m_dir){ case UP: paint.drawImage(m_rectSphere,*glo.tankimage,QRect(2*PICTANKWIDTH,style*2*PICTANKHEIGHT,PICTANKWIDTH,PICTANKHEIGHT));//身体 paint.drawImage(xm_rectSphere,*glo.tankimage,QRect(2*PICTANKWIDTH,(style*2+1)*PICTANKHEIGHT,PICTANKWIDTH,PICTANKHEIGHT));//炮塔 break; case DOWN: paint.drawImage(m_rectSphere,*glo.tankimage,QRect(0*PICTANKWIDTH,style*2*PICTANKHEIGHT,PICTANKWIDTH,PICTANKHEIGHT));//身体 paint.drawImage(xm_rectSphere,*glo.tankimage,QRect(0*PICTANKWIDTH,(style*2+1)*PICTANKHEIGHT,PICTANKWIDTH,PICTANKHEIGHT));//炮塔 break; case LEFT: paint.drawImage(m_rectSphere,*glo.tankimage,QRect(1*PICTANKWIDTH,style*2*PICTANKHEIGHT,PICTANKWIDTH,PICTANKHEIGHT));//身体 paint.drawImage(xm_rectSphere,*glo.tankimage,QRect(1*PICTANKWIDTH,(style*2+1)*PICTANKHEIGHT,PICTANKWIDTH,PICTANKHEIGHT));//炮塔 break; case RIGHT: paint.drawImage(m_rectSphere,*glo.tankimage,QRect(3*PICTANKWIDTH,style*2*PICTANKHEIGHT,PICTANKWIDTH,PICTANKHEIGHT));//身体 paint.drawImage(xm_rectSphere,*glo.tankimage,QRect(3*PICTANKWIDTH,(style*2+1)*PICTANKHEIGHT,PICTANKWIDTH,PICTANKHEIGHT));//炮塔 break; } }//此段代码可以优化简洁 void Tank::Move() { if(ismove==true){ c6dd switch(m_dir){ case UP: m_pos.setY(m_pos.y()-m_step); break; case DOWN: m_pos.setY(m_pos.y()+m_step); break; case LEFT: m_pos.setX(m_pos.x()-m_step); break; case RIGHT: m_pos.setX(m_pos.x()+m_step); break; } CalculateSphere(); qDebug("move on"); } qDebug("move off"); } void Tank::CalculateSphere(){ this->m_rectSphere.setRect(m_pos.x()-TANKWIDTH/2,m_pos.y()-TANKHEIGHT/2,TANKWIDTH,TANKHEIGHT); }
main.h结构体Glo中加入选项Tank *player.
class QImage; class GameMap; class Tank; typedef struct{ unsigned int framei; QImage *blockimage; GameMap *gamemap; Tank *player; }Glo;//实列化Glo类型变量,能实列化出来,所有的指针变量所占空间daxiao一样。
mainwindow.h里加入
#include"tank.h"
mainwindow.cpp构造函数里加入
glo.player=new Tank();
mainwindow.cpp paintEvent函数变更如下
void MainWindow::paintEvent(QPaintEvent *event) { Q_UNUSED(event); setFixedSize(WIDTH,HEIGHT); paint.begin(this); glo.gamemap->Display(paint); glo.player->Display(paint); paint.end(); }
mainwindow.cpp keyPressEvent变更如下
void MainWindow::keyPressEvent(QKeyEvent *event){ qDebug("key:--------------------------%d ",event->key()); //当按键为M时设置游戏状态为mapedit if(event->key()==Qt::Key_M) { gamestatus=mapedit; }else if(event->key()==Qt::Key_G){ gamestatus=gameing; } if(gamestatus==mapedit){ if(event->key()==Qt::Key_S) { glo.gamemap->savemap("1.dat"); } else if(event->key()==Qt::Key_L) { glo.gamemap->loadmap("1.dat"); } }else if(gamestatus==gameing){ if(event->key()==Qt::Key_S) { glo.player->setdir(DOWN);glo.player->startmove(); } else if(event->key()==Qt::Key_A) { glo.player->setdir(LEFT);glo.player->startmove(); } else if(event->key()==Qt::Key_W) { glo.player->setdir(UP);glo.player->startmove(); } else if(event->key()==Qt::Key_D) { glo.player->setdir(RIGHT);glo.player->startmove(); }else if(event->key()==Qt::Key_J){ glo.player->startfire(); } } update(); }
到此你将有了一个可以移动穿墙的tank,还可以移动到游戏外界
后面C++(qt)游戏实战项目:坦克大战(五)将用到之前基类的碰撞函数来约束坦克遇到墙停止
本文章为作者原创
转载请标明本系列文章地址:http://blog.csdn.net/qq_26046771/article/details/72643740
相关文章推荐
- C++(qt)游戏实战项目:坦克大战(前言)
- C++(qt)游戏实战项目:坦克大战(二)
- C++(qt)游戏实战项目:坦克大战(三)
- C++(qt)游戏实战项目:坦克大战(四)
- C++(qt)游戏实战项目:坦克大战(一)
- C++(qt)游戏实战项目:坦克大战(六源代码)
- C++(qt)游戏实战项目:坦克大战(五)
- Qt/C++项目实战模仿酷狗之播放列表实现(一)
- Unity3D游戏引擎实战开发从入门到精通(坦克大战项目实战、NGUI开发、GameObject)
- Qt/C++ 项目实战模仿酷狗之实现 换肤(二)
- C++实战项目:坦克大战(一)
- Android项目实战(四十一):游戏和视频类型应用 状态栏沉浸式效果
- C++ 解决:QT项目从文件获取汉字显示乱码
- python实战游戏开发——开始游戏项目
- Android快乐贪吃蛇游戏实战项目开发教程-05虚拟方向键(四)四个三角形按钮
- Cocos2d-X游戏【泰然网《跑酷》】JS到C++移植2:如何新建一个项目,引擎目录结构介绍
- Qt中纯C++项目发布为dll的方法(超详细步骤)
- android游戏开发项目实战——数独
- 在windows下的qt5.7.1 的纯C或C++项目命令行中文乱码、无法编译等问题解决
- QT编写纯C或C++项目时乱码: