让敌人射击玩家

huangapple go评论94阅读模式
英文:

Make enemies shoot the player

问题

现在敌人已经随机移动并瞄准玩家了,现在我想让敌人射击玩家,但我不知道为什么子弹会随机飞射,正如您在下面的GIF中所看到的(按左Ctrl键射击)。

以下是玩家类(Player class):

  1. class Player:
  2. # 初始化玩家
  3. def __init__(self, x, y, player):
  4. self.x = x
  5. self.y = y
  6. self.image = first_player_spaceship_image
  7. self.life = 6
  8. self.original_player_image = first_player_spaceship_image
  9. self.angle = 0
  10. self.rect = self.image.get_rect()
  11. # 移动玩家
  12. def movePlayer(self):
  13. # ...(此处省略了一些代码,用于处理玩家移动)
  14. # 旋转玩家以瞄准鼠标指向的方向
  15. def rotate(self):
  16. # ...(此处省略了一些代码,用于旋转玩家角度)
  17. # 绘制玩家
  18. def drawPlayer(self):
  19. screen.blit(self.image, self.rect.topleft)

这是我的敌人类(Enemy class):

  1. class ShootingEnemy:
  2. # 初始化敌人
  3. def __init__(self):
  4. # ...(此处省略了一些代码,用于初始化敌人)
  5. # 继续移动射击敌人
  6. def continueMoveShootingEnemy(self):
  7. # ...(此处省略了一些代码,用于移动敌人)
  8. # 旋转射击敌人以瞄准玩家
  9. def rotate(self):
  10. # ...(此处省略了一些代码,用于旋转敌人角度)
  11. def drawShootingEnemies(self):
  12. screen.blit(self.image, (self.x, self.y))

这是我的子弹类(Bullet class):

  1. class BulletEnemyShooting:
  2. # 初始化敌人射击的子弹
  3. def __init__(self, shooting_enemy):
  4. # ...(此处省略了一些代码,用于初始化子弹)
  5. # 移动子弹
  6. def MoveBulletEnemyShooting(self):
  7. # ...(此处省略了一些代码,用于移动子弹)
  8. def drawBulletEnemyShooting(self):
  9. screen.blit(self.image, (self.x, self.y))
  10. # 旋转子弹以适应新的角度
  11. def rotate(self):
  12. # ...(此处省略了一些代码,用于旋转子弹)

请注意,我在代码中省略了一些与移动和旋转有关的代码,以确保尽可能保持清晰度。如果您希望进一步了解如何实现子弹的正确射击,请检查与子弹移动和旋转相关的代码部分,以确保它们按照您的预期进行。

英文:

I have made the enemies to move randomly and look at the player, now I would like to get the enemies to shoot the player, I do not know why but the shots go totally random, as you can see in the gif below(you shoot by pressing left ctrl).

Anyway this is the Player class:

  1. class Player:
  2. # inizialize the player
  3. def __init__(self, x, y, player):
  4. self.x = x
  5. self.y = y
  6. self.image = first_player_spaceship_image
  7. self.life = 6
  8. self.original_player_image = first_player_spaceship_image
  9. self.angle = 0
  10. self.rect = self.image.get_rect()
  11. # move the player
  12. def movePlayer(self):
  13. if key[0]:
  14. self.x -= 10
  15. elif key[1]:
  16. self.x += 10
  17. if key[2]:
  18. self.y -= 10
  19. elif key[3]:
  20. self.y += 10
  21. # check borders
  22. if self.x <= 0:
  23. self.x = 0
  24. if self.x + shooting_enemy_image.get_width() >= display_width:
  25. self.x = display_width - first_player_spaceship_image.get_width()
  26. if self.y <= 0:
  27. self.y = 0
  28. if self.y + first_player_spaceship_image.get_height() >= display_height:
  29. self.y = display_height - first_player_spaceship_image.get_height()
  30. # rotate the player where the mouse is aiming
  31. def rotate(self):
  32. mouse_x, mouse_y = pygame.mouse.get_pos()
  33. rel_x, rel_y = mouse_x - self.x, mouse_y - self.y
  34. self.angle = (180 / math.pi) * -math.atan2(rel_y, rel_x) - 90
  35. self.image = pygame.transform.rotate(self.original_player_image, int(self.angle))
  36. orig_center = self.original_player_image.get_rect(topleft=(self.x, self.y)).center
  37. self.rect = self.image.get_rect(center=orig_center)
  38. # draw the player
  39. def drawPlayer(self):
  40. screen.blit(self.image, self.rect.topleft)

This is my Enemy class:

  1. class ShootingEnemy:
  2. # inizialize the enemy
  3. def __init__(self):
  4. self.image = shooting_enemy_image
  5. self.new_shooting_enemy_image = shooting_enemy_image
  6. self.x = display_width // 2
  7. self.y = 0
  8. self.begin_down = True
  9. self.choices = 0
  10. self.timer = 51
  11. self.left_again = False
  12. self.right_again = False
  13. self.up_again = False
  14. self.down_again = False
  15. self.rect = shooting_enemy_image.get_rect()
  16. self.angle = 0
  17. # draw the shooting enemy
  18. def continueMoveShootingEnemy(self):
  19. if self.y < 30:
  20. self.y += 1
  21. if self.y == 30:
  22. self.begin_down = False
  23. self.y += 1
  24. else:
  25. if not self.begin_down and self.timer > 20:
  26. self.choices = random.choice([1, 2, 3, 4])
  27. self.timer = 0
  28. self.timer += 1
  29. if self.left_again:
  30. self.x += 1
  31. self.left_again = False
  32. if self.right_again:
  33. self.x -= 1
  34. self.right_again = False
  35. if self.up_again:
  36. self.y += 0
  37. self.up_again = False
  38. if self.down_again:
  39. self.y -= 1
  40. self.down_again = False
  41. else:
  42. if self.choices == 1:
  43. if self.x >= display_width:
  44. self.timer = 21
  45. self.right_again = True
  46. else:
  47. if self.y < 140 and self.y > 10:
  48. self.y += 1
  49. self.x += 4
  50. else:
  51. self.x += 4
  52. if self.choices == 2:
  53. if self.x <= 0:
  54. self.timer = 21
  55. self.left_again = True
  56. else:
  57. if self.y < 140 and self.y > 10:
  58. self.y += 1
  59. self.x -= 4
  60. else:
  61. self.x -= 4
  62. if self.choices == 3:
  63. if self.y >= 150:
  64. self.timer = 21
  65. self.down_again = True
  66. else:
  67. self.y += 2
  68. if self.choices == 4:
  69. if self.y <= 0:
  70. self.timer = 21
  71. self.up_again = True
  72. else:
  73. self.y -= 2
  74. # rotate the shooting enemy where the player is
  75. def rotate(self):
  76. temp_x, temp_y = player_one_istance.x - self.x, player_one_istance.y - self.y
  77. self.angle = (180 / math.pi) * -math.atan2(temp_y, temp_x) - 90
  78. self.image = pygame.transform.rotate(self.new_shooting_enemy_image, int(self.angle))
  79. orig_center = self.new_shooting_enemy_image.get_rect(topleft=(self.x, self.y)).center
  80. self.rect = self.image.get_rect(center=orig_center)
  81. def drawShootingEnemies(self):
  82. screen.blit(self.image, (self.x, self.y))

And this is my Bullet class:

  1. class BulletEnemyShooting:
  2. # initialize the bullet for the enemy shooting
  3. def __init__(self, shooting_enemy):
  4. self.x = shooting_enemy.x
  5. self.y = shooting_enemy.y
  6. self.image = laser_bullet
  7. self.original_image = laser_bullet
  8. self.angle = shooting_enemy.angle
  9. self.vel_x = math.cos(self.angle) * 45
  10. self.vel_y = math.sin(self.angle) * 45
  11. self.rect = self.image.get_rect()
  12. # draw the bullet
  13. def MoveBulletEnemyShooting(self):
  14. self.x += self.vel_x
  15. self.y += self.vel_y
  16. if self.x < -laser_bullet.get_width() or self.x >= display_width + laser_bullet.get_width() \
  17. or self.y < -laser_bullet.get_height() or self.y >= display_height + laser_bullet.get_height():
  18. enemy_shooting_bullets_list.pop(enemy_shooting_bullets_list.index(self))
  19. def drawBulletEnemyShooting(self):
  20. screen.blit(self.image, (self.x, self.y))
  21. # rotate the bullet to the new angle
  22. def rotate(self):
  23. self.image = pygame.transform.rotate(self.original_image,
  24. ((180 / math.pi) * -math.atan2(self.vel_y, self.vel_x) - 90))
  25. self.rect = self.image.get_rect()

In case you want to test the code I have eliminated unnecessary things:
https://pastebin.com/HT93hUzt

You can download replacement images to test the code here (remember to change the image load string!):

https://www.flaticon.com/free-icon/spaceship_1702089?term=spaceship&page=1&position=12

https://www.flaticon.com/free-icon/spaceship_1114531

https://www.flaticon.com/search?word=laserbeam

让敌人射击玩家

答案1

得分: 1

我自己找到了答案,对于那些感兴趣的人,我将代码放在这里:

  1. class BulletEnemyShooting:
  2. def __init__(self, temp_angle, pos, target):
  3. self.image = enemy_laser_bullet_image
  4. self.original_image = enemy_laser_bullet_image
  5. # `pos` 参数是子弹 rect 的中心。
  6. self.rect = self.image.get_rect(center=pos)
  7. self.position = Vector2(pos) # 子弹的位置。
  8. # 此向量指向从射击敌人位置到目标位置
  9. direction = target - pos
  10. # 方向向量的极坐标。
  11. radius, angle = direction.as_polar()
  12. # 通过负角度旋转图像(因为y轴翻转了)。
  13. self.image = pygame.transform.rotozoom(self.image, -angle, 1)
  14. # 速度是归一化的方向向量,缩放到所需的长度。
  15. self.velocity = direction.normalize() * 11
  16. def update(self):
  17. # 移动子弹
  18. self.position += self.velocity # 更新位置向量。
  19. self.rect.center = self.position # 以及 rect。
  20. def drawBulletEnemyShooting(self):
  21. screen.blit(self.image, self.rect)
  22. # 检查敌人射击子弹是否与玩家碰撞,如果是,则减少生命。
  23. def collisionBulletShootingEnemy(self, shooting_enemy, shooting_enemy_image):
  24. global score
  25. shooting_enemy_rect = pygame.Rect(shooting_enemy.x, shooting_enemy.y, shooting_enemy_image.get_width(), shooting_enemy_image.get_height())
  26. bullet_rect = pygame.Rect(self.x, self.y, laser_bullet.get_width(), laser_bullet.get_height())
  27. # 检查碰撞
  28. if shooting_enemy_rect.colliderect(bullet_rect):
  29. bullets_list.pop(bullets_list.index(self))
  30. shooting_enemy.x = 5000
  31. score += 1
  32. # 这必须在游戏的主循环中完成,因此每次都必须重新计算目标。
  33. if enemy_shooting_bool:
  34. for shooting_enemy in shooting_enemies_list:
  35. target = Vector2(player_one_istance.x, player_one_istance.y)
  36. bullet = BulletEnemyShooting(shooting_enemy.angle, (shooting_enemy.x, shooting_enemy.y), target)
  37. enemy_bullets_list.append(bullet)
  38. enemy_shooting_bool = False
  39. 我希望这对某人有所帮助
  40. <details>
  41. <summary>英文:</summary>
  42. I found the answer on my own, for those interested, I put the code here:
  43. class BulletEnemyShooting:
  44. def __init__(self, temp_angle, pos, target):
  45. self.image = enemy_laser_bullet_image
  46. self.original_image = enemy_laser_bullet_image
  47. # The `pos` parameter is the center of the bullet.rect.
  48. self.rect = self.image.get_rect(center=pos)
  49. self.position = Vector2(pos) # The position of the bullet.
  50. # This vector points from the shooting_enemy position to target position
  51. direction = target - pos
  52. # The polar coordinates of the direction vector.
  53. radius, angle = direction.as_polar()
  54. # Rotate the image by the negative angle (because the y-axis is flipped).
  55. self.image = pygame.transform.rotozoom(self.image, -angle, 1)
  56. # The velocity is the normalized direction vector scaled to the desired length.
  57. self.velocity = direction.normalize() * 11
  58. def update(self):
  59. #move the bullet
  60. self.position += self.velocity # Update the position vector.
  61. self.rect.center = self.position # And the rect.
  62. def drawBulletEnemyShooting(self):
  63. screen.blit(self.image, self.rect)
  64. #check if the enemy shooting bullet collides with player and in that case decrement the life
  65. def collisionBulletShootingEnemy(self, shooting_enemy, shooting_enemy_image):
  66. global score
  67. shooting_enemy_rect = pygame.Rect(shooting_enemy.x, shooting_enemy.y, shooting_enemy_image.get_width(), shooting_enemy_image.get_height() )
  68. bullet_rect = pygame.Rect(self.x, self.y, laser_bullet.get_width(), laser_bullet.get_height() )
  69. # check collision
  70. if shooting_enemy_rect.colliderect(bullet_rect):
  71. bullets_list.pop(bullets_list.index(self))
  72. shooting_enemy.x = 5000
  73. score += 1
  74. And this has to be done in the main loop of the game, so the target has to be recalculated each time:
  75. if enemy_shooting_bool:
  76. for shooting_enemy in shooting_enemies_list:
  77. target = Vector2(player_one_istance.x, player_one_istance.y)
  78. bullet = BulletEnemyShooting(shooting_enemy.angle, (shooting_enemy.x, shooting_enemy.y), target)
  79. enemy_bullets_list.append(bullet)
  80. enemy_shooting_bool = False
  81. I hope this can help someone!
  82. </details>

huangapple
  • 本文由 发表于 2020年1月6日 18:58:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/59610856.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定