移植追踪功能(追踪弹)

调用对象和参数要正确地对应,不然会有意想不到的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)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值