Untitled

 avatar
unknown
python
2 years ago
2.7 kB
7
Indexable
import pygame as pg
import math

pg.init()

class Particle(pg.sprite.Sprite):
    def __init__(self, pos, goal, radius, img_list):
        super().__init__()
        self.images = img_list
        self.imgindex = 0
        self.image = self.images[self.imgindex]
        self.rect = self.image.get_rect(center=pos + goal)
        self.goal = pg.math.Vector2(goal)
        self.radius = radius
        self.counter = 0
        self.t = 0

    def animate(self):
        self.imgindex = (self.imgindex + 1) % len(self.images)
        self.image = self.images[self.imgindex]

    def update(self):
        self.counter += 1
        if self.counter % 50 == 0:
            self.animate()
        self.t += 0.01

        alpha = 0.5
        beta = 0.5

        vx = alpha * self.rect.x - beta * self.rect.y
        vy = alpha * self.rect.x + beta * self.rect.y
        dpos = self.t * pg.Vector2(vx, vy)
        self.rect.center -= dpos
        #self.rect.center += self.goal

        d = pg.math.Vector2.distance_to(pg.math.Vector2(self.rect.center), self.goal)
        if d < 30:
            print("dead")
            #self.kill()

w, h = 1000, 700
screen = pg.display.set_mode((w, h))
clock = pg.time.Clock()
particles = pg.sprite.Group()
radius = 200
player_pos = pg.math.Vector2(w//2,h//2)

def colorize(img_list, color):
    colorimg = pg.Surface(img_list[0].get_size()).convert_alpha()
    colorimg.fill(color)
    for img in img_list:
        img.blit(colorimg,(0,0), special_flags = pg.BLEND_RGBA_MULT)

particle_imgs = [
    pg.image.load("test.png").convert_alpha(),
    pg.image.load("test2.png").convert_alpha(),
]
colorize(particle_imgs, (255,255,0))

chr_imgs = [
    pg.image.load("char.png").convert_alpha(),
]
chr_index = 0
chr_rect = chr_imgs[0].get_rect(center = (w//2, h//2))

is_running = True
add_particles = False

while is_running:
    screen.fill((15,100,10))
    screen.blit(chr_imgs[chr_index], chr_rect.topleft)

    for event in pg.event.get():
        if event.type == pg.QUIT:
            is_running = False
        if event.type == pg.KEYDOWN:
            add_particles = True

    if add_particles:
        add_particles = False
        particle_num = 10
        anglediff = math.pi*2 / particle_num
        theta = 0
        for i in range(particle_num):
            theta += anglediff
            particles.add(
                Particle(
                    radius * pg.Vector2(math.cos(theta), math.sin(theta)), 
                    (w//2,h//2), 
                    radius, 
                    particle_imgs
                )
            )

    particles.update()
    particles.draw(screen)

    clock.tick(10)
    pg.display.flip()

pg.quit()
Editor is loading...