问题描述
在python pygame中,我试图使我的玩家从玩家中心开始朝着鼠标的方向射击粒子
class粒子:def __init __(self,...,dx,dy,x,y):self.dx = dxself.dy = dyself.x = xself.y = yself.Rect = self.img.get_rect()def draw():self.x + = self.dxself.y + = self.dyself.Rect.centerx = self.xself.Rect.centery = self.yscreen.blit(self.img,(self.Rect.x,self.Rect.y))
theta是播放器中心与鼠标之间的夹角,以度为单位,范围为0到360,0为正x方向
dx = math.cos(theta)dy = math.sin(theta)粒子(...,dx,dy,starting_x,starting_y)
但是粒子似乎在随机方向上发射?
完整代码:
import pygame导入数学FPS = 60白=(255,255,255)红色=(255,0,0)蓝色=(0,0,255)pygame.init()屏幕= pygame.display.set_mode([300,300])时钟= pygame.time.Clock()类粒子:def __init __(self,dx,dy,x,y):self.dx = dxself.dy = dyself.x = xself.y = yself.Rect = pygame.Rect(100,150,5,5)def画图(自己,屏幕):self.x + = self.dxself.y + = self.dyself.Rect.centerx = self.xself.Rect.centery = self.ypygame.draw.rect(screen,BLUE,self.Rect)粒子= []完成=错误未完成时:screen.fill(白色)对于pygame.event.get()中的事件:如果event.type == pygame.QUIT:完成=正确如果pygame.mouse.get_pressed()[0]:mousex = pygame.mouse.get_pos()[0]mousey = pygame.mouse.get_pos()[1]theta =数学度(math.atan(player_rect.x-mousey/player_rect.y-mousex))dx = math.cos(theta)dy = math.sin(theta)particle.append(Particle(dx,dy,player_rect.x,player_rect.y))对于粒子中的粒子:粒子绘制(屏幕)player_rect = pygame.Rect(100,150,10,10)pygame.draw.rect(screen,RED,player_rect)clock.tick(FPS)pygame.display.update()pygame.quit()
无需计算角度.只需计算归一化方向(
import pygame导入数学FPS = 60白=(255,255,255)红色=(255,0,0)蓝色=(0,0,255)pygame.init()屏幕= pygame.display.set_mode([300,300])时钟= pygame.time.Clock()类粒子:def __init __(self,dx,dy,x,y):self.dx = dxself.dy = dyself.x = xself.y = yself.Rect = pygame.Rect(100,150,5,5)def画图(自己,屏幕):self.x + = self.dxself.y + = self.dyself.Rect.centerx = self.xself.Rect.centery = self.ypygame.draw.rect(screen,BLUE,self.Rect)粒子= []完成=错误未完成时:screen.fill(白色)对于pygame.event.get()中的事件:如果event.type == pygame.QUIT:完成=正确如果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.centerylen = math.hypot(dx,dy)如果len>0:particle.append(Particle(dx/len,dy/len,* player_rect.center))对于粒子中的粒子:粒子绘制(屏幕)player_rect = pygame.Rect(100,150,10,10)pygame.draw.rect(screen,RED,player_rect)pygame.display.flip()clock.tick(FPS)pygame.quit()
In python pygame I'm trying to make my player shoot a particle in the direction of the mouse, starting at the players centre
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 is the angle between the centre of the player and the mouse in degrees, ranging from 0 to 360,0 being the positive x direction
dx = math.cos(theta)
dy = math.sin(theta)
Particle(..., dx, dy, starting_x, starting_y)
However the particles seem to fire in random directions?
Full code:
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()
There is no need to compute the angle. Just compute the normalized direction (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))
The center of the rectangle is player_rect.centerx
, player_rect.centery
respectively player_rect.center
rather than player_rect.x
, player_rect.y
.
Complete example:
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()
这篇关于在python pygame中使用cos()和sin()以恒定的速度从玩家发射粒子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!