如何使子弹跟随鼠标

2024-06-13 06:25:47 发布

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

我正在为一个项目制作一个游戏,但我的学习还不够,即使是一个简单的游戏,所以即使这里有类似的问题,我不确定如何改变它,以适应我的游戏。你知道吗

我确实设法制造了子弹,但它只能向上射击,而我需要它向老鼠所在的任何地方射击。你知道吗

我试图按照这里的一些答案,但它会出现错误消息,如果我试图编辑这些,甚至更多的错误消息弹出,我真的不知道为什么。你知道吗

到目前为止,我的情况是:

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.

我痛苦地意识到,这个游戏是高度缺陷,我会感谢任何帮助,我可以得到,即使它与这个问题无关。(例如,子弹与敌人的碰撞检测。碰撞检测的球员和敌人),但目前,这个问题是我最关心的。非常感谢你的帮助!你知道吗


Tags: inrectre游戏消息foris错误
3条回答

您可以在事件中获得鼠标位置pygame.鼠标按钮. 你知道吗

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

这是代码的一部分,用于控制您询问的特性(球移动的位置),但我想您知道这一点是因为您编写了它。你知道吗

it will come up with error messages and if I try to edit those, even more error messages pop up and I really don't know why.

好吧,那是给你编的程序。继续修理。你知道吗

顺便说一下,我强烈建议安装一个过梁。在您的代码上运行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)

这是43行到144行完全相同的操作—如果您控制代码的绝对数量,那么阅读和编辑就容易多了。你知道吗

所以,看看它,当MOUSEBUTTONDOWN,你在播放器的位置创建一个新的Bullet()。然后,当你更新子弹的时候。将其y坐标上移3个单位。你知道吗

我建议给每个bullet一个velocitytarget属性。 这将允许你“发射并忘记”每一颗子弹,而不必担心 每次手动更新。你知道吗

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

相关问题 更多 >