在蟒蛇游戏中,我试图让我的玩家朝鼠标的方向射击一个粒子,从玩家中心开始。
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()发布于 2020-11-24 20:28:22
不需要计算角度。只需计算归一化方向(单位向量):
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.centerx,player_rect.centery,player_rect.center,而不是player_rect.x,player_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()https://stackoverflow.com/questions/64993388
复制相似问题