Hướng dẫn python drawing code copy and paste - sao chép và dán mã bản vẽ python

Hướng dẫn và hình ảnh động của Rùa Python

Vui lòng sử dụng Google Chrome hoặc Mozilla Firefox để xem hoạt hình đúng cách.Google Chrome or Mozilla FireFox to see the animations properly.

Rùa Python là thứ phát triển từ ngôn ngữ lập trình logo, được phát minh vào năm 1966 bởi Wally Feurzig. Với sự trợ giúp của phương pháp lập trình theo định hướng đối tượng, chúng ta có thể tạo ra một bộ hoạt hình ấn tượng một cách dễ dàng.Object Oriented Programming approach, we can create an impressive set of animations easily.

Hoạt hình sau đây được tạo ra bởi Python Rùa; Mã ở dưới cùng của hướng dẫn này.

Những yêu cầu

Trước khi sử dụng rùa Python cho hoạt hình, vui lòng thực hiện các bước sau để cài đặt môi trường:

  • & nbsp; & nbsp; Tải xuống phiên bản Python mới nhất từ ​​đây.
  • & nbsp; & nbsp; Tải Python Idle - Môi trường học tập và phát triển tích hợp - từ Windows.
  • & nbsp; & nbsp; Mở một tệp mới và lưu nó với phần mở rộng .py..py extension.
  • & nbsp; & nbsp; Viết ra rùa nhập ở đầu tệp để nhập mô -đun - các lớp và phương thức.import turtle at the top of the file in order to import the module - classes and methods.
  • & nbsp; & nbsp; viết ra mã và tạo hình ảnh động ấn tượng.

Cách tốt nhất để học Rùa Python là chạy bộ mã, từ đơn giản nhất đến tiên tiến hơn dần dần, thay vì nỗ lực để hiểu đầy đủ trình giả lập lúc đầu. Đây là cách tiếp cận được áp dụng trong hướng dẫn này.

Tài liệu của Rùa Python là ở đây.

Dưới đây là một số phương pháp rùa; Họ chỉ đạo con rùa phải làm gì:

Hướng dẫnNghĩa
rùa .forward (số)Tiến về phía trước
Rùa.back (số)Di chuyển về phía sau
Rùa.Right (góc)Quay theo chiều kim đồng hồ
Rùa.left (góc)Quay ngược chiều kim đồng
rùa.pencolor (chuỗi màu)Vẽ màu
Rùa. Tăng cường (số)Chọn kích thước của bút nib
rùa.circle (bán kính)Vẽ một vòng tròn
Rùa.Speed ​​(số)Chọn tốc độ - 1 đến 10
rùa.write (tin nhắn, phông chữ)Viết trên màn hình
rùa.ht ()Giấu con rùa
rùa.setpos (x, y)Thay đổi vị trí của rùa

Vẽ một góc phải

Hoạt hình sau đây cho thấy con rùa đang làm việc trong việc sản xuất góc phải trên màn hình:

Đây là mã cho hoạt hình trên:

Nhập rùa & nbsp; & nbsp; & nbsp; #làm cho một đối tượng rùa của lớp rùa để vẽ màn hình = rùa.screen () & nbsp; & nbsp; & nbsp; #Making một khung vẽ cho màn hình vẽ.setup (400.300) & nbsp; & nbsp; & nbsp; #choosing màn hình kích thước màn hình.bgcolor ('đen') & nbsp; & nbsp; & nbsp; #Making Canvas Black Trtl.Pencolor ('Red') & nbsp; & nbsp; & nbsp; #Making màu của bút trtl.pensize (5) & nbsp; & nbsp; & nbsp; #choos Kích thước của bút nib trtl.speed (1) & nbsp; & nbsp; & nbsp; #choosing tốc độ vẽ trtl.shape ('rùa') & nbsp; & nbsp; & nbsp; #Drawing một dòng 200 pixel trtl.right (90) & nbsp; & nbsp; & nbsp; #Drawing một dòng 200 pixel trtl.penup () & nbsp; & nbsp; & nbsp; # Làm cho vị trí mới của rùa trtl.pendown () & nbsp; & nbsp; & nbsp;# đưa bút xuống để vẽ lại trtl.pencolor ('màu xanh lá cây') Viết ('Vivax Solutions', font = ("Arial", 20, "in đậm")) & nbsp; & nbsp; & nbsp; # Chọn phông chữ trtl.penup () trtl.ht () & nbsp; & nbsp; & nbsp; # giấu con rùa khỏi màn hình
trtl = turtle.Turtle()    #making a turtle object of Turtle class for drawing
screen=turtle.Screen()    #making a canvas for drawing
screen.setup(400,300)    #choosing the screen size
screen.bgcolor('black')    #making canvas black
trtl.pencolor('red')    #making colour of the pen red
trtl.pensize(5)    #choosing the size of pen nib
trtl.speed(1)    #choosing the speed of drawing
trtl.shape('turtle')   #choosing the shape of pen nib
trtl.forward(150)    #drawing a line of 200 pixels
trtl.right(90)    #asking turtle to turn 90 degrees
trtl.forward(150)    #drawing a line of 200 pixels
trtl.penup()    # preparing for moving pen without drawing
trtl.setpos(-140,-120)    # making the new position of the turtle
trtl.pendown()   # bringing the pen down for drawing again
trtl.pencolor('green')    # choosin the pen colour as green
trtl.write('Vivax Solutions', font=("Arial", 20, "bold"))    # chosing the font
trtl.penup()
trtl.ht()    # hiding the turtle from the screen

# Dấu hiệu cho biết các ý kiến ​​trong các tập lệnh Python. sign indicates the comments in Python scripts.

Vẽ một hình vuông

Hoạt hình sau đây cho thấy cách con rùa vẽ một hình vuông trên màn hình:

Đây là mã cho hoạt hình trên:

Nhập rùa & nbsp; & nbsp; & nbsp; #làm cho một đối tượng rùa của lớp rùa để vẽ màn hình = rùa.screen () & nbsp; & nbsp; & nbsp; #Making một khung vẽ cho màn hình vẽ.setup (400.300) & nbsp; & nbsp; & nbsp; #choosing màn hình kích thước màn hình.bgcolor ('đen') & nbsp; & nbsp; & nbsp; #Making Canvas Black Trtl.Pencolor ('Red') & nbsp; & nbsp; & nbsp; #Making màu của bút trtl.pensize (5) & nbsp; & nbsp; & nbsp; #choos Kích thước của bút nib trtl.speed (1) & nbsp; & nbsp; & nbsp; #choosing tốc độ vẽ trtl.shape ('rùa') & nbsp; & nbsp; & nbsp; #Drawing một dòng 200 pixel trtl.right (90) & nbsp; & nbsp; & nbsp; #Drawing một dòng 200 pixel trtl.penup () & nbsp; & nbsp; & nbsp; # Làm cho vị trí mới của rùa trtl.pendown () & nbsp; & nbsp; & nbsp;# đưa bút xuống để vẽ lại trtl.pencolor ('màu xanh lá cây') Viết ('Vivax Solutions', font = ("Arial", 20, "in đậm")) & nbsp; & nbsp; & nbsp; # Chọn phông chữ trtl.penup () trtl.ht () & nbsp; & nbsp; & nbsp; # giấu con rùa khỏi màn hình
trtl = turtle.Turtle()    #making a turtle object of Turtle class for drawing
screen=turtle.Screen()    #making a canvas for drawing
screen.setup(400,300)    #choosing the screen size
screen.bgcolor('black')    #making canvas black
trtl.pencolor('red')    #making colour of the pen red
trtl.pensize(5)    #choosing the size of pen nib
trtl.speed(1)    #choosing the speed of drawing
trtl.shape('turtle')   #choosing the shape of pen nib
trtl.forward(100)    #top line
trtl.right(90)
trtl.forward(100)    # right vertical line
trtl.right(90)
trtl.forward(100)   # bottom line
trtl.right(90)
trtl.forward(100)   # left vertical line
# information printing
trtl.penup()
trtl.setpos(-120,100)
trtl.pendown()
trtl.pencolor('green')
trtl.write('Square - Vivax Solutions', font=("Arial", 16, "bold"))
trtl.penup()
trtl.ht()

# Dấu hiệu cho biết các ý kiến ​​trong các tập lệnh Python.

Nhập rùa & nbsp; & nbsp; & nbsp; # Nhập mô -đun trtl = rùa.turtle () & nbsp; & nbsp; & nbsp; #làm cho một đối tượng rùa của lớp rùa để vẽ màn hình = rùa.screen () & nbsp; & nbsp; & nbsp; #Making một khung vẽ cho màn hình vẽ.setup (400.300) & nbsp; & nbsp; & nbsp; #choosing màn hình kích thước màn hình.bgcolor ('đen') & nbsp; & nbsp; & nbsp; #Making Canvas Black Trtl.Pencolor ('Red') & nbsp; & nbsp; & nbsp; #Making màu của bút trtl.pensize (5) & nbsp; & nbsp; & nbsp; #choos Kích thước của bút nib trtl.speed (1) & nbsp; & nbsp; & nbsp; . ; & nbsp; & nbsp; trtl.right (90) & nbsp; & nbsp; & nbsp; # Để biến # thông tin in trtl.penup () trtl.setpos (-120.100) trtl.pendown () trtl.pencolor ('Green') trtl.write ('Square - Vivax Solutions', font = ("Arial", 16 , "in đậm")) trtl.penup () trtl.ht ()
trtl = turtle.Turtle()    #making a turtle object of Turtle class for drawing
screen=turtle.Screen()    #making a canvas for drawing
screen.setup(400,300)    #choosing the screen size
screen.bgcolor('black')    #making canvas black
trtl.pencolor('red')    #making colour of the pen red
trtl.pensize(5)    #choosing the size of pen nib
trtl.speed(1)    #choosing the speed of drawing
trtl.shape('turtle')   #choosing the shape of pen nib
for i in range(4): # for loop to minimize the same lines of codes being written
      trtl.forward(100)   # for lines
      trtl.right(90)    # for turning
# information printing
trtl.penup()
trtl.setpos(-120,100)
trtl.pendown()
trtl.pencolor('green')
trtl.write('Square - Vivax Solutions', font=("Arial", 16, "bold"))
trtl.penup()
trtl.ht()

Vòng lặp cho bản vẽ của hình vuông dễ dàng hơn nhiều. Vai trò của nó thực sự quan trọng khi chúng ta tạo ra các đa giác khác.for loop makes the drawing of the square much easier. It role is really important when we create other polygons.

Sách được đề xuất

Hướng dẫn python drawing code copy and paste - sao chép và dán mã bản vẽ python

Nếu bạn đang học khoa học máy tính tại GCSE, đây là một bộ sách dành cho bạn: chúng là hướng dẫn sửa đổi, nhưng chúng bao gồm mọi chủ đề duy nhất, đồng thời cung cấp thông tin phong phú để nắm bắt các khái niệm theo cách sáng tạo; Các cuốn sách cho thấy một con đường rõ ràng để đi theo, một cái gì đó mà các cuốn sách văn bản cồng kềnh không làm được; Họ là tốt ngay cả cho một người trước đây thứ sáu.

Vẽ đa giác

Để thay đổi mã trên để vẽ đa giác, chúng ta cần tính đến một số khái niệm trong hình học. Như bạn có thể thấy, với mỗi lượt, con rùa di chuyển qua một góc nhất định, được chỉ định bởi bên phải (góc). Đây là góc bên ngoài của đa giác, giống như 360/n, trong đó n là số lượng cạnh.right(angle). This is the exterior angle of the polygon, which is the same as 360/n, where n is the number of sides.

Với và trong khi các vòng lặp, mã sau đây tạo ra một tập hợp các đa giác trên màn hình - từ một hình tam giác đến một độ mười.for and while loops, the following code produces a set of polygons on screen - from a triangle to a decagon.

Mã cho hoạt hình trên như sau:

Nhập rùa & nbsp; & nbsp; & nbsp; & nbsp; # Nhập thời gian nhập mô -đun & nbsp; & nbsp; & nbsp; & nbsp; #làm cho một đối tượng rùa của lớp rùa để vẽ màn hình = rùa.screen () & nbsp; & nbsp; & nbsp; & nbsp; #Making một khung vẽ cho màn hình vẽ.setup (620,470) & nbsp; & nbsp; & nbsp; & nbsp; #choosing màn hình kích thước màn hình.bgpic ('bg.gif') & nbsp; & nbsp; & nbsp; & nbsp; #Making Canvas Black Trtl.Pencolor ('Red') & nbsp; & nbsp; & nbsp; & nbsp; #Making màu của bút trtl.pensize (5) & nbsp; & nbsp; & nbsp; & nbsp; #choos Kích thước của bút nib trtl.speed (1) & nbsp; & nbsp; & nbsp; & nbsp; #Choos tốc độ vẽ trtl.shape ('rùa') & nbsp; & nbsp; & nbsp; & nbsp; #choos = ['Tam giác', 'Square', 'Lầu năm góc', 'Hexagon', 'Heptagon', 'Octagon', 'Nonagon', 'Decagon'] trong khi n
import time    #importing the time module trtl = turtle.Turtle()     #making a turtle object of Turtle class for drawing
screen=turtle.Screen()     #making a canvas for drawing
screen.setup(620,470)     #choosing the screen size
screen.bgpic('bg.gif')     #making canvas black
trtl.pencolor('red')     #making colour of the pen red
trtl.pensize(5)     #choosing the size of pen nib
trtl.speed(1)     #choosing the speed of drawing
trtl.shape('turtle')    #choosing the shape of pen nib
time.sleep(12)
n=3    starting for a triangle
shapes=['Triangle','Square','Pentagon','Hexagon','Heptagon','Octagon','Nonagon','Decagon']
while n<11: #    limiting to a decagon
        for i in range(n):     # for loop to minimize the same lines of codes being written
            trtl.pencolor('red')
            trtl.forward(100)     #top line
            trtl.right(360/n)    #determining the exterior angle of the polygon
trtl.penup()
trtl.setpos(-80,180)    #moving the turtle to make the animation more centric
trtl.pendown()
trtl.pencolor('blue')
trtl.write(' This is '+shapes[n-3], font=("Arial", 16, "bold"))    #printing the name of the polygon
n=n+1
time.sleep(1)
    #making turtle sleep for one second trtl.clear()
trtl.penup()
trtl.setpos(-n*8,n*14)
trtl.pendown()

Nếu bạn muốn thực hành nó một cách tương tác, đây là mã:

Vẽ thư e

Xin lưu ý cách hình ảnh được tập trung trên màn hình, với phương thức rùa.setpos ().

Vẽ vòng tròn

Dưới đây là mã để thêm hai số và vẽ một vòng tròn:

Nhập rùa & nbsp; & nbsp; & nbsp; # Nhập mô -đun trtl = rùa.turtle () & nbsp; & nbsp; & nbsp; #làm cho một đối tượng rùa của lớp rùa để vẽ màn hình = rùa.screen () & nbsp; & nbsp; & nbsp; #Making một khung vẽ cho màn hình vẽ.setup (420.320) & nbsp; & nbsp; & nbsp; #choosing màn hình kích thước màn hình.bgpic ('bg.gif') & nbsp; & nbsp; & nbsp; #Making màu của cây bút trtl.pensize (4) & nbsp; & nbsp; & nbsp; #choos Kích thước của bút nib trtl.speed (1) & nbsp; & nbsp; & nbsp; #Choos tốc độ vẽ trtl.shape ('rùa') & nbsp; & nbsp; & nbsp;
trtl = turtle.Turtle()    #making a turtle object of Turtle class for drawing
screen=turtle.Screen()    #making a canvas for drawing
screen.setup(420,320)    #choosing the screen size
screen.bgpic('bg.gif')    #making canvas black
trtl.pencolor('red')    #making colour of the pen red
trtl.pensize(4)    #choosing the size of pen nib
trtl.speed(1)    #choosing the speed of drawing
trtl.shape('turtle')   #choosing the shape of pen nib
trtl.circle(60)   drawing circle with radius 60 pixels

Hoạt hình sau đây cho thấy cách hai số được thực hiện trong hai đầu vào và câu trả lời sau này được đưa ra:

Như bạn có thể thấy, điểm bắt đầu của vòng tròn là trung tâm của màn hình theo mặc định, không phải là trung tâm của vòng tròn. Để giải quyết vấn đề này, chúng tôi phải đặt vị trí theo mã như sau:

Nhập rùa & nbsp; & nbsp; & nbsp; # Nhập mô -đun trtl = rùa.turtle () & nbsp; & nbsp; & nbsp; #làm cho một đối tượng rùa của lớp rùa để vẽ màn hình = rùa.screen () & nbsp; & nbsp; & nbsp; #Making một khung vẽ cho màn hình vẽ.setup (420.320) & nbsp; & nbsp; & nbsp; #choosing màn hình kích thước màn hình.bgpic ('bg.gif') & nbsp; & nbsp; & nbsp; #Making màu của cây bút trtl.pensize (4) & nbsp; & nbsp; & nbsp; #choos Kích thước của bút nib trtl.shape ('rùa') & nbsp; & nbsp; & nbsp; -60) & nbsp; & nbsp; & nbsp;#cài đặt vị trí mới trtl.pendown () & nbsp; & nbsp; & nbsp;
trtl = turtle.Turtle()    #making a turtle object of Turtle class for drawing
screen=turtle.Screen()    #making a canvas for drawing
screen.setup(420,320)    #choosing the screen size
screen.bgpic('bg.gif')    #making canvas black
trtl.pencolor('red')    #making colour of the pen red
trtl.pensize(4)    #choosing the size of pen nib
trtl.shape('turtle')   #choosing the shape of pen nib
trtl.penup()   #moving the pen up
trtl.setpos(0,-60)   #setting new position
trtl.pendown()   #moving the pen down
trtl.circle(60)   #drawing circle with radius 60 pixels

Ad: No more sore throat suffering with this:

Drawing Concentric Circles

Here is the code for drawing concentric circles with the aid of loops:

import turtle    # importing the module
trtl = turtle.Turtle()    #making a turtle object of Turtle class for drawing
screen=turtle.Screen()    #making a canvas for drawing
screen.setup(420,320)    #choosing the screen size
screen.bgpic('bg.gif')    #making canvas black
trtl.pencolor('red')    #making colour of the pen red
trtl.pensize(4)    #choosing the size of pen nib
trtl.shape('turtle')   #choosing the shape of pen nib
n=0   
while n<7:   #loop for 7 circles
n=n+1   
trtl.penup()   
trtl.setpos(0,-n*20)   
trtl.pendown()   
trtl.circle(20*n)   

The following animation shows how the animation works:

Changing Colours

The colour of the pen can be changed in many different ways; here are two ways:

  1. turtle.pencolor('red')
  2. turtle.pencolor(red, green, blue)

If you use the second method, red, green and blue can be any integer between 1 - 255. However, before that, turtle.colormode(1) or turtle.colormode(255) must be declared in the code.

Here is the code:

import turtle    # importing the module
import random    # importing random module for generating random numbers
trtl = turtle.Turtle()    #making a turtle object of Turtle class for drawing
screen=turtle.Screen()    #making a canvas for drawing
screen.setup(420,320)    #choosing the screen size
screen.bgpic('bg.gif')    #making canvas black
trtl.pensize(4)    #choosing the size of pen nib
trtl.speed(1)    #choosing the speed of drawing
trtl.shape('turtle')   #choosing the shape of pen nib
n=0
while n<7:
r=random.randint(1,120)   #random numbers for red
g=random.randint(81,200)   #random numbers for green
b=random.randint(61,255)   #random numbers for blue
turtle.colormode(255)   #declaring the colour mode
trtl.pencolor(r,g,b)   #pen colour
n=n+1
trtl.penup()
trtl.setpos(0,-n*20)
trtl.pendown()
trtl.circle(20*n)

The following animation shows the output - with random colours, of course.

Advanced Animations - turtle in its habitat!

The following code creates a turtle that moves around on a beach - leaving behind a certain regular pattern!

import turtle
import random
trtl = turtle.Turtle()
screen=turtle.Screen()
screen.setup(420,320)
screen.bgpic('bg.gif')
trtl.pensize(4)
trtl.speed(1)
trtl.shape('turtle')
turtle.colormode(255)
trtl.pencolor(242,242,242)
trtl.penup()
trtl.setpos(-160,-100)
trtl.pendown()
for i in range(4):
        trtl.forward(40)
        trtl.left(90)
        trtl.forward(30)
        trtl.right(90)
        trtl.forward(40)

The following animation shows the iteration that leads to a countdown, based on the user input:

Advanced Animations - diverging turtles

In this animation, turtles leave their foot print on the beach: this is achieved by turtle.stamp() method along with turtle.penup(). This is the code:

import turtle
import random
import time
screen=turtle.Screen()
trtl=turtle.Turtle()
screen.setup(420,320)
screen.bgpic('bg.gif')
trtl.shape('turtle')
trtl.color('darkgoldenrod','black')
s=10
trtl.penup()
trtl.setpos(30,30)
for i in range(28):
        s=s+2
        trtl.stamp()
        trtl.forward(s)
        trtl.right(25)
        time.sleep(0.25)      #activated with a break of a 1/4th of a second

The animation is as follows:

Advanced Animations - colourful star

With the aid of simple geometry, a star can be drawn by Python Turtle. This is the code:

import turtle
import time
screen=turtle.Screen()
trtl=turtle.Turtle()
screen.setup(420,320)
screen.bgcolor('black')
clr=['red','green','blue','yellow','purple']
trtl.pensize(4)
trtl.penup()
trtl.setpos(-90,30)
trtl.pendown()
for i in range(5):
     trtl.pencolor(clr[i])
     trtl.forward(200)
     trtl.right(144)
trtl.penup()
trtl.setpos(80,-140)
trtl.pendown()
trtl.pencolor('olive')
trtl.write('Vivax Solutions',font=("Arial", 12, "normal"))
trtl.ht()

This is the animation that produces the star:

Advanced Animations - a clock dial

In the following animation, Python Turtle is used to construct a clock dial - with numbers, 1 to 12, surrounded by a circle. This is the code:

import turtle
screen=turtle.Screen()
trtl=turtle.Turtle()
screen.setup(620,620)
screen.bgcolor('black')
clr=['red','green','blue','yellow','purple']
trtl.pensize(4)
trtl.shape('turtle')
trtl.penup()
trtl.pencolor('red')
m=0
for i in range(12):
      m=m+1
      trtl.penup()
      trtl.setheading(-30*i+60)
      trtl.forward(150)
      trtl.pendown()
      trtl.forward(25)
      trtl.penup()
      trtl.forward(20)
      trtl.write(str(m),align="center",font=("Arial", 12, "normal"))
      if m==12:
        m=0
      trtl.home()
trtl.home()
trtl.setpos(0,-250)
trtl.pendown()
trtl.pensize(10)
trtl.pencolor('blue')
trtl.circle(250)
trtl.penup()
trtl.setpos(150,-270)
trtl.pendown()
trtl.pencolor('olive')
trtl.write('Vivax Solutions',font=("Arial", 12, "normal"))
trtl.ht()

Đây là hoạt hình tại nơi làm việc:

Hoạt hình nâng cao - Hoa nhiều màu

Các mã sau đây tạo ra một bông hoa trong các màu khác nhau. Các màu được cung cấp với sự trợ giúp của một danh sách. Mã như sau:

trtl = rùa.turtle () screen = rùa.screen () screen.setup (620,620) screen.bgcolor ('black') trtl.pensize (3) trtl.speed (10) n = -1 cho góc trong phạm vi (0,360 , 15): & nbsp; & nbsp; & nbsp; & nbsp; n = n+1 & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; nếu n == 5: & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; n = -1 & nbsp; & nbsp; & nbsp; & nbsp; trtl.color (màu [n]) & nbsp; & nbsp; & nbsp; & nbsp; trtl.seth (góc) & nbsp; & nbsp; & nbsp; & nbsp; trtl.circle (100) trtl.penup () trtl.setpos (150, -270) trtl.pendown () trtl.pencolor ('Olive') trtl.write ('Vivax Solutions', font = ("Arial", 12 , "bình thường")) trtl.ht ()
screen=turtle.Screen()
screen.setup(620,620)
screen.bgcolor('black')
trtl.pensize(3)
trtl.speed(10)
n=-1
for angle in range(0,360,15):
     n=n+1
       if n==5:
       n=-1
      trtl.color(colors[n])
      trtl.seth(angle)
      trtl.circle(100)
trtl.penup()
trtl.setpos(150,-270)
trtl.pendown()
trtl.pencolor('olive')
trtl.write('Vivax Solutions',font=("Arial", 12, "normal"))
trtl.ht()

Đây là hình ảnh động:

Như bạn có thể thấy, hiệu ứng được tạo ra bởi chức năng rùa.seth () - trong việc chuyển hướng của rùa.turtle.seth() function - in turning the direction of turtle.

Vẽ nhiều hình vuông

Trong hoạt hình này, số lượng hình vuông được xác định trước được rút ra với sự trợ giúp của một hàm. Hàm có hai tham số - chiều dài của hình vuông và màu. Sau đó, sử dụng một vòng lặp cho vòng lặp, chúng ta có thể vẽ số lượng hình vuông chúng ta cần. Bạn có thể thay đổi độ dài và màu sắc để thực hành tương tác.

Tạo giấy biểu đồ

Với mã được đưa ra dưới đây, bạn có thể tạo một giấy biểu đồ một cách dễ dàng với Python Rùa:

Nhập rùa trtl = rùa.turtle () trtl.speed (10) cho i trong phạm vi (0,400,20): trtl.pencolor ('lightgrey') & nbsp; & nbsp; & nbsp; & nbsp; trtl.penup () & nbsp; & nbsp; & nbsp; & nbsp; trtl.setpos (-200+I, -200) & nbsp; & nbsp; & nbsp; & nbsp; Nếu i == 0: & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; trtl.left (90) & nbsp; & nbsp; & nbsp; & nbsp; trtl.pendown () & nbsp; & nbsp; & nbsp; & nbsp; trtl.forward (400) & nbsp; & nbsp; & nbsp; & nbsp; trtl.backward (400) cho i trong phạm vi (0,400,20): & nbsp; & nbsp; & nbsp; & nbsp; trtl.pencolor ('lightgrey') & nbsp; & nbsp; & nbsp; & nbsp; trtl.penup () & nbsp; & nbsp; & nbsp; & nbsp; trtl.setpos (-200, -200+i) & nbsp; & nbsp; & nbsp; & nbsp; Nếu i == 0: & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; trtl.right (90) & nbsp; & nbsp; & nbsp; & nbsp; trtl.pendown () & nbsp; & nbsp; & nbsp; & nbsp; trtl.forward (400) & nbsp; & nbsp; & nbsp; & nbsp; trtl.backward (400) trtl.penup () trtl.home () trtl.pendown () trtl.pencolor ('black') trtl.backward (200) trtl.forward (400) trtl.backward (200) trtl.left . 5. -180) trtl.pendown () trtl.write ("Vivax Solutions") trtl.ht ()
trtl=turtle.Turtle()
trtl.speed(10)
for i in range(0,400,20):
trtl.pencolor('lightgrey')
      trtl.penup()
      trtl.setpos(-200+i,-200)
      if i==0:
         trtl.left(90)
      trtl.pendown()
      trtl.forward(400)
      trtl.backward(400)
for i in range(0,400,20):
      trtl.pencolor('lightgrey')
      trtl.penup()
      trtl.setpos(-200,-200+i)
      if i==0:
         trtl.right(90)
      trtl.pendown()
      trtl.forward(400)
      trtl.backward(400)
trtl.penup()
trtl.home()
trtl.pendown()
trtl.pencolor('black')
trtl.backward(200)
trtl.forward(400)
trtl.backward(200)
trtl.left(90)
trtl.forward(200)
trtl.backward(400)
trtl.penup()
trtl.setpos(5,5)
trtl.pendown()
trtl.write(0)
trtl.penup()
trtl.setpos(190,5)
trtl.pendown()
trtl.write("x")
trtl.penup()
trtl.setpos(5,190)
trtl.pendown()
trtl.write("y")
trtl.penup()
trtl.setpos(80,-180)
trtl.pendown()
trtl.write("Vivax Solutions")
trtl.ht()

Với mã trên, có thể tạo ra những điều sau đây:

Hướng dẫn python drawing code copy and paste - sao chép và dán mã bản vẽ python

Bạn có thể thực hành nó tương tác ở đây:

Mã cho năm mươi sắc thái của hoạt hình màu xám

Để tạo ra hình ảnh động ở đầu hướng dẫn này, vui lòng làm theo mã này:

Nhập rùa rùa.penup () rùa.setpos (-100.250) rùa.pendown () rùa.pencolor ('olive') rùa.write ('Fifty Shades of Grey', Font = ("Arial", 18, "Bold" )) rùa.penup () rùa.setpos (0,0) rùa.pendown () rùa.color ("đen", "trắng") rùa Phạm vi (bình phương): rùa.fillcolor (bóng râm, bóng râm, bóng râm) rùa.begin_fill () rùa.left (360 // hình vuông) cho bên trong phạm vi (4): & nbsp; & nbsp; & nbsp; & nbsp; rùa. ) & nbsp; & nbsp; & nbsp; & nbsp; rùa. .Penup () rùa.setpos (150, -270) rùa.pendown () rùa.pencolor ('olive') rùa. .xong()
turtle.penup()
turtle.setpos(-100,250)
turtle.pendown()
turtle.pencolor('olive')
turtle.write('Fifty Shades of Grey',font=("Arial", 18, "bold"))
turtle.penup()
turtle.setpos(0,0)
turtle.pendown()
turtle.color("black", "white")
turtle.colormode(1.0)
SQUARES = 50
SIDE = 150
shade = 1.0
for count in range(SQUARES):
turtle.fillcolor(shade, shade, shade)
turtle.begin_fill()
turtle.left(360 // SQUARES)
for side in range(4):
    turtle.forward(SIDE)
    turtle.left(90)
    turtle.end_fill()
    shade -= turtle.colormode() / float(SQUARES)
turtle.penup()
turtle.setpos(150,-270)
turtle.pendown()
turtle.pencolor('olive')
turtle.write('Vivax Solutions',font=("Arial", 12, "normal"))
turtle.done()

Bây giờ bạn đã đọc hướng dẫn này, bạn cũng sẽ thấy các hướng dẫn sau đây rất hữu ích:

  • Python: cơ bản đến trung gian - tương tác
  • Visual Basic - Tương tác
  • Máy tính Little Man - Hướng dẫn LMC
  • Lập trình hướng đối tượng - OOP
  • Hướng dẫn HTML
  • Hướng dẫn JavaScript

Làm thế nào để bạn vẽ đồ họa trong Python?

Các bước để tạo một đồ họa trong Python.Các tính năng của cửa sổ đồ họa.Tạo các loại đối tượng đồ họa khác nhau ...
Nhập tất cả các thư viện từ lớp đồ họa ..
Tạo một đối tượng cửa sổ ..
Vẽ các đối tượng trong cửa sổ đã tạo ..
Đóng cửa sổ..

Bạn có thể sử dụng Python để vẽ không?

Rùa rùa là một tính năng python giống như một bảng vẽ, cho phép bạn chỉ huy một con rùa để vẽ khắp nơi!Bạn có thể sử dụng các chức năng như rùa.Chuyển tiếp (...) và Rùa.Trái (...) có thể di chuyển con rùa xung quanh., which lets you command a turtle to draw all over it! You can use functions like turtle. forward(...) and turtle. left(...) which can move the turtle around.

Penup () làm gì trong Python?

Penup () về cơ bản đảm bảo rằng đối tượng chuyển động mà bạn đã tạo không vẽ bất cứ thứ gì trên cửa sổ.Vì vậy, nếu bạn có một quả bóng và bạn muốn nó di chuyển xung quanh và vẽ bất cứ thứ gì trên cửa sổ, thì bạn sử dụng Penup ().makes sure that the moving object that you've created does not draw anything on the window. So if you have a ball and you want it to move around and draw anything on the window, then you use the penup().

Tùa T con rùa () có nghĩa là gì trong Python?

Tóm lại, rùa.Rùa () là phương pháp xây dựng của rùa lớp;Nó trả về một ví dụ của lớp.the constructor method of the class Turtle ; it returns an instance of the class.