Угол фейерверка Пайтон
Мне нужно реализовать что бы салют летел под каким-то углом, не под прямым углом. Не знаю как это реализовать. Буду рад Вашей помощи.
Код:
import pygame
import random
import math
pygame.init()
screen = pygame.display.set_mode((800, 800))
pygame.display.set_caption("Button!")
main_font = pygame.font.SysFont("cambria", 30)
display = pygame.display.set_mode((1000, 600))
clock = pygame.time.Clock()
FPS = 100
class Button():
def __init__(self, image, x_pos, y_pos, text_input):
self.image = image
self.x_pos = x_pos
self.y_pos = y_pos
self.rect = self.image.get_rect(center=(self.x_pos, self.y_pos))
self.text_input = text_input
self.text = main_font.render(self.text_input, True, (10, 5, 6))
self.text_rect = self.text.get_rect(center=(self.x_pos, self.y_pos))
def update(self):
screen.blit(self.image, self.rect)
screen.blit(self.text, self.text_rect)
def checkForInput(self, position):
if position[0] in range(self.rect.left, self.rect.right) and position[1] in range(self.rect.top, self.rect.bottom):
#print("Button Press!")
game2()
def checkForInput2(self, position):
if position[0] in range(self.rect.left, self.rect.right) and position[1] in range(self.rect.top, self.rect.bottom):
#print("Button Press!")
game()
def changeColor(self, position):
if position[0] in range(self.rect.left, self.rect.right) and position[1] in range(self.rect.top, self.rect.bottom):
self.text = main_font.render(self.text_input, True, (64, 15, 120))
else:
self.text = main_font.render(self.text_input, True, (98, 74, 53))
button_surface = pygame.image.load("button.png")
button_surface = pygame.transform.scale(button_surface, (150, 50))
button = Button(button_surface, 500, 500, "ФИГУРА")
button2 = Button(button_surface, 500, 500, "УГОЛ")
def choose_random_color():
r = random.randint(0, 255)
g = random.randint(0, 255)
b = random.randint(0, 255)
return r, g, b
class Circles():
def __init__(self, r, x, y):
self.color = choose_random_color()
self.radius = r
self.x = x
self.y = y
angle = random.uniform(-60, 360) # угол
velocity_mag = random.uniform(.3, 4) # радиус разброса
self.vx = velocity_mag * math.cos(math.radians(angle))
self.vy = -velocity_mag * math.sin(math.radians(angle))
self.timer = 0
self.ended = False
def move(self):
self.x += self.vx
self.y += self.vy
self.timer += 1
if self.timer >= 100: # вермя действия частиц
self.ended = True
def draw(self):
circleX = self.x
circleY = self.y
radius = self.radius
pygame.draw.circle(display, self.color, (circleX, circleY), radius-10)
class Streak():
def __init__(self, x, y):
self.color = choose_random_color()
self.x = x
self.y = y
angle = random.uniform(-120, 270) # угол
velocity_mag = random.uniform(.3, 4) # радиус разброса
self.vx = velocity_mag * math.cos(math.radians(angle))
self.vy = -velocity_mag * math.sin(math.radians(angle))
self.timer = 0
self.ended = False
def get_angle(self):
return math.atan2(-self.vy, self.vx)
def move(self):
self.x += self.vx
self.y += self.vy
self.timer += 1
if self.timer >= 60: # вермя действия частиц
self.ended = True
def draw(self):
angle = self.get_angle()
length = 1
dx = length * math.cos(angle)
dy = length * math.sin(angle)
a = [int(self.x + dy), int(self.y - dx)]
b = [int(self.x - dy), int(self.y + dx)]
pygame.draw.line(display, self.color, a, b, 1)
class Firework():
def __init__(self):
self.x = random.randint(0, 1000)
self.y = 600
self.velocity = random.uniform(3.5, 7) # скорость
self.end_y = random.uniform(10, 300) # высота полёта
self.ended = False
def move(self):
self.y -= self.velocity
if self.y <= self.end_y:
self.ended = True
def draw(self):
a = [self.x, int(self.y + 15)] # длина феерверка
b = [self.x, int(self.y - 15)]
color = [random.randint(0, 255) for _ in range(3)]
pygame.draw.line(display, color, a, b, 4)
def game():
fireworks = [Firework()]
streaks = []
a = True
while a:
for event in pygame.event.get():
if event.type == pygame.QUIT:
return
if event.type == pygame.MOUSEBUTTONDOWN:
a = False
button.checkForInput(pygame.mouse.get_pos())
if random.uniform(0, 1) <= 1 / 60:
fireworks.append(Firework())
screen.fill("white")
button.update()
button.changeColor(pygame.mouse.get_pos())
#display.fill((255, 192, 203))
for firework in fireworks:
firework.move()
firework.draw()
if firework.ended:
streaks += [Circles(random.randint(2, 20), firework.x, firework.y) for i in range(random.randint(20, 700))] # количество частиц
fireworks.remove(firework)
for streak in streaks:
streak.move()
streak.draw()
if streak.ended:
streaks.remove(streak)
pygame.display.update()
clock.tick(FPS)
def game2():
fireworks = [Firework()]
streaks = []
a = True
while a:
for event in pygame.event.get():
if event.type == pygame.QUIT:
return
if event.type == pygame.MOUSEBUTTONDOWN:
a = False
button.checkForInput2(pygame.mouse.get_pos())
button2.checkForInput2(pygame.mouse.get_pos())
if random.uniform(0, 1) <= 1 / 60:
fireworks.append(Firework())
screen.fill("white")
button.update()
button.changeColor(pygame.mouse.get_pos())
for firework in fireworks:
firework.move()
firework.draw()
if firework.ended:
streaks += [Streak(firework.x, firework.y) for i in range(random.randint(20, 700))]#количество частиц
fireworks.remove(firework)
for streak in streaks:
streak.move()
streak.draw()
if streak.ended:
streaks.remove(streak)
pygame.display.update()
clock.tick(FPS)
game()
pygame.quit()