Repository navigation
Expand file tree
/
Copy pathBullet.cpp
More file actions
113 lines (104 loc) · 1.58 KB
/
Copy pathBullet.cpp
File metadata and controls
113 lines (104 loc) · 1.58 KB
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
#include "stdafx.h"
#include "Bullet.h"
Bullet::Bullet(int x, int y, TankMoveDir dir, SDL_Texture* texture,
Tank* tank):
tex(texture), dir(dir), tank(tank)
{
_x = x;
_y = y;
bulletSpeed = INIT_BULLET_SPEED;
}
// 绘制方法
void Bullet::Draw(SDL_Renderer* renderer)
{
int width = 0, height = 0;
SDL_QueryTexture(tex, NULL, NULL, &width, &height);
SDL_Rect rect = {
_x, _y, width, height
};
SDL_RenderCopy(renderer, tex, NULL, &rect);
}
// 移动
void Bullet::Move()
{
switch (dir)
{
case LEFT:
_x -= bulletSpeed;
break;
case UP:
_y -= bulletSpeed;
break;
case RIGHT:
_x += bulletSpeed;
break;
case DOWN:
_y += bulletSpeed;
break;
case NONE:
break;
default:
break;
}
}
bool Bullet::MoveSelf()
{
if (!IsCollisionScreen()){
Move();
return false;
}
else{
// 设置tank可以继续发射子弹
tank->setIsCanLauncher(true);
return true;
}
}
// 检测子弹是否在屏幕范围之内
bool Bullet::IsCollisionScreen()
{
bool result = false;
switch (dir)
{
case LEFT:
{
if ((_x - bulletSpeed) < GAME_LEFT_AREA)
result = true;
else
result = false;
}
break;
case UP:
{
if ((_y - bulletSpeed) < GAME_TOP_AREA)
result = true;
else
result = false;
}
break;
case RIGHT:
{
if ((_x + bulletSpeed + BULLET_LENGTH) > GAME_RIGHT_AREA)
result = true;
else
result = false;
}
break;
case DOWN:
{
if ((_y + bulletSpeed + BULLET_LENGTH) > GAME_BOTTOM_AREA)
result = true;
else
result = false;
}
break;
case NONE:
break;
default:
break;
}
return result;
}
Bullet::~Bullet(void)
{
// nothing to do
}