我如何使敌人的弹丸攻击玩家移动的位置?

时间:2020-06-09 02:46:43

标签: python pygame

我有一个敌人会射击弹丸,但只会向右射击,我想让它向任何位置的玩家射击我都不知道该怎么做vid您可以步行让我扔出如何做的步骤,那太好了,谢谢

这就是我所做的事情,它只会向右射击,我希望它在玩家所处的任何位置向玩家射击

   for shootss in shootsright:
       if shootss.x < 500 and shootss.x > 0:
           shootss.x += 7
       else:
           shootsright.pop(shootsright.index(shootss))
   if len(shootsright) < 1:
           shootsright.append(Bools(round(enemyshoots1.x+enemyshoots1.width-107),round(enemyshoots1.y + enemyshoots1.height-50),(0,0,0)))


这是我的子弹班

# enemys bullets
ksud = pygame.image.load("heart.png")
class Bools(object):
  def __init__(self, x, y,color):
      self.x = x
      self.y = y
      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.speed = 10
      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)





我的敌人课


shotsright = pygame.image.load("shooting2.png")
shotsleft = pygame.image.load("shooting1.png")
class enemyshoot:
   def __init__(self,x,y,height,width,color):
       self.x = x
       self.y =y
       self.height = height
       self.width = width
       self.color = color
       self.shootsright = pygame.image.load("shooting2.png")
       self.shotsleft = pygame.image.load("shooting1.png")
       self.shootsright = pygame.transform.scale(self.shootsright,(self.shootsright.get_width()//3,self.shootsright.get_height()//3))
       self.shotsleft = pygame.transform.scale(self.shotsleft,(self.shotsleft.get_width()//3,self.shotsleft.get_height()//3))

       self.rect = pygame.Rect(x,y,height,width)
       self.health = 10
       self.hitbox = (self.x + -20, self.y + 30, 31, 57)
   def draw(self):
       self.rect.topleft = (self.x,self.y)
       window.blit(self.shootsright,self.rect)
       self.hits = (self.x + 20, self.y, 28,60)
       pygame.draw.rect(window, (255,0,0), (self.hitbox[0], self.hitbox[1] - 60, 50, 10)) # NEW
       pygame.draw.rect(window, (0,255,0), (self.hitbox[0], self.hitbox[1] - 60, 50 - (5 * (10 - self.health)), 10))
       self.hitbox = (self.x + 60, self.y + 80, 81, 87)

black = (0,0,0)
enemyshoots1 = enemyshoot(1100,240,100,100,black)        
enemyshooting = [enemyshoots1]

这是我的完整代码
script

1 个答案:

答案 0 :(得分:0)

由于您要维护许多不同的类,因此查看脚本对我没有多大帮助。

   for shootss in shootsright:
       if shootss.x < 500 and shootss.x > 0:
           shootss.x += 7

在这里,当您更新项目符号x坐标时,您将以正的恒定值对其进行递增。

然后在子弹类中像这样更新它。

   window.blit(self.ksud,self.rect)

您可以做的是改变子弹的速度,改变其方向。您可以使用类似这样的代码

if object_you_want_to_follow.x > object_shooting_the_bullet.x:
    bulletSpeed = +7
elif object_you_want_to_follow.x < object_shooting_the_bullet.x:
    bulletSpeed = -7

如果要沿y方向垂直跟随,则同样适用。

优化技巧

据我了解,在您的项目符号类中添加此内容没有什么区别,因为您稍后将对其进行更新。

   window.blit(self.ksud, player_rect)

编辑

要使事情变得简单,您只需执行此操作

   for shootss in shootsright:
       if shootss.x < 500 and shootss.x > 0:
           if enemy.x < playerman.x:
               shootss.x += 7
           else:
               shootss.x -= 7
       else:
           shootsright.pop(shootsright.index(shootss))
   if len(shootsright) < 1:
           shootsright.append(Bools(round(enemyshoots1.x+enemyshoots1.width-107),round(enemyshoots1.y + enemyshoots1.height-50),(0,0,0)))