[英]How to make bullet follow mouse
我正在为一个项目制作游戏,但是我几乎没有学到甚至可以制作一个简单的游戏,因此即使此处存在类似的问题,我也不确定如何进行更改以适合我的游戏。
我确实设法制造了子弹,但它只能向上射击,而无论鼠标在哪里我都需要它射击。
我尝试按照此处的一些答案进行操作,但它会显示错误消息,如果我尝试编辑这些错误消息,则会弹出更多错误消息,我真的不知道为什么。
这是我到目前为止的内容:
Code is removed for now. Will re-upload in 1 to 2 months.
run = True
while run:
[...]
elif event.type == pygame.MOUSEBUTTONDOWN:
bullet = Bullet()
bullet.rect.x = player.rect.x
bullet.rect.y = player.rect.y
all_sprite_list.add(bullet)
这是我的整个代码,如果有帮助的话:
Code is removed for now. Will re-upload in 1 to 2 months.
我非常痛苦地意识到该游戏存在严重缺陷,即使与该问题无关,我也将不胜感激。 (例如,子弹与敌人的碰撞检测。玩家与敌人的碰撞检测)但是目前,这个问题是我最大的担忧。 非常感谢您的帮助!
您可以在事件pygame.MOUSEBUTTONDOWN中获取鼠标的位置。
if event.type == pygame.MOUSEBUTTONDOWN:
aim_pos = event.pos
您可能还希望子弹遵循射击的方向。
player_position = player.rect.center
# Assume you have got where you're aiming by aim_pos.
bullet_vec = pygame.math.Vector2(aim_pos[0] - player_position[0],
aim_pos[1] - player_position[1]).normalize() * 10 #move speed
bullet = Bullet()
bullet.rect.center = player.rect.center
bullet.vec = bullet_vec
all_sprite_list.add(bullet)
并按照方向移动。
class Bullet(pygame.sprite.Sprite):
....your codes
def update(self):
self.rect.move_ip(self.vec.x, self.vec.y)
def update(self):
self.rect.y -= 3
这是控制所需功能(球移动的位置)的代码部分,但是我想您知道这是因为您编写了它。
它会显示错误消息,如果我尝试编辑这些错误消息,则会弹出更多错误消息,我真的不知道为什么。
好吧,这就是为您编程。 继续修复它们。
顺带一提,我强烈建议您安装皮棉机。 在您的代码上运行pylint
可以为我提供所有这些,这似乎是您要我们提供的反馈:
$ pylint --errors-only your-game.py
************* Module game
your-game.py:8:0: E1101: Module 'pygame' has no 'init' member (no-member)
your-game.py:30:21: E1121: Too many positional arguments for lambda call (too-many-function-args)
your-game.py:45:21: E1121: Too many positional arguments for lambda call (too-many-function-args)
your-game.py:93:21: E1121: Too many positional arguments for lambda call (too-many-function-args)
your-game.py:130:21: E1121: Too many positional arguments for lambda call (too-many-function-args)
your-game.py:299:25: E1101: Module 'pygame' has no 'QUIT' member (no-member)
your-game.py:301:25: E1101: Module 'pygame' has no 'MOUSEBUTTONDOWN' member (no-member)
your-game.py:336:25: E1101: Module 'pygame' has no 'QUIT' member (no-member)
your-game.py:339:27: E1101: Module 'pygame' has no 'KEYDOWN' member (no-member)
your-game.py:340:28: E1101: Module 'pygame' has no 'K_LEFT' member (no-member)
your-game.py:342:30: E1101: Module 'pygame' has no 'K_RIGHT' member (no-member)
your-game.py:344:30: E1101: Module 'pygame' has no 'K_UP' member (no-member)
your-game.py:346:30: E1101: Module 'pygame' has no 'K_DOWN' member (no-member)
your-game.py:349:27: E1101: Module 'pygame' has no 'KEYUP' member (no-member)
your-game.py:350:28: E1101: Module 'pygame' has no 'K_LEFT' member (no-member)
your-game.py:352:30: E1101: Module 'pygame' has no 'K_RIGHT' member (no-member)
your-game.py:354:30: E1101: Module 'pygame' has no 'K_UP' member (no-member)
your-game.py:356:30: E1101: Module 'pygame' has no 'K_DOWN' member (no-member)
your-game.py:359:27: E1101: Module 'pygame' has no 'MOUSEBUTTONDOWN' member (no-member)
your-game.py:376:0: E1101: Module 'pygame' has no 'quit' member (no-member)
另外,让我们谈谈这个:
wall = Wall(0, 0, 10, 800)
wall_list.add(wall)
all_sprite_list.add(wall)
数百行。 您可以对计算机进行编程以为您生成所有这些位置。 但是,即使您不想这样做,也不要这样重复自己。
walls = (
(0, 0, 10, 800),
(40, 40, 10, 75),
(50, 40, 190, 10),
# ...
)
for wall_coords in walls:
wall = Wall(*wall_coords)
wall_list.add(wall)
all_sprite_list.add(wall)
这就是您的144条操作完全相同的43行-如果您将代码的数量控制在可控范围内,则易于阅读和编辑。
因此,看着它,当MOUSEBUTTONDOWN
,您可以在播放器的位置创建一个新的Bullet()。 然后,当您更新项目符号时。 您将其y
坐标上移3个单位。
我建议给每个bullet
一个velocity
和target
属性。 这样您就可以“发射并忘记”每个项目符号,而不必担心每次都要手动更新它。
def __init__(self, velocity: float, target: tuple or list):
# other stuff
self.velocity = velocity
self.target = target
def update(self):
# get the angle towards the target
x_delta = self.target[0] - self.rect.x
y_delta = self.target[1] - self.rect.y
rotation = -math.atan2(y_delta, x_delta)
# move towards the target
self.rect.x += math.cos(rotation) * self.velocity
self.rect.y -= math.sin(rotation) * self.velocity
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.