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

2024-04-04

在 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()

无需计算角度。只需计算归一化方向(单位向量 https://en.wikipedia.org/wiki/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.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()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

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

随机推荐

  • Mac OS X 10.6.7 Java 路径当前 JDK 令人困惑

    我无法理解 Mac OSX 上的多个 java 版本 实际路径与链接 通常在 Windows 中 如果我的机器上安装了多个版本 我可以选择我想要的版本的路径并使用它 但在 MAC OS X 中 我知道有一个叫做链接的东西被指向 Curren
  • .net 4.6框架已就地升级那么.net框架4.5在vs2015中如何工作?

    net 4 6框架已就地升级那么 net框架4 5在vs2015中如何工作 我只看到一个文件夹 v4 0 30319 dll 是如何存储的以及在哪里 NET Framework 4 5 是 4 0 的就地升级 4 5 1 是 4 5 的替代
  • 我可以在 Citrus 静态响应适配器负载中使用 Citrus 变量吗?

    我正在使用静态响应端点适配器返回从资源文件获取的模拟服务响应 private void setAdapterResponse StaticResponseEndpointAdapter adapter String response thr
  • 在 Python、MATLAB 等中使用 eval [重复]

    这个问题在这里已经有答案了 我确实知道不应该使用eval 出于所有显而易见的原因 性能 可维护性等 我的问题更多的是 它有合法用途吗 人们应该使用它而不是以另一种方式实现代码 由于它是用多种语言实现的 并且可能导致糟糕的编程风格 我认为它仍
  • 如何在 ggplot2 图例中使用下标 [R]

    我可以在 ggplot2 图例中使用下标吗 我懂了这个问题 https stackoverflow com questions 5293715 how to use greek symbols in ggplot2关于传说和其他地方的希腊字
  • wifi getLinkSpeed() 始终返回一个值

    我正在开发一个显示互联网速度的应用程序 喜欢 https play google com store apps details id netspeed pt https play google com store apps details
  • 同步获取 Android 上最后一个已知位置

    在 Android 上使用 LocationClient v2 API 获取最后已知位置的 正确 方法是什么 同步 manner UPDATE 这是我想出的最好的方法 它不是同步的 但它克服了处理问题的负担connect and onCon
  • 如何检查脏标志

    我想知道如果用户选择从页面导航 检查页面是否脏的最佳方法是什么 例如 有一个注册表单 用户输入他的所有信息 然后不小心点击了一个链接来进行导航 我在网上发现了这个 如果有人对任何表单输入值进行更改 它会检查页面是否脏
  • 将新的 GUID 插入 Visual Studio 2012

    是否可以创建代码片段或类似的内容来自动生成 GUID 并将其插入到 Visual Studio 2012 中的文本编辑器中 我经常需要生成新的 GUID 例如 WiX 安装程序 以及我们自己的内部框架 我曾经使用宏来执行这项工作 创建一个新
  • 在python中生成任意长度的数字升序列表

    我可以调用一个返回升序数字列表的函数吗 IE function 10 会回来 0 1 2 3 4 5 6 7 8 9 你要range https docs python org 3 library functions html func r
  • EF6(代码优先)、MVC、Unity 和没有存储库的服务层

    我的应用程序使用 SQL Server 2012 EF6 MVC 和 Web API 它还使用存储库和各种文件 例如 DatabaseFactory cs Disposable cs IDatabaseFactory cs IReposit
  • WPF 显示/隐藏带有触发器的控件

    我是 WPF 新手 我尝试创建 xaml 逻辑来根据 ViewModel 上的 AllowMiscTitle 值显示 隐藏控件 xaml 由两个字段组成 一个标准图块的组合框 先生 女士 其他 当选择 其他 时 我希望显示文本框 我创建了以
  • 如何从 Ubuntu 连接到 Windows SQL Server

    在过去的两天里 我一直在尝试连接到远程 Windows SQL 服务器 但没有明显成功 我正在尝试使用 python 连接pyodbc包裹 我尝试关注所有与此相关的博客文章 但运气不佳 我首先关注了that http onefinepub
  • JSON 解析后小部件未更新

    我有以下代码 Override public void onReceive Context context Intent intent super onReceive context intent if CLOCK WIDGET UPDAT
  • 更新条目而不更新时间戳

    我在 mysql 表中有一个属性为 ON UPDATE CURRENT TIMESTAMP 的时间戳 有没有办法在特殊情况下手动禁用更新时间戳 例如 更新条目以修改博客文章 但不重新日期 您可以在更新命令中手动将列的值设置为其当前值 UPD
  • cmake add_custom_command 具有多个输出文件的问题

    我注意到在使用 cmake 时存在一些潜在的不正确行为add custom command和多个输出文件 我已经能够将问题隔离到一个最小的示例 见下文 看来当我在我的中指定了两个输出文件时add custom command 在某些情况下
  • WPF 绑定属性 Path 和 XPath 是否互斥?

    假设我有一个UserControl whose DataContext被设置为一个具有XmlDataProvider财产 我想在控件的 XAML 中绑定到此属性 并指定一些 XPath 我试过这个
  • Telerik RadGrid - 如何设置插入时的默认数据?

    当我单击 添加记录 按钮时 我希望其中一列具有默认值 我如何在后面的代码中做到这一点 这是一个动态日期并且可以随时更改 如果该列不是GridTemplateColumn 您可以使用列的指定默认值DefaultInsertValue属性 像这
  • Java:如何对两个对应的数组进行排序?

    我有两个数组 First array 25 20 50 30 12 11 Second Array New York New Jersey Detroit Atlanta Chicago Los Angeles 第二个数组中的每两个城市对应
  • 在 python pygame 中使用 cos() 和 sin() 以恒定速度从玩家发射粒子

    在 python pygame 中 我试图让我的玩家从玩家中心开始向鼠标方向发射粒子 class Particle def init self dx dy x y self dx dx self dy dy self x x self y