S
S
shinodaaa2021-08-07 12:49:37
Python
shinodaaa, 2021-08-07 12:49:37

How to make point offset?

I wrote a code that creates stars, now I need to supplement it so that while the cycle lasts, they move into corners with each new action of the cycle, but I don’t understand how to do it

import turtle
import random as rnd
import time 

def starFILL(n, dlina):
    main.begin_fill()
    if n % 2 != 0:
     for i in range(n):
          main.forward(dlina)
          angle = n // 2 * 360 / n
          main.left(angle)
    main.end_fill()

window = turtle.Screen()
window.bgcolor('black')
window.setup(1920, 1080)

main = turtle.Turtle()
main.speed(0)
main.color('yellow')
for i in range(100000):
    x = rnd.randint(-1500, 1500)
    y = rnd.randint(-700, 1080)
    main.up()
    main.setposition(x + 5,y + 5)
    main.down()
    size = rnd.randint(5, 20)
    vershina = rnd.choice([5,7,9,11,13,15,17])
    starFILL(vershina, size)   
time.sleep(5)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vindicar, 2021-08-07
@Vindicar

If you want to make an "explosion", i.e. scattering of stars from the center, then you will need to intelligently present information about the stars. Now you do not store them at all, so the stars are drawn differently for you every time.
What do you need to know to fly?
1. Coordinates of the center from where the expansion is coming from. I guess it will be
center = (0, 0)
I hope you know what a tuple is.
2. For each star, you need to know the number of its rays. Also, for each star, you need to determine the step in x and y. For example, yes.
dx = rnd.randint(-5, 5)
dy = rnd.randint(-5, 5)
n_beam = rnd.choice([5,7,9,11,13,15,17])
size = rnd.randint( 5, 20)
star = (dx, dy, n_beam, size)
You will have a list of many such star tuples.
4. We need to know how many steps we have taken since the animation started.
step_count = 0
Initially 0, then it will increase.
First you generate stars.

center = (0, 0)
N = 1000
stars = []
for i in range(N):
    dx = rnd.randint(-5, 5) #на сколько пикселей по горизонтали сдвинется звезда за шаг
    dy = rnd.randint(-5, 5) #на сколько пикселей по вертикали сдвинется звезда за шаг
    n_beam = rnd.choice([5,7,9,11,13,15,17]) #сколько лучей у звезды
    size = rnd.randint(5, 20) #размер звезды
    star = (dx, dy, n_beam, size) #описание звезды
    stars.append(star) #добавляем звезду в массив звёзд

Then do the animation loop:
for step in range(100): #сколько кадров анимации сделать
    turtle.clear() #тут нужно очистить экран
    for star in stars: #перебираем звезды
        # определяем координаты центра звезды на этом шаге
        pos_x = center[0] + step * star[0]
        pos_y = center[1] + step * star[1]
        # позиционируем черепашку
        main.up()
        main.setposition(x + 5,y + 5)
        main.down()
        # рисуем звезду
        starFILL(star[1], star[2])
    time.sleep(1.0)

Something like this. It would be more accurate to use not tuples, but classes. If you're interested, write in the comments and I'll explain.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question