SERob3 (python3) - Գրաֆիկական պատկերներ turtle գրադարանի միջոցով

Post Reply
davbaghdasaryan
Posts: 101
Joined: Thu Apr 09, 2020 8:08 pm

SERob3 (python3) - Գրաֆիկական պատկերներ turtle գրադարանի միջոցով

Post by davbaghdasaryan »

Graphic figures through the Turtle library | Графические изображения с помощью библиотеки "turtle"


Փորձենք նկարել գրաֆիկական պատկերներ «python3» ծրագրավորման լեզվի միջոցով։
Պետք է մտնել անհրաժեշտ կատալոգը, որի համար գրում ենք՝ «cd projects», «cd SERob3», եւ աշխատացնելու ենք «graphics.py» ծրագիրը:
Տվյալ դեպքում օգտվելու ենք python3-ից։
Ներմուծում ենք «turtle» գրադարանը․ աշխատելու ենք մեզ ծանոթ «Կրիա»-ի ծրագրի հետ։ Անհրաժեշտ է սլաքին տալ անուն (տվյալ դեպքում կվերնագրենք «arrow»): Սահմանում ենք սլաքի արագությունը «arrow.speed(1)» հրամանով։
Նշում ենք, որ 4 անգամ սլաքը շարժվի 100 քայլ եւ պտտվի 90 աստիճան։ Ավարտում ենք գործողությունը «turtle.done()» հրամանով։
Սեղմեք այստեղ

Code: Select all

import turtle

arrow = turtle.Turtle()

arrow.speed(100)

for i in range(4):
	arrow.forward(100)
	arrow.right(90)
	
turtle.done()
Այս ծրագրով սլաքը պետք է գծի քառակուսի։ Ստուգենք արդյունքը։
Աշխատացնելու համար գրում ենք՝ «sudo python3», եւ ծրագրի անվանումը՝ «graphics.py»։ Ծրագիրը գծեց քառակուսի․ ամեն ինչ ճիշտ է։
Տերմինալի հրամաններ

Code: Select all

cd projects
cd SERob3
sudo python3 graphics.py
Տեսնել նկարը
Screen Shot 2020-05-22 at 12.22.24.png
Screen Shot 2020-05-22 at 12.22.24.png (14.71 KiB) Viewed 10472 times
Հիմա փորձենք ստեղծել առավել բարդ ու գեղեցիկ պատկեր։ Ստեղծենք եւս մեկ «j» փոփոխական։ Մեր գործողությունները կկատարվեն «j»-ի ներսում։ Ըստ ծրագրի՝ սլաքը պետք է 36 անգամ նույն վայրում գծի քառակուսի։ Որպեսզի այն պտտվի, պետք է, որ մենք «j»-ի ներսում գրենք եւս մեկ հրաման, որ սլաքը թեքվի 10 աստիճանով։
Այս ծրագրի ժամանակ 36 անգամ կգծի քառակուսի՝ ամեն անգամ 10 աստիճան թեքվելով։ Ստուգենք արդյունքը։ Տեսնում ենք ճիշտ արդյունք։
Որպեսզի շատ երկար չսպասենք, կարող ենք փոխել արագությունը «arrow.speed(100)» հրամանով։
Սեղմեք այստեղ

Code: Select all

import turtle

arrow = turtle.Turtle()

arrow.speed(100)

for j in range(36):
	for i in range(4):
		arrow.forward(100)
		arrow.right(90)
	arrow.right(10)

turtle.done()
Ահա․ մեզ անհրաժեշտ պատկերը ստացվեց։
Տեսնել նկարը
Screen Shot 2020-05-21 at 11.44.46.png
Screen Shot 2020-05-21 at 11.44.46.png (146.66 KiB) Viewed 10472 times
Հրամանները նույնն են, ինչ որ «Կրիա»-ում, սակայն հրամաններից առաջ պետք է գրել սլաքի անվանումը։
Հավելյալ նյութեր
Last edited by davbaghdasaryan on Mon Aug 02, 2021 6:01 pm, edited 2 times in total.

davbaghdasaryan
Posts: 101
Joined: Thu Apr 09, 2020 8:08 pm

Графические изображения с помощью библиотеки "turtle"

Post by davbaghdasaryan »

Попробуем нарисовать графические изображения с помощью языка программирования "python3".
Войдем в необходимый каталог. Для этого запишем "cd projects", "cd SERob3" и запустим программу "graphics.py".
В данном случае будем использовать python3.
Импортируем библиотеку "turtle". Будем работать с известной нам программой "Черепаха". Необходимо стрелке задать имя (в данном случае назовем "arrow"). Скорость стрелки определяем командой "arrow.speed(1)".
Указываем, чтобы стрелка 4 раза переместилась на 100 шагов и перевернулась на 90 градусов. Действие завершаем командой "turtle.done( )".

посмотреть

Code: Select all

import turtle

arrow = turtle.Turtle()

arrow.speed(100)

for i in range(4):
	arrow.forward(100)
	arrow.right(90)
	
turtle.done()
По этой программе стрелка должна начертить квадрат. Проверим результат.
Чтобы запустить программу, пишем "sudo python3" и название программы - "graphics.py". Программа начертила квадрат; все верно.
посмотреть

Code: Select all

cd projects
cd SERob3
sudo python3 graphics.py
посмотреть
Screen Shot 2020-05-22 at 12.22.24.png
Screen Shot 2020-05-22 at 12.22.24.png (14.71 KiB) Viewed 10319 times
Теперь попробуем создать более сложное и красивое изображение. Создадим еще одну переменную "j". Наши действия будут выполняться внутри "j". Согласно программе, стрелка должна 36 раз на одном и том же месте начертить квадрат. Чтобы он вращался, мы должны внутри "j" написать еще одну команду, чтобы стрелка наклонилась на 10 градусов.
По этой программе 36 раз будет нарисован квадрат, каждый раз с наклоном на 10 градусов. Проверим результат. Мы видим правильный результат.
Чтобы не ждать слишком долго, мы можем изменить скорость с помощью команды "arrow.speed (100)".
посмотреть

Code: Select all

import turtle

arrow = turtle.Turtle()

arrow.speed(100)

for j in range(36):
	for i in range(4):
		arrow.forward(100)
		arrow.right(90)
	arrow.right(10)

turtle.done()
Вот, мы получили необходимое нам изображение.
посмотреть
Screen Shot 2020-05-22 at 12.22.24.png
Screen Shot 2020-05-22 at 12.22.24.png (14.71 KiB) Viewed 10319 times
Команды те же самые, что и в "Կրիա", только перед командами нужно писать название стрелки.
Более подробнaя информация о библиотеке "Turtle"
Attachments
Screen Shot 2020-05-21 at 11.44.46.png
Screen Shot 2020-05-21 at 11.44.46.png (146.66 KiB) Viewed 10319 times

davbaghdasaryan
Posts: 101
Joined: Thu Apr 09, 2020 8:08 pm

Graphic figures through the Turtle library

Post by davbaghdasaryan »

Let’s try to draw graphic figures through the “python3” programming language.
You need to enter the required catalogue by typing “cd projects”, “cd SerRob3” and run the “graphics.ру” program.
We will use python 3 in this case.
Import the “turtle” library. We are going to work with a “turtle” program.
Give the arrow a name (“arrow” for this case). Define the arrow speed with “arrow.speed(1) command.

Mention that an arrow must move 100 steps for 4 times and rotate 90°.
We end the operation with the "turtle.done ()" command.
Code:

Code: Select all

import turtle

arrow = turtle.Turtle()

arrow.speed(100)

for i in range(4):
	arrow.forward(100)
	arrow.right(90)
	
turtle.done()
An arrow must line a square with this program. Let’s check the results.
In order to run the program, type “sudo python3” and the program name ”graphics.ру”. The program drew a square. It’s working properly.

Terminal:

Code: Select all

cd projects
cd SERob3
sudo python3 graphics.py
Pictures:
Screen Shot 2020-05-22 at 12.22.24.png
Screen Shot 2020-05-22 at 12.22.24.png (14.71 KiB) Viewed 10291 times
Now let's try to create a more complex and beautiful figure.
Create another “j” variable. Our operations will be performed inside the “j”. 
According to the program, the arrow should line the square 36 times in the same place. In order to rotate it we must type another command inside the “j”. This will ensure the bending of the arrow for 10 degrees.
This program will line a square for 36 times, bending for 10 degrees every time.
Let’s check the results.
 We got the results we needed.
We can change the arrow speed with “arrow.speed(100)” command to speed up the process.

Code:

Code: Select all

import turtle

arrow = turtle.Turtle()

arrow.speed(100)

for j in range(36):
	for i in range(4):
		arrow.forward(100)
		arrow.right(90)
	arrow.right(10)

turtle.done()
We have got the figure we planned.
Pictures
Screen Shot 2020-05-22 at 12.22.24.png
Screen Shot 2020-05-22 at 12.22.24.png (14.71 KiB) Viewed 10291 times
The commands are the same for the “turtle” too but we need to type the arrow name.
See more:
Attachments
Screen Shot 2020-05-21 at 11.44.46.png
Screen Shot 2020-05-21 at 11.44.46.png (146.66 KiB) Viewed 10291 times

Post Reply