您的位置:首页 > 编程语言 > C语言/C++

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

2017-12-18 14:37 615 查看
#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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51

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);

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95

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一样。
1
2
3
4
5
6
7
8
9

mainwindow.h里加入
#include"tank.h"
1

mainwindow.cpp构造函数里加入
glo.player=new Tank();
1

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();
}
1
2
3
4
5
6
7
8
9
10
11

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();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: