簡體   English   中英

在pygame中的點之間畫一條線

[英]Drawing a line between points in pygame

所以我試圖制作一些類似於煙花的東西。 我做了一個粒子類,它將組成煙花。

  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 self.pull值每 0.1 秒增加 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()
    

 

您必須將位置元組的副本附加到位置列表中,而不是對位置的引用:

self.points.append(self.pos)

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM