【问题标题】:How Can I Make My Enemys Projectile Attack The Player Where Ever The Player Moves?我怎样才能让我的敌人投射攻击玩家无论玩家移动到哪里?
【发布时间】:2020-06-09 02:46:43
【问题描述】:

我有一个敌人会发射弹丸,但只向右侧射击我希望它向任何位置的玩家射击带我把如何做的步骤告诉我,那太好了,谢谢

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

   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

【问题讨论】:

  • 你有很多行代码,你应该试着清理一下,例如,你可以将每个精灵/对象放在一个列表中,所以当你想要滚动时,你只需要循环遍历一个列表而不是所有这些循环,对于绘图也是如此。您也可以尝试创建一个大师班,然后在该班的基础上构建其他课程,您也有很多空白行,您可能会减少很多行

标签: python pygame


【解决方案1】:

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

   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)))

【讨论】:

  • 我是不是做错了什么image 好像子弹根本不动object shooting the bullets 是什么意思@ 你是在说我说的那部分for shootss in shootsright有一个可以发射子弹的物体我刚刚让子弹站在敌人射击枪旁边
  • object_shooting_bullets 是您游戏中的敌人。无论如何,您所做的似乎只有一个问题是正确的。您显示的代码中似乎存在缩进问题。您的 elifelse 语句应该缩进。
  • 它跟随玩家,但它不像vid,就像在玩家所在的地方射击并且它只是悬停在它的头上
  • 是的,很抱歉。看看我对 if 条件所做的更改。您应该将 shootss.x 更改为 enemy.x。那是因为你应该根据敌人相对于玩家的位置来决定子弹的方向。
  • 它工作了,但它仍然没有攻击它上面的玩家,它看起来只是在移动它的 x 轴vid
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-29
  • 1970-01-01
  • 2021-04-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多