Skip to content

Instantly share code, notes, and snippets.

@edo0xff
Created April 1, 2020 00:51
Show Gist options
  • Save edo0xff/45ee2191dffa1233e42dfd766dc6fde0 to your computer and use it in GitHub Desktop.
Save edo0xff/45ee2191dffa1233e42dfd766dc6fde0 to your computer and use it in GitHub Desktop.
import sys
import math
import pygame
import random
COLOR_DE_FONDO = (0,0,0)
N_PARTICULAS_MIN = 50
N_PARTICULAS_MAX = 100
def colorRandom():
return (random.randint(0,255), random.randint(0,255), random.randint(0,255))
# calcula el angulo entre 2 vectores
def angleBetwenPoints(vector_1, vector_2):
x1 = float(vector_1[0])
x2 = float(vector_2[0])
y1 = float(vector_1[1])
y2 = float(vector_2[1])
m = (y2 - y1)/(x2 - x1)
a = math.degrees(math.atan(m))
if a < 0:
a = 90+((-90-a)*-1)
return math.radians(a)
def coordenadasToPygame(x,y):
return x+350, 700-y
def pygameToCoordenadas(x,y):
return x-350, 700-y
class Particula():
def __init__(self, x, y, alpha, life, v0):
self.alpha = alpha
self.alpha2 = math.radians(random.randrange(0,360))
self.t = 0
self.t_explosion = 0
self.x, self.y = x,y
self.initial_x, self.initial_y = x, y
self.a = (math.cos(self.alpha) + random.randrange(-5,5), math.cos(self.alpha) + random.randrange(-5,5))
self.life = random.randint(3, 6)
self.proyectil_life = life
self.width = 3
self.height = 3
self.x_line, self.y_line = 0, 50
self.init_x_line, self.init_y_line = 0, 50
# ecuaciones mamalonas
self.v0 = v0
self.v1 = random.randrange(17, 20)
self.g = 9.8
def mover(self, display, t):
if self.proyectil_life > 0:
x, y = coordenadasToPygame(self.x_line, self.y_line)
pygame.draw.rect(display, colorRandom(), [x, y, 3, 3])
self.x_line = self.init_x_line + (self.v0 * math.cos(self.alpha) * self.t)
self.y_line = self.init_y_line + ((self.v0 * math.sin(self.alpha) * self.t) - ((self.g*self.t**2)/2.0))
self.x = self.x_line
self.y = self.y_line
self.initial_x = self.x
self.initial_y = self.y
else:
x, y = coordenadasToPygame(self.x, self.y)
pygame.draw.rect(display, colorRandom(), [x, y, self.width, self.height])
self.x = self.initial_x + (((self.v0 * math.cos(self.alpha)) + (self.v1 * math.cos(self.alpha2)))*self.t_explosion)
self.y = self.initial_y + ((self.v1 * math.sin(self.alpha2) * self.t_explosion) - ((self.g*self.t_explosion**2)/2.0))
def crearParticulas(x,y):
n_particulas = random.randint(N_PARTICULAS_MIN, N_PARTICULAS_MAX)
lista = []
x,y = pygameToCoordenadas(x,y)
angulo = angleBetwenPoints([0,50], [x,y])
life = random.randint(7, 15)
v0 = random.randrange(80, 100)
for i in range(n_particulas):
particula = Particula(x,y, angulo, life, v0)
lista.append(particula)
return lista
pygame.init()
display_width = 700
display_heigth = 700
display = pygame.display.set_mode([display_width, display_heigth])
pygame.display.set_caption("Simulacion de fuegos artificiales")
clock = pygame.time.Clock()
fps = 60
lista_de_particulas = []
t = 0
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
x, y = event.pos
particulas = crearParticulas(x, y)
lista_de_particulas.append(particulas)
display.fill(COLOR_DE_FONDO)
x,y = coordenadasToPygame(0,50)
pygame.draw.rect(display, colorRandom(), [x, y, 4, 4])
i = 0
for particulas in lista_de_particulas:
j = 0
if particulas[0].proyectil_life > 0:
x, y = coordenadasToPygame(particulas[0].x_line, particulas[0].y_line)
myfont = pygame.font.SysFont("monospace", 15)
label = myfont.render("%im" %(particulas[0].y_line), 1, colorRandom())
display.blit(label, (x+10, y+5))
for particula in particulas:
particula.mover(display, t)
if particula.proyectil_life < 0:
particula.t_explosion += 1.0/fps
particula.life -= 0.03
particula.proyectil_life -= 0.03
particula.t += 1.0/fps
if particula.x > display_width or particula.y > display_heigth or particula.a[0] == 0 or particula.a[1] == 0 or particula.life <= 0:
lista_de_particulas[i].pop(j)
j += 1
if len(particulas) == 0:
lista_de_particulas.pop(i)
i += 1
t += 1.0/fps
pygame.display.flip()
clock.tick(fps)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment