forked from zhouyumin/FC-tank
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tankbase.cpp
191 lines (177 loc) · 3.85 KB
/
tankbase.cpp
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include "tankbase.h"
#include <QDebug>
void tankBase::shot()
{
//子弹已经射出就 return
if(bullet.getActive() == true)
return;
// bullet.bump=false;
bullet.setActive(true);
bullet.setDir(dir);
int deviation = (SIZE-bullet.w)/2;//弹道与坦克边界的偏移量
if(dir == direct::up)
{
bullet.rect.setRect(rect.x()+deviation,rect.y(),bullet.w,bullet.h);
}
else if(dir == direct::down)
{
bullet.rect.setRect(rect.x()+deviation,rect.bottom()-bullet.h,bullet.w,bullet.h);
}
else if(dir == direct::left)
{
bullet.rect.setRect(rect.left(),rect.y()+deviation,bullet.h,bullet.w);
}
else if(dir == direct::right)
{
bullet.rect.setRect(rect.right()-bullet.h,rect.y()+deviation,bullet.h,bullet.w);
}
// if(bullet.rect.x()==0||bullet.rect.y()==0||bullet.rect.x()>=13*SIZE||bullet.rect.y()>=13*SIZE)
// {
// bullet.busy=false;
// }
}
void tankBase::move()
{
int x = rect.x();
int y = rect.y();
if(dir == direct::up)
{
y -= speed;
}
else if(dir == direct::down)
{
y += speed;
}
else if(dir == direct::left)
{
x -= speed;
}
else if(dir == direct::right)
{
x += speed;
}
if(canReachable(x,y,this->dir))
{
rect.moveTo(x,y);
// qDebug()<<"move"<<rect.x()<<" "<<rect.y();
}
}
void tankBase::setDir(direct dir)
{
this->dir=dir;
}
void tankBase::display(QPainter &paint,bool state)
{
//state两个状态切换实现履带转动效果
if(state)
{
if(dir==direct::up)
{
paint.drawPixmap(rect.x(),rect.y(),upimg1);
}
else if(dir==direct::down)
{
paint.drawPixmap(rect.x(),rect.y(),downimg1);
}
else if(dir==direct::left)
{
paint.drawPixmap(rect.x(),rect.y(),leftimg1);
}
else if(dir==direct::right)
{
paint.drawPixmap(rect.x(),rect.y(),rightimg1);
}
}
else
{
if(dir==direct::up)
{
paint.drawPixmap(rect.x(),rect.y(),upimg2);
}
else if(dir==direct::down)
{
paint.drawPixmap(rect.x(),rect.y(),downimg2);
}
else if(dir==direct::left)
{
paint.drawPixmap(rect.x(),rect.y(),leftimg2);
}
else if(dir==direct::right)
{
paint.drawPixmap(rect.x(),rect.y(),rightimg2);
}
}
}
bool tankBase::canReachable(int x, int y,direct dir)
{
//转换map坐标
x /= BASESIZE;
y /= BASESIZE;
int x1(0);
int y1(0);
if(dir==direct::up)
{
x1 = x + 1;
y1 = y;
}
else if (dir==direct::down)
{
y += 1;
y1 = y;
x1 = x + 1;
}
else if(dir==direct::left)
{
x1 = x;
y1 = y+1;
}
else if(dir==direct::right)
{
x += 1;
x1 = x;
y1 = y + 1;
}
//判断是否越界
if(x<0 || x1<0 || x>25 || x1>25 || y<0 || y1<0 || y>25 || y1>25)
{
// qDebug()<<"越界"<<x<<" "<<y;
return false;
}
// 判断是否有障碍物
else if(map[y][x]<='2'&&map[y1][x1]<='2')//注意行和列与x,y的关系
{
return true;
}
else
{
// qDebug()<<"障碍";
return false;
}
}
tankBase::tankBase()
{
}
tankBase &tankBase::operator=(const tankBase &other)
{
if(this==&other)return *this;
upimg1=other.upimg1;
upimg2=other.upimg2;
downimg1=other.downimg1;
downimg2=other.downimg2;
leftimg1=other.leftimg1;
leftimg2=other.leftimg2;
rightimg1=other.rightimg1;
rightimg2=other.rightimg2;
speed=other.speed;
rect=other.rect;
dir=other.dir;
bullet = other.bullet;
return *this;
}
tankBase::tankBase(const tankBase &other)
{
*this=other;
}
tankBase::~tankBase()
{
}