在 python pygame 中使用 cos() 和 sin() 以恒定速度从玩家发射粒子

Firing particles from a player at a constant speed with cos() and sin() in python pygame

在 python pygame 中,我试图让我的玩家从玩家中心开始朝鼠标方向发射粒子

class Particle:
    def __init__(self, ..., dx, dy, x, y):
        self.dx = dx
        self.dy = dy
        self.x = x
        self.y = y
        self.Rect = self.img.get_rect()

    def draw(self):
        self.x += self.dx
        self.y += self.dy
        self.Rect.centerx = self.x
        self.Rect.centery = self.y
        screen.blit(self.img, (self.Rect.x, self.Rect.y))

theta 是玩家的中心和鼠标之间的角度,范围从 0 到 360, 0 为正 x 方向

dx = math.cos(theta) 
dy = math.sin(theta)
Particle(..., dx, dy, starting_x, starting_y)

然而粒子似乎是随机发射的?

完整代码:

import pygame
import math

FPS = 60
WHITE = (255, 255, 255)
RED = (255, 0, 0)
BLUE = (0, 0, 255)

pygame.init()

screen = pygame.display.set_mode([300, 300])

clock = pygame.time.Clock()

class Particle:
    def __init__(self, dx, dy, x, y):
        self.dx = dx
        self.dy = dy
        self.x = x
        self.y = y
        self.Rect = pygame.Rect(100, 150, 5, 5)


    def draw(self, screen):
        self.x += self.dx
        self.y += self.dy
        self.Rect.centerx = self.x
        self.Rect.centery = self.y
        pygame.draw.rect(screen, BLUE, self.Rect)


particles = []

done = False
while not done:

    screen.fill(WHITE)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True


        if pygame.mouse.get_pressed()[0]:
            mousex = pygame.mouse.get_pos()[0]
            mousey = pygame.mouse.get_pos()[1]

            theta = math.degrees(math.atan(player_rect.x - mousey/  player_rect.y - mousex))
            dx = math.cos(theta) 
            dy = math.sin(theta)
            particles.append(Particle(dx, dy, player_rect.x, player_rect.y))

    for particle in particles:
        particle.draw(screen)



    player_rect = pygame.Rect(100, 150, 10, 10)
    pygame.draw.rect(screen, RED, player_rect)
    clock.tick(FPS)
    pygame.display.update()
pygame.quit()

无需计算角度。只需计算归一化方向 (Unit vector):

dx, dy = mousex - player_rect.centerx, mousey - player_rect.centery
len = math.hypot(dx, dy)
if len > 0:
    particles.append(Particle(dx/len, dy/len, player_rect.centerx, player_rect.centery))

矩形的中心分别是player_rect.centerxplayer_rect.centery player_rect.center而不是player_rect.xplayer_rect.y.


完整示例:

import pygame
import math

FPS = 60
WHITE = (255, 255, 255)
RED = (255, 0, 0)
BLUE = (0, 0, 255)

pygame.init()
screen = pygame.display.set_mode([300, 300])
clock = pygame.time.Clock()

class Particle:
    def __init__(self, dx, dy, x, y):
        self.dx = dx
        self.dy = dy
        self.x = x
        self.y = y
        self.Rect = pygame.Rect(100, 150, 5, 5)

    def draw(self, screen):
        self.x += self.dx
        self.y += self.dy
        self.Rect.centerx = self.x
        self.Rect.centery = self.y
        pygame.draw.rect(screen, BLUE, self.Rect)

particles = []

done = False
while not done:
    screen.fill(WHITE)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True

        if pygame.mouse.get_pressed()[0]:
            mousex = pygame.mouse.get_pos()[0]
            mousey = pygame.mouse.get_pos()[1]

            dx, dy = mousex - player_rect.centerx, mousey - player_rect.centery
            len = math.hypot(dx, dy)
            if len > 0:
                particles.append(Particle(dx/len, dy/len, *player_rect.center))

    for particle in particles:
        particle.draw(screen)

    player_rect = pygame.Rect(100, 150, 10, 10)
    pygame.draw.rect(screen, RED, player_rect)
    pygame.display.flip()
    clock.tick(FPS)

pygame.quit()