用Pygame绘制两点间的连线

6

我试图制作类似于烟花的东西。我创建了一个粒子类,它将组成烟花效果。

  class Particle:
        def __init__(self, pos, angle):
            self.pos = pos
            self.angle = angle
            self.color = choice([(217, 103, 51), (238, 95, 30)])
            self.radius = uniform(2, 7)
            self.pull = 0
            self.start = time.time()
    
        def adjust(self):
            self.radius -= 0.03
    
        def draw(self):
            if self.radius > 0:
                pygame.draw.circle(D, self.color, (int(self.pos[0])
                                               , int(self.pos[1])), int(self.radius))
    
        def move(self):
            now  = time.time()
            self.pos[0] += cos(radians(self.angle)) * 2
            self.pos[1] += (sin(radians(self.angle)) + self.pull) * 2
            if now - self.start > 0.1:
                self.pull += 0.25
                self.start = now

然后我创建了一个Fireworks类,它可以从0到360度发射粒子。

class FireWorks:
    def __init__(self):
        self.particles = []
        for i in range(360):
            self.particles.append(Particle([600, 300], i))

    def explode(self):
        for i in range(len(self.particles)):
            self.particles[i].draw()
            self.particles[i].move()
            self.particles[i].adjust()
        for p in self.particles:
            if p.radius < 0:
                self.particles.remove(p)

现在我想要从粒子生成的位置(600,300)开始画一条线,沿着粒子运动的路径。但问题在于粒子不会直线移动。为了使它看起来更加自然,我对y值进行了如下处理:self.pos[1] += (sin(radians(self.angle)) + self.pull) * 2。每0.1秒,self.pull 的值增加0.25。我尝试过的一种方法是,在y值增加时存储该位置值,并在这些位置之间绘制线条,以形成曲线,但根本没有绘制任何东西,并导致延迟。以下是仅涉及生成点和在点之间绘制线条的代码示例:

class Particle:
    def __init__(self, pos, angle):
        self.points = [] #added list to init to hold the points between which lines need  to be drawn

    def move(self):
       # In move method, every time a value is added to y, we record position at that point
       if now - self.start > 0.1:
          self.points.append(self.pos)
          self.pull += 0.25
    
    def draw(self):
        # Iterate through the points in the list and draw a line between them
        for i in range(len(self.points)):
            for j in range(1, len(self.points)):
                pygame.draw.line(D, self.color, (int(self.points[i][0]), int(self.points[i][1]))
                                 , (int(self.points[j][0]), int(self.points[j][1])), int(self.radius))

以下是完整的代码供参考。

import pygame
from math import radians, sin, cos
from random import choice, uniform, randint
import time

pygame.init()

WIN = pygame.display
D = WIN.set_mode((1200, 600))

class Particle:
    def __init__(self, pos, angle):
        self.pos = pos
        self.angle = angle
        self.color = choice([(217, 103, 51), (238, 95, 30)])
        self.radius = uniform(2, 7)
        self.pull = 0
        self.start = time.time()
        self.points = []

    def adjust(self):
        self.radius -= 0.03

    def draw(self):
        if self.radius > 0:
            pygame.draw.circle(D, self.color, (int(self.pos[0])
                                           , int(self.pos[1])), int(self.radius))
            for i in range(len(self.points)):
                for j in range(1, len(self.points)):
                    pygame.draw.line(D, self.color, (int(self.points[i][0]), int(self.points[i][1]))
                                     , (int(self.points[j][0]), int(self.points[j][1])), int(self.radius))

    def move(self):
        now  = time.time()
        self.pos[0] += cos(radians(self.angle)) * 2
        self.pos[1] += (sin(radians(self.angle)) + self.pull) * 2
        if now - self.start > 0.1:
            self.points.append(self.pos)
            self.pull += 0.25
            self.start = now


class FireWorks:
    def __init__(self):
        self.particles = []
        for i in range(360):
            self.particles.append(Particle([600, 300], i))

    def explode(self):
        for i in range(len(self.particles)):
            self.particles[i].draw()
            self.particles[i].move()
            self.particles[i].adjust()
        for p in self.particles:
            if p.radius < 0:
                self.particles.remove(p)
            

f = FireWorks()
D.fill((0, 0, 0))
while True:
    pygame.event.get()
    D.fill((0, 0, 0))
    f.explode()
    WIN.flip()
    

 
1个回答

3
你需要将位置元组的副本附加到位置列表中,而不是引用位置: self.points.append(self.pos) -> self.points.append(self.pos.copy())
self.points.append(self.pos[:])

注意,self.pos 指的是一个包含2个元素的元组。因此, self.points.append(self.pos) 将该位置的新引用添加到列表中,但不会生成一个新的位置。
为了提高性能,建议将整数位置添加到列表中,并使用 pygame.draw.lines() 沿着粒子路径画出线条。
class Particle:
    # [...]

    def draw(self):
        if self.radius > 0:
            pygame.draw.circle(D, self.color, (int(self.pos[0])
                                           , int(self.pos[1])), int(self.radius))
            if len(self.points) > 1:
                pygame.draw.lines(D, self.color, False, self.points)

    def move(self):
        now  = time.time()
        self.pos[0] += cos(radians(self.angle)) * 2
        self.pos[1] += (sin(radians(self.angle)) + self.pull) * 2
        if now - self.start > 0.1:
            x, y = round(self.pos[0]), round(self.pos[1])
            self.points.append((x, y))
            self.pull += 0.25
            self.start = now

谢谢,它起作用了。但它仍然会导致延迟,并且不是我预期的样子。有什么改进的想法吗?再次感谢。 - user12291970
1
@hippozhipos,我不明白为什么你要在嵌套循环中沿粒子路径绘制线条。不过,我已经扩展了答案。 - Rabbid76

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接