91 lines
2.0 KiB
Python
91 lines
2.0 KiB
Python
import random
|
|
|
|
import pygame
|
|
|
|
from my_package.Particle_class import Particle
|
|
|
|
|
|
class Spark(Particle):
|
|
def __init__(self,x,y,color,fade_speed=4):
|
|
super().__init__(x,y,color,fade_speed)
|
|
self.y = y
|
|
self.rad = CIRCLE_RADIUS
|
|
self.image = pygame.Surface((self.rad * 2, self.rad * 2), pygame.SRCALPHA)
|
|
pygame.draw.circle(self.image, self.color, (self.rad, self.rad), self.rad)
|
|
|
|
def appear(self, screen):
|
|
if self.alpha > 0:
|
|
self.image.set_alpha(self.alpha)
|
|
screen.blit(self.image, (self.pos[0] - self.rad, self.pos[1] - self.rad))
|
|
|
|
|
|
particles = []
|
|
width = 600
|
|
height = 600
|
|
SIZE = (width, height)
|
|
WHITE = (255, 255, 255)
|
|
RED = (255, 0, 0)
|
|
BLUE = (0, 0, 255)
|
|
ORANGE = (255, 128, 0)
|
|
BLACK = (0, 0, 0)
|
|
CIRCLE_RADIUS = 3
|
|
f_x = [-0.1,0,0.1]
|
|
f_y = [-0.2,-0.1]
|
|
COLOR = (ORANGE, RED)
|
|
|
|
def main():
|
|
screen = init_game()
|
|
game_loop(screen)
|
|
exit_game()
|
|
|
|
def init_game():
|
|
global clock
|
|
clock = pygame.time.Clock()
|
|
pygame.init()
|
|
screen = pygame.display.set_mode(SIZE)
|
|
return screen
|
|
|
|
def game_loop(screen):
|
|
while True:
|
|
if not event_handling():
|
|
break
|
|
if not update_game():
|
|
break
|
|
draw_screen(screen)
|
|
|
|
|
|
def event_handling():
|
|
for event in pygame.event.get():
|
|
if event.type == pygame.QUIT:
|
|
return False
|
|
return True
|
|
|
|
def update_game():
|
|
for _ in range(2):
|
|
particles.append(Spark(300,300,random.choice(COLOR)))
|
|
|
|
for particle_object in particles:
|
|
particle_object.apply_force((random.choice(f_x), -0.1))
|
|
|
|
if particle_object.y <= 0 or particle_object.alpha <= 0:
|
|
|
|
print(particle_object)
|
|
particles.remove(particle_object)
|
|
|
|
particle_object.fade()
|
|
particle_object.update()
|
|
|
|
clock.tick(60)
|
|
return True
|
|
|
|
def draw_screen(screen):
|
|
screen.fill(BLACK)
|
|
for particle_object in particles:
|
|
particle_object.set_surface(screen)
|
|
particle_object.appear(screen)
|
|
pygame.display.flip()
|
|
|
|
def exit_game():
|
|
pygame.quit()
|
|
|
|
main() |