Last active
December 10, 2021 08:23
-
-
Save ShashkovS/e29a3b5741d9376f849abde8e7848497 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from drawzero import * | |
from random import randint, choice, uniform | |
from dataclasses import dataclass | |
NUM_STARS = 100 | |
D = 1 | |
Vx, Vy, Vz = (500, -3, 100) | |
# Список скоростей, которые мы будем каждые 2 секунды менять | |
speed_list = [ | |
(500, 0, 0), | |
(300, -1, 0), | |
(100, -3, 0), | |
(-100, -5, 0), | |
(-300, -5, 100), | |
(-500, -5, 200), | |
(-300, -5, 400), | |
(-100, -7, 500), | |
(-50, -10, 400), | |
(50, -7, 700), | |
(200, -3, 300), | |
(400, -1, 100), | |
] | |
@dataclass | |
class Star: | |
x: float | |
y: float | |
z: float | |
r: int | |
color: tuple | |
def gen_star(): | |
'''Создать случайную звезду''' | |
x = randint(-1000000 // 2, 1000000 // 2) | |
y = randint(1, 1000) | |
z = randint(-1000000 // 2, 1000000 // 2) | |
r = 2000 | |
color = (randint(0, 255), randint(0, 255), randint(0, 255)) | |
return Star(x, y, z, r, color) | |
def create_stars(): | |
'''Создать массив звёзд''' | |
stars = [] | |
for i in range(NUM_STARS): | |
stars.append(gen_star()) | |
return stars | |
def move_stars(stars, speed): | |
'''Подвинуть все звёзды в соответствии с текущей скоростью. | |
Если звезда не помещается на экран, то вместо неё создать новую''' | |
Vx, Vy, Vz = speed | |
for i, star in enumerate(stars): | |
star.x += Vx | |
star.y += Vy | |
star.z += Vz | |
if ( | |
not 1 < star.y < 1000 | |
or not (0 < 500 + D * star.x / star.y < 1000) | |
or not (0 < 500 + D * star.x / star.z < 1000) | |
): | |
stars[i] = gen_star() | |
def draw_stars(stars): | |
'''Нарисовать все звёзды''' | |
for i, star in enumerate(stars): | |
y = star.y | |
screen_x = 500 + D * star.x / y | |
screen_y = 500 + D * star.z / y | |
screen_r = D * star.r / y | |
filled_circle(star.color, (screen_x, screen_y), screen_r) | |
step = 0 | |
stars = create_stars() | |
while True: | |
step += 1 | |
# Чистим экран | |
clear() | |
# Двигаем звёзды | |
move_stars(stars, speed_list[(step // 60) % len(speed_list)]) | |
# Рисуем звёзды | |
draw_stars(stars) | |
# Ждём 1/30 секунды | |
tick() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment