Лабораторные работы по Python, ЛФИ, 1 семестр
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

45 lines
1.3 KiB

from random import randint
import turtle
import math
number_of_turtles = 20
steps_of_time_number = 300
3 years ago
turtle.screensize(100, 150)
pool = [turtle.Turtle(shape='turtle') for i in range(number_of_turtles)]
for unit in pool:
unit.penup()
unit.speed(350)
unit.goto(randint(-200, 200), randint(-150, 150))
unit.left(randint(-180, 180))
3 years ago
unit.rev_x = False
unit.rev_y = False
for i in range(steps_of_time_number):
for unit in pool:
3 years ago
x, y = unit.xcor(), unit.ycor()
if abs(x) >= turtle.screensize()[0] and not unit.rev_x:
unit.seth(90 - unit.heading())
3 years ago
unit.rev_x = True
elif not abs(x) >= turtle.screensize()[0]:
unit.rev_x = False
if abs(y) >= turtle.screensize()[1] and not unit.rev_y:
unit.seth(-unit.heading())
3 years ago
unit.rev_y = True
elif abs(y) < turtle.screensize()[1]:
unit.rev_y = False
unit.forward(7)
for another_unit in pool:
if another_unit == unit:
continue
if math.hypot(another_unit.xcor() - unit.xcor(), another_unit.ycor() - unit.ycor()) < 6:
angle = randint(-180, 180)
3 years ago
unit.seth(angle)
another_unit.seth(-angle)
unit.forward(2)
another_unit.forward(2)