Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3592604
  • 博文数量: 365
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 2522
  • 用 户 组: 普通用户
  • 注册时间: 2019-10-28 13:40
文章分类

全部博文(365)

文章存档

2023年(8)

2022年(130)

2021年(155)

2020年(50)

2019年(22)

我的朋友

分类: Python/Ruby

2023-01-17 17:05:54

class Particle:

    def __init__(self, x, y, firework, colour):

        self.firework = firework

        self.pos = vector(x, y)

        self.origin = vector(x, y)

        self.radius = 20

        self.remove = False

        self.explosion_radius = randint(5, 18)

        self.life = 0

        self.acc = vector(0, 0)

        # trail variables

        self.trails = []  # stores the particles trail objects

        self.prev_posx = [-10] * 10  # stores the 10 last positions

        self.prev_posy = [-10] * 10  # stores the 10 last positions

        if self.firework:

            self.vel = vector(0, -randint(17, 20))

            self.size = 5

            self.colour = colour

            for i in range(5):

                self.trails.append(Trail(i, self.size, True))

        else:

            self.vel = vector(uniform(-1, 1), uniform(-1, 1))

            self.vel.x *= randint(7, self.explosion_radius + 2)

            self.vel.y *= randint(7, self.explosion_radius + 2)

            # 向量

            self.size = randint(2, 4)

            self.colour = choice(colour)

            # 5 tails总计

            for i in range(5):

                self.trails.append(Trail(i, self.size, False))

    def apply_force(self, force):

        self.acc += force

    def move(self):

        if not self.firework:

            self.vel.x *= 0.8

            self.vel.y *= 0.8

        self.vel += self.acc

        self.pos += self.vel

        self.acc *= 0

        if self.life == 0 and not self.firework:  # check if particle is outside explosion radius

            distance = 外汇跟单gendan5.commath.sqrt((self.pos.x - self.origin.x) ** 2 + (self.pos.y - self.origin.y) ** 2)

            if distance > self.explosion_radius:

                self.remove = True

        self.decay()

        self.trail_update()

        self.life += 1

    def show(self, win):

        pygame.draw.circle(win, (self.colour[0], self.colour[1], self.colour[2], 0), (int(self.pos.x), int(self.pos.y)),

                           self.size)

    def decay(self):  # random decay of the particles

        if 50 > self.life > 10:  # early stage their is a small chance of decay

            ran = randint(0, 30)

            if ran == 0:

                self.remove = True

        elif self.life > 50:

            ran = randint(0, 5)

            if ran == 0:

                self.remove = True

阅读(887) | 评论(0) | 转发(0) |
0

上一篇:Python 万能代码模版:爬虫代码篇

下一篇:没有了

给主人留下些什么吧!~~