调用对象和参数要正确地对应,不然会有意想不到的bug(在调用tracer速度的地方写成了调用target的速度,改了一下午才发现
class Bullet2(pygame.sprite.Sprite):
def trace(enemy, self):
x1, y1 = self.rect.x, self.rect.y # 应为pygame的rect函数只接受int类型数,所以为了不影响计算精度,我们先暂时定义两个数用于计算,最后将这连个数传入rect即可
x2, y2 = enemy.rect.x, enemy.rect.y # 同上
dx = x2 - x1
dy = y1 - y2
r = math.sqrt(math.pow(dx, 2) + math.pow(dy, 2))
sin = dy / r
cos = dx / r
# 追踪弹的速度和路径
x1 += cos * self.speed
y1 -= sin * self.speed
self.rect.left, self.rect.top = x1, y1
for循环是要完成第一个参数才会继续执行下一个参数目标,用for循环迭代永远循环复合的第一个参数敌机是永远达不到第二个敌机参数的,现在只遍历小型敌机组
for each in bullet2:
if each.active == True:
for enemy in small_enemies:
myclass.Bullet2.trace(enemy, each)