87 lines
2.1 KiB
Python
87 lines
2.1 KiB
Python
import random
|
|
|
|
import pygame
|
|
|
|
from my_package.Particle_class import Particle
|
|
|
|
|
|
class Spark(Particle):
|
|
def __init__(self, x, y, color=(255,255,255), fade_speed=20):
|
|
super().__init__(x, y, color, fade_speed)
|
|
self.x = x
|
|
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)
|
|
BLACK = (0, 0, 0)
|
|
CIRCLE_RADIUS = 2
|
|
f_x = [-0.1,0,0.1]
|
|
|
|
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():
|
|
x = pygame.mouse.get_pos()[0]
|
|
y = pygame.mouse.get_pos()[1]
|
|
for _ in range(10):
|
|
particles.append(Spark(x,y))
|
|
|
|
for particle_object in particles:
|
|
particle_object.apply_force((random.randint(-2,2),random.randint(-2,2)))
|
|
|
|
if particle_object.y <= 0 or particle_object.x <= 0 or particle_object.y == height or particle_object.x == width or particle_object.alpha <= 0:
|
|
particle_object.set_surface(None)
|
|
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() |