简体   繁体   English

如何在 pygame 中让粒子跟随我的鼠标

[英]how to make particles follow my mouse in pygame

I'm trying to make sure that the particles that arise when I click my mouse follow my mouse.我试图确保单击鼠标时出现的粒子跟随鼠标。 For some reason, the particles just follow me to the top left.出于某种原因,粒子只是跟着我到左上角。 Can anyone tell me what I am doing wrong?谁能告诉我我做错了什么?

Here is my code:这是我的代码:

import pygame
import sys
import random
import math

from pygame.locals import *
pygame.init()

clock = pygame.time.Clock()
screen = pygame.display.set_mode((500,500))
particles = []
while True:
    screen.fill((0,0,0))
    for event in pygame.event.get():
        if event.type == MOUSEBUTTONDOWN:
            mx,my = pygame.mouse.get_pos()
            particles.append([[pygame.Rect(mx,my,10,10)]])
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        
    for particle in particles:
        mx,my = pygame.mouse.get_pos()
        pygame.draw.rect(screen,(255,255,255),particle[0][0])
        radians = math.atan2((particle[0][0].y - my),(particle[0][0].x -mx))
        dy1 = math.sin(radians)
        dx1 = math.cos(radians)
        particle[0][0].x -= dx1
        particle[0][0].y -= dy1
    
    pygame.display.update()
    clock.tick(60)

The issue is caused, because pygame.Rect stores integral values.问题是由于pygame.Rect存储整数值引起的。 If you add an floating point value, then the fraction part gets lost and the result is truncated.如果添加浮点值,则小数部分会丢失并且结果会被截断。round the resulting coordinates to solve the issue:将结果坐标round以解决问题:

particle[0][0].x = round(particle[0][0].x - dx1)
particle[0][0].y = round(particle[0][0].y - dy1)

Note, it is sufficient to append a pygame.Rect object to the list, rather than a list of a list of pygame.Rect :注意,append 一个pygame.Rect object 的列表就足够了,而不是一个列表的列表pygame.Rect

particles.append([[pygame.Rect(mx,my,10,10)]])

particles.append(pygame.Rect(mx,my,10,10))

Example:例子:

particles = []
while True:
    screen.fill((0,0,0))

    mx, my = pygame.mouse.get_pos()
    for event in pygame.event.get():
        if event.type == MOUSEBUTTONDOWN:
            particles.append(pygame.Rect(mx, my, 10, 10))
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        
    for particle in particles:
        pygame.draw.rect(screen, (255,255,255), particle)
        radians = math.atan2(my - particle.y, mx - particle.x)
        particle.x = round(particle.x + math.cos(radians))
        particle.y = round(particle.y + math.sin(radians))

For an even more sophisticated approach see How to make smooth movement in pygame有关更复杂的方法,请参阅如何在 pygame 中进行平滑移动

It works with a simple adjustment:它通过简单的调整即可工作:

    dy1 = math.sin(radians) * 10
    dx1 = math.cos(radians) * 10

The problem is that you try to move the particles for less than a pixel at a time which results in them not moving at all and the movement being lost.问题是您尝试一次将粒子移动不到一个像素,这导致它们根本不移动并且运动丢失。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM