敌方炮弹攻击快速问题的解决途径

2024-09-27 09:31:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图让敌人的子弹攻击玩家 但它的进攻方式很快我不知道为什么VIDEO 我的敌人子弹课

    # enemys bullets
    ksud = pygame.image.load("heart.png")
    class Boolss(object):
       def __init__(self, x, y,color, xspeed, yspeed):
           self.x = x
           self.y = y
           self.xspeed = xspeed
           self.yspeed = yspeed
           self.ksud = pygame.image.load("heart.png")
           self.hitbox  = self.ksud.get_rect()
           self.rect  = self.ksud.get_rect()
           self.rect.topleft = (self.x,self.y)
           self.color = color
           self.hitbox = (self.x + 57, self.y + 33, 29, 52) # NEW
       def draw(self, window):
            self.rect.topleft = (self.x,self.y)
            player_rect = self.ksud.get_rect(center = self.rect.center) 
            player_rect.centerx += 0 # 10 is just an example
            player_rect.centery += 0 # 15 is just an example
            window.blit(self.ksud, player_rect)
            self.hitbox = (self.x + 97, self.y + 33, 10, 10) # NEW
            window.blit(self.ksud,self.rect)

这是它附加子弹一样的攻击球员

          for shootss in shootsright:


                shootss.x += shootss.xspeed
                shootss.y += shootss.yspeed
                if shootss.x > 500 or shootss.x < 0 or shootss.y > 500 or shootss.y < 0:
                    shootsright.pop(shootsright.index(shootss))




            if len(shootsright) < 1:
                start_x = round(enemyshoots1.x+enemyshoots1.width-107)
                start_y = round(enemyshoots1.y + enemyshoots1.height-50)
                target_x = playerman.x+playerman.width//2
                target_y = playerman.y+playerman.width//2
                dir_x, dir_y = target_x - start_x, target_y - start_y
                distance = math.sqrt(dir_x**2 + dir_y**2)
                if distance > 0:
                    shootsright.append(Boolss(start_x,start_y,(0,0,0),dir_x, dir_y))
        #------------------------------------------------------------------------------

Tags: rectselftargetdirstartcolorplayer子弹
1条回答
网友
1楼 · 发布于 2024-09-27 09:31:01

问题在于:

start_x = round(enemyshoots1.x+enemyshoots1.width-107)
start_y = round(enemyshoots1.y + enemyshoots1.height-50)
target_x = playerman.x+playerman.width//2
target_y = playerman.y+playerman.width//2
dir_x, dir_y = target_x - start_x, target_y - start_y

如果打印dir_xdir_y,您将看到值非常高。您可能会在每一帧将该值添加到项目符号的位置。这就是为什么他们在你真正看到他们之前就离开了屏幕

你可以试试这个:

BULLET_SPEED = 5

delta_x, delta_y = target_x - start_x, target_y - start_y
distance = math.sqrt(delta_x ** 2 + delta_y ** 2)
dir_x = BULLET_SPEED * delta_x / distance
dir_y = BULLET_SPEED * delta_y / distance

认为这个解决方案不舍入值,PyGod会因为浮点值的坐标而抛出异常。在使用坐标进行绘图之前,应先将坐标四舍五入

相关问题 更多 >

    热门问题