بايثون
تصميم جرافيك مع بايثون مكتبة Turtle
في Python ، تعد مكتبة Turtle طريقك لتصميم رسومات جرافيك فقط عن طريق الكود البرمجي وتعتبر مكتبة Turtle ذات تاريخ طويل. في هذه المقالة ، سوف آخذك عبر برنامج متقدم لإنشاء Turtle Graphics باستخدام لغة برمجة Python
هل تريد الحصول على المزيد من الأيضاحات عن هذه المكتبة يمكنك زيارة الرابط هذا للدخول على صفحة المكتبة.
مشروع مصمم على مكتبة Turtle
الكود البرمجي Turtle
import turtle as tu
roo = tu.Turtle() # Turtle object
wn = tu.Screen() # Screen Object
wn.bgcolor("black") # Screen Bg color
wn.title("Fractal Tree Pattern")
roo.left(90) # moving the turtle 90 degrees towards left
roo.speed(20) # setting the speed of the turtle
def draw(l): # recursive function taking length 'l' as argument
if (l < 10):
return
else:
roo.pensize(2) # Setting Pensize
roo.pencolor("yellow") # Setting Pencolor as yellow
roo.forward(l) # moving turtle forward by 'l'
roo.left(30) # moving the turtle 30 degrees towards left
draw(3 * l / 4) # drawing a fractal on the left of the turtle object 'roo' with 3/4th of its length
roo.right(60) # moving the turtle 60 degrees towards right
draw(3 * l / 4) # drawing a fractal on the right of the turtle object 'roo' with 3/4th of its length
roo.left(30) # moving the turtle 30 degrees towards left
roo.pensize(2)
roo.backward(l) # returning the turtle back to its original psition
draw(20) # drawing 20 times
roo.right(90)
roo.speed(2000)
# recursion
def draw(l):
if (l < 10):
return
else:
roo.pensize(2)
roo.pencolor("magenta") # magenta
roo.forward(l)
roo.left(30)
draw(3 * l / 4)
roo.right(60)
draw(3 * l / 4)
roo.left(30)
roo.pensize(2)
roo.backward(l)
draw(20)
roo.left(270)
roo.speed(2000)
# recursion
def draw(l):
if (l < 10):
return
else:
roo.pensize(2)
roo.pencolor("red") # red
roo.forward(l)
roo.left(30)
draw(3 * l / 4)
roo.right(60)
draw(3 * l / 4)
roo.left(30)
roo.pensize(2)
roo.backward(l)
draw(20)
roo.right(90)
roo.speed(2000)
# recursion
def draw(l):
if (l < 10):
return
else:
roo.pensize(2)
roo.pencolor('#FFF8DC') # white
roo.forward(l)
roo.left(30)
draw(3 * l / 4)
roo.right(60)
draw(3 * l / 4)
roo.left(30)
roo.pensize(2)
roo.backward(l)
draw(20)
########################################################
def draw(l):
if (l < 10):
return
else:
roo.pensize(3)
roo.pencolor("lightgreen") # lightgreen
roo.forward(l)
roo.left(30)
draw(4 * l / 5)
roo.right(60)
draw(4 * l / 5)
roo.left(30)
roo.pensize(3)
roo.backward(l)
draw(40)
roo.right(90)
roo.speed(2000)
# recursion
def draw(l):
if (l < 10):
return
else:
roo.pensize(3)
roo.pencolor("red") # red
roo.forward(l)
roo.left(30)
draw(4 * l / 5)
roo.right(60)
draw(4 * l / 5)
roo.left(30)
roo.pensize(3)
roo.backward(l)
draw(40)
roo.left(270)
roo.speed(2000)
# recursion
def draw(l):
if (l < 10):
return
else:
roo.pensize(3)
roo.pencolor("yellow") # yellow
roo.forward(l)
roo.left(30)
draw(4 * l / 5)
roo.right(60)
draw(4 * l / 5)
roo.left(30)
roo.pensize(3)
roo.backward(l)
draw(40)
roo.right(90)
roo.speed(2000)
# recursion
def draw(l):
if (l < 10):
return
else:
roo.pensize(3)
roo.pencolor('#FFF8DC') # white
roo.forward(l)
roo.left(30)
draw(4 * l / 5)
roo.right(60)
draw(4 * l / 5)
roo.left(30)
roo.pensize(3)
roo.backward(l)
draw(40)
########################################################
def draw(l):
if (l < 10):
return
else:
roo.pensize(2)
roo.pencolor("cyan") # cyan
roo.forward(l)
roo.left(30)
draw(6 * l / 7)
roo.right(60)
draw(6 * l / 7)
roo.left(30)
roo.pensize(2)
roo.backward(l)
draw(60)
roo.right(90)
roo.speed(2000)
# recursion
def draw(l):
if (l < 10):
return
else:
roo.pensize(2)
roo.pencolor("yellow") # yellow
roo.forward(l)
roo.left(30)
draw(6 * l / 7)
roo.right(60)
draw(6 * l / 7)
roo.left(30)
roo.pensize(2)
roo.backward(l)
draw(60)
roo.left(270)
roo.speed(2000)
# recursion
def draw(l):
if (l < 10):
return
else:
roo.pensize(2)
roo.pencolor("magenta") # magenta
roo.forward(l)
roo.left(30)
draw(6 * l / 7)
roo.right(60)
draw(6 * l / 7)
roo.left(30)
roo.pensize(2)
roo.backward(l)
draw(60)
roo.right(90)
roo.speed(2000)
# recursion
def draw(l):
if (l < 10):
return
else:
roo.pensize(2)
roo.pencolor('#FFF8DC') # white
roo.forward(l)
roo.left(30)
draw(6 * l / 7)
roo.right(60)
draw(6 * l / 7)
roo.left(30)
roo.pensize(2)
roo.backward(l)
draw(60)
wn.exitonclick()
لماذا لا يعمل الكود
قم بالتأكد من أنك قمت بتثبيت مكتبة turtle
طريقة تنزيل المكتبات على محرر بايثون
لا