|
|
# 炸弹类
class Bomb:
def __init__(self):
self.radius = 30
self.x = random.choice([-50, WIDTH + 50])
self.y = HEIGHT + 50
# 随机速度和角度
angle = random.uniform(math.pi/4, math.pi*3/4)
speed = random.uniform(6, 10)
if self.x > WIDTH:
self.vx = -math.cos(angle) * speed
else:
self.vx = math.cos(angle) * speed
self.vy = -math.sin(angle) * speed
self.gravity = 0.2
self.exploded = False
def update(self):
self.vy += self.gravity
self.x += self.vx
self.y += self.vy
return (self.x < -100 or self.x > WIDTH + 100 or
self.y > HEIGHT + 100)
def draw(self, win):
if not self.exploded:
# 炸弹主体
pygame.draw.circle(
win, BLACK, (int(self.x), int(self.y)), self.radius)
pygame.draw.circle(
win, WHITE, (int(self.x), int(self.y)), self.radius, 2)
# 引线
pygame.draw.line(win, BROWN,
(int(self.x), int(self.y) - self.radius),
(int(self.x) + 10, int(self.y) - self.radius - 15), 3)
else:
# 爆炸效果
for i in range(3):
pygame.draw.circle(
win, RED, (int(self.x), int(self.y)), self.radius + i*10, 5)
def check_hit(self, start_pos, end_pos):
if self.exploded:
return False
# 与水果相同的碰撞检测逻辑
x1, y1 = start_pos
x2, y2 = end_pos
x0, y0 = self.x, self.y
dx = x2 - x1
dy = y2 - y1
length_sq = dx*dx + dy*dy
if length_sq == 0:
return False
t = ((x0 - x1)*dx + (y0 - y1)*dy) / length_sq
t = max(0, min(1, t))
closest_x = x1 + t*dx
closest_y = y1 + t*dy
distance = math.sqrt((closest_x - x0)**2 + (closest_y - y0) ** 2)
return distance <= self.radius
|
|