在pygame中向鼠标方向射击子弹


问题内容

我只是想不出为什么我的子弹不起作用。我做了一个子弹班,这是:

class Bullet:
    def __init__(self):
        self.x = player.x
        self.y = player.y
        self.height = 7
        self.width = 2
        self.bullet = pygame.Surface((self.width, self.height))
        self.bullet.fill((255, 255, 255))

现在,我在游戏类中添加了几个函数,下面是新代码:

class Game:
    def __init__(self):
        self.bullets = []

    def shoot_bullet(self):
         if self.bullets:
            for bullet in self.bullets:
                rise = mouse.y - player.y
                run = mouse.x - player.x
                angle = math.atan2(rise, run)

                bullet.x += math.cos(angle)
                bullet.y += math.sin(angle)

                pygame.transform.rotate(bullet.bullet, -math.degrees(angle))
                D.blit(bullet.bullet, (bullet.x, bullet.y))


    def generate_bullet(self):
        if  mouse.is_pressed():
            self.bullets.append(Bullet())

我期望代码执行的操作是每次按下鼠标按钮时Bullet()都会添加game.bullets一个,然后game.shoot_bullet计算播放器和鼠标之间的角度,并相应地沿鼠标方向发射子弹。但是,结果是一团糟,子弹实际上不旋转也不动。它们生成并怪异地移到屏幕的左侧。我不确定我是否弄乱了东西,或者我使用的方法是完全错误的。


问题答案:

首先,pygame.transform.rotate不变形对象本身,而是创建一个新的旋转曲面并将其返回。

如果要向特定方向发射子弹,则在发射子弹时即定义方向,但不会连续变化。发射子弹后,设置子弹的起始位置,然后将方向向量计算为鼠标位置:

self.pos = (x, y)
mx, my = pygame.mouse.get_pos()
self.dir = (mx - x, my - y)

方向向量不应取决于与鼠标的距离,而必须是Unit向量。通过除以欧几里得距离来归一化向量

length = math.hypot(*self.dir)
if length == 0.0:
    self.dir = (0, -1)
else:
    self.dir = (self.dir[0]/length, self.dir[1]/length)

计算向量的角度并旋转子弹。通常,向量的角度可以通过来计算atan2(y, x)。由于yatan2(-y, x)轴通常指向上方,因此y轴需要反转(),但是在PyGame坐标系中y轴指向下方(请参阅如何知道两点之间的角度?):

angle = math.degrees(math.atan2(-self.dir[1], self.dir[0]))

self.bullet = pygame.Surface((7, 2)).convert_alpha()
self.bullet.fill((255, 255, 255))
self.bullet = pygame.transform.rotate(self.bullet, angle)

要更新子弹的位置,只需缩放方向(按速度)并将其添加到子弹的位置即可:

self.pos = (self.pos[0]+self.dir[0]*self.speed, 
            self.pos[1]+self.dir[1]*self.speed)

要将旋转的项目符号绘制在正确的位置,请获取旋转的项目符号的边界矩形并使用设置中心点self.pos(请参阅如何使用PyGame围绕其中心旋转图像?):

bullet_rect = self.bullet.get_rect(center = self.pos)
surf.blit(self.bullet, bullet_rect)

最小示例: [![](https://i.stack.imgur.com/5jD0C.png) repl.it/@Rabbid76/PyGame- FireBulletInDirectionOfMouse](https://repl.it/@Rabbid76/PyGame- FireBulletInDirectionOfMouse#main.py)

import pygame
import math

pygame.init()
window = pygame.display.set_mode((500, 500))
clock = pygame.time.Clock()

class Bullet:
    def __init__(self, x, y):
        self.pos = (x, y)
        mx, my = pygame.mouse.get_pos()
        self.dir = (mx - x, my - y)
        length = math.hypot(*self.dir)
        if length == 0.0:
            self.dir = (0, -1)
        else:
            self.dir = (self.dir[0]/length, self.dir[1]/length)
        angle = math.degrees(math.atan2(-self.dir[1], self.dir[0]))

        self.bullet = pygame.Surface((7, 2)).convert_alpha()
        self.bullet.fill((255, 255, 255))
        self.bullet = pygame.transform.rotate(self.bullet, angle)
        self.speed = 2

    def update(self):  
        self.pos = (self.pos[0]+self.dir[0]*self.speed, 
                    self.pos[1]+self.dir[1]*self.speed)

    def draw(self, surf):
        bullet_rect = self.bullet.get_rect(center = self.pos)
        surf.blit(self.bullet, bullet_rect)

bullets = []
pos = (250, 250)
run = True
while run:
    clock.tick(60)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
        if event.type == pygame.MOUSEBUTTONDOWN:
            bullets.append(Bullet(*pos))

    for bullet in bullets[:]:
        bullet.update()
        if not window.get_rect().collidepoint(bullet.pos):
            bullets.remove(bullet)

    window.fill(0)
    pygame.draw.circle(window, (0, 255, 0), pos, 10)
    for bullet in bullets:
        bullet.draw(window)
    pygame.display.flip()