-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathbullet.cpp
236 lines (218 loc) · 4.49 KB
/
bullet.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#include "bullet.h"
#include <QDebug>
Bullet::Bullet()
{
w = 8;
h = 12;
speed = BASESIZE*2 / 3;
active = false;
bump = false;
leftimg.load((rootdir+"\\pic\\bullet-0.gif").c_str());
leftimg = resizePic(leftimg,h,w);
upimg.load((rootdir+"\\pic\\bullet-1.gif").c_str());
upimg = resizePic(upimg,w,h);
rightimg.load((rootdir+"\\pic\\bullet-2.gif").c_str());
rightimg = resizePic(rightimg,h,w);
downimg.load((rootdir+"\\pic\\bullet-3.gif").c_str());
downimg = resizePic(downimg,w,h);
// bump1.load((rootdir+"\\pic\\bump1.gif").c_str());
// bump1 = resizePic(bump1,BASESIZE,BASESIZE);
// bump2.load((rootdir+"\\pic\\bump2.gif").c_str());
// bump2 = resizePic(bump2,BASESIZE,BASESIZE);
bump3.load((rootdir+"\\pic\\bump3.gif").c_str());
bump3 = resizePic(bump3,BASESIZE,BASESIZE);
rect.setRect(-1,-1,0,0);
}
Bullet::Bullet(const Bullet &other)
{
*this=other;
}
void Bullet::setActive(bool a)
{
active = a;
if(active==false)
{
bump = true;
//这里设置偏移量
int x = rect.x();
int y = rect.y();
if(dir==direct::up||dir ==direct::down)
{
x-=(BASESIZE-w)/2;
}
else if(dir ==direct::left||dir == direct::right)
{
y-=(BASESIZE-w)/2;
}
bumpx = x;
bumpy = y;
rect.setRect(-1,-1,0,0);
}
}
bool Bullet::getActive()
{
return active;
}
void Bullet::move()
{
if(getActive()==false)
return;
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,dir))
{
rect.moveTo(x,y);
}
else
{
setActive(false);
}
}
void Bullet::setDir(direct dir)
{
this->dir=dir;
}
void Bullet::display(QPainter &paint)
{
if(!getActive())//未发射 return
{
return;
}
if(dir==direct::up)
{
paint.drawPixmap(rect.x(),rect.y(),upimg);
}
else if(dir==direct::down)
{
paint.drawPixmap(rect.x(),rect.y(),downimg);
}
else if(dir==direct::left)
{
paint.drawPixmap(rect.x(),rect.y(),leftimg);
}
else if(dir==direct::right)
{
paint.drawPixmap(rect.x(),rect.y(),rightimg);
}
}
bool Bullet::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()<<"子弹越界";
return false;
}
//判断是否有障碍物
else if((map[y][x]<='2'||map[y][x]=='4')&&(map[y1][x1]<='2'||map[y1][x1]=='4'))//注意行和列与x,y的关系
{
return true;
}
else
{
//打砖块
if(map[y][x]=='3')
{
map[y][x]='0';
}
if(map[y1][x1]=='3')
{
map[y1][x1]='0';
}
if(map[y][x]=='5'||map[y1][x1]=='5')
{
QSound::play((rootdir+"sound\\bin.wav").c_str());
}
return false;
}
}
Bullet &Bullet::operator=(const Bullet &other)
{
if(this==&other)return *this;
speed=other.speed;
active=other.active;
bump = other.bump;
bumpx = other.bumpx;
bumpy = other.bumpy;
w=other.w;
h=other.h;
dir=other.dir;
upimg=other.upimg;
downimg=other.downimg;
leftimg=other.leftimg;
rightimg=other.rightimg;
// bump1=other.bump1;
// bump2=other.bump2;
bump3=other.bump3;
rect=other.rect;
return *this;
}
void Bullet::showExplosion(QPainter& paint)
{
// static int index=0;
// if(index>=3)
// {
bump=false;
// index=0;
// }
// if(index==0)
// {
// paint.drawPixmap(bumpx,bumpy,bump1);
// }
// else if(index==1)
// {
// paint.drawPixmap(bumpx,bumpy,bump2);
// }
// else if(index==2)
// {
// }
paint.drawPixmap(bumpx,bumpy,bump3);
// index++;
}
Bullet::~Bullet()
{
}