C++(qt)游戏实战项目:坦克大战(四)

屏幕上光有地图也不行,还需要我们的坦克加入其中
新建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){
    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

你可能感兴趣的:(项目实战)