Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Đọc Python Turtle Basics
Bàn luận is an inbuilt module of python. It enables us to draw any drawing by a turtle and methods defined in the turtle module and by using some logical loops. turtle drawings are basically drawn using four methods defined in the turtle module.
Điều kiện tiên quyết: Những điều cơ bản của Rùa Python: moves the turtle[pen] in the forward direction by x unit.
Rùa là một mô -đun sẵn có của Python. Nó cho phép chúng tôi vẽ bất kỳ bản vẽ nào bằng rùa và các phương pháp được xác định trong mô -đun rùa và bằng cách sử dụng một số vòng logic. Bản vẽ rùa về cơ bản được vẽ bằng bốn phương pháp được xác định trong mô -đun rùa.: moves the turtle[pen] in the backward direction by x unit.
Chuyển tiếp [x]: di chuyển rùa [bút] theo hướng chuyển tiếp theo đơn vị x.: rotate the turtle[pen] by n degree in clockwise direction.
Backward [x]: di chuyển rùa [bút] theo hướng ngược bởi đơn vị x.: rotate the turtle[pen] by n degree in anticlockwise direction.
Phải [N]: Xoay rùa [bút] theo n độ theo hướng theo chiều kim đồng hồ.
Trái [N]: Xoay rùa [PEN] theo N theo hướng ngược chiều kim đồng hồ.
Trong bài viết này, chúng ta sẽ học cách vẽ các đa giác có hình dạng khác nhau bằng mô -đun rùa. Với số lượng cạnh [N] và chiều dài của các cạnh [L], người ta có thể dễ dàng vẽ bất kỳ hình dạng đa giác nào. Hãy để cố gắng hiểu nó tốt hơn với sự trợ giúp của các ví dụ.
import
turtle
t
=
3 1500
3 1501
=
3 1503
3 1504
3 1505
3 1504__17171718
The number of sides the Polygon should have is : 3 The length of each side the Polygon should have is : 602
The number of sides the Polygon should have is : 3 The length of each side the Polygon should have is : 603
3 1509
=
3 1503
3 1504
3 1505
3 1504
4 1505
3 1508
4
150
7 4
150
84
150
9 The number of sides the Polygon should have is : 3
The length of each side the Polygon should have is : 60
0The number of sides the Polygon should have is : 3
The length of each side the Polygon should have is : 60
1
10 100
The number of sides the Polygon should have is : 3
The length of each side the Polygon should have is : 60
2The number of sides the Polygon should have is : 3
The length of each side the Polygon should have is : 60
5The number of sides the Polygon should have is : 3
The length of each side the Polygon should have is : 60
6 The number of sides the Polygon should have is : 3
The length of each side the Polygon should have is : 60
7 The number of sides the Polygon should have is : 3
The length of each side the Polygon should have is : 60
8
4
150
7 4
150
84
150
9 The number of sides the Polygon should have is : 3
The length of each side the Polygon should have is : 60
0The number of sides the Polygon should have is : 3
The length of each side the Polygon should have is : 60
1
3 150
The number of sides the Polygon should have is : 3
The length of each side the Polygon should have is : 60
2The number of sides the Polygon should have is : 3
The length of each side the Polygon should have is : 60
5The number of sides the Polygon should have is : 3
The length of each side the Polygon should have is : 60
6 The number of sides the Polygon should have is : 3
The length of each side the Polygon should have is : 60
7 The number of sides the Polygon should have is : 3
The length of each side the Polygon should have is : 60
8
4
150
7 4
150
84
150
9 The number of sides the Polygon should have is : 3
The length of each side the Polygon should have is : 60
0The number of sides the Polygon should have is : 3
The length of each side the Polygon should have is : 60
1
4 150
The number of sides the Polygon should have is : 3
The length of each side the Polygon should have is : 60
2The number of sides the Polygon should have is : 3
The length of each side the Polygon should have is : 60
5The number of sides the Polygon should have is : 3
The length of each side the Polygon should have is : 60
6 The number of sides the Polygon should have is : 3
The length of each side the Polygon should have is : 60
7 The number of sides the Polygon should have is : 3
The length of each side the Polygon should have is : 60
8
Python chứa một mô -đun gọi là Rùa. Nó cho phép chúng tôi phác thảo bất kỳ bản vẽ nào bằng rùa, kỹ thuật từ mô -đun rùa và các vòng logic. Về cơ bản, các bản vẽ rùa được tạo ra bằng một trong bốn kỹ thuật được liệt kê trong mô -đun rùa.Turtle. It allows us to sketch any drawing using a turtle, techniques from the turtle module, and logical loops. Basically, turtle drawings are created using one of four techniques listed in the turtle module.
- Chuyển tiếp [P]: Con rùa [bút] được di chuyển về phía trước một lượng bằng p. The turtle [pen] is moved forward by an amount equal to p.
- Đảo ngược [P]: Chuyển vị trí của rùa [PEN] ngược lại bằng một lượng bằng p. shifts the turtle's [pen's] position backward by an amount equal to p.
- Phải [Q]: Xoay theo chiều kim đồng hồ [PEN] theo độ Q. the turtle [pen] by q degrees.
- trái [q]: xoay con rùa [bút] theo độ Q ngược chiều kim đồng hồ bằng cách nhấn bên trái [q] Rotate the turtle [pen] by q degrees counterclockwise by pressing the left[q]
Chúng tôi sẽ nói về việc vẽ đa giác trong Rùa trong bài viết này. Rùa thực chất là một mô-đun tích hợp Python. Rùa cho phép chúng ta vẽ một loạt các hình dạng. Tkinter chủ yếu được sử dụng cho hình ảnh. Do đó, Tkinter là cần thiết ngoài Python.drawing polygons in Turtle in this article. Turtle is essentially a Python built-in module. Turtle allows us to draw a variety of shapes. Tkinter is mostly used for the visuals. Tkinter is therefore required in addition to Python.
Do đó, điều này sẽ gây ra lỗi nếu bạn không có Tkinter -throw an error if you don't have Tkinter -
Nếu nhàn rỗi [môi trường phát triển và học tập tích hợp] đã được cài đặt. Tkinter sau đó đã được thiết lập. Nhưng nếu không, thì hãy tuân thủ các hướng dẫn dưới đây.IDLE [Integrated Development and Learning Environment] is already installed. Tkinter is then already set up. But if it's not, then adhere to the guidelines below.
Lưu ý: Để thực hiện các hoạt động sau, bạn phải là một siêu người dùng. Trong trường hợp không có một, chúng tôi khuyên bạn nên liên hệ với quản trị viên hệ thống của bạn.
Cho rằng phần mềm có thể truy cập thông qua kho lưu trữ Ubuntu mặc định. Vì vậy, nâng cấp kho lưu trữ có lợi. Sau đó, cài đặt Tkinter.software is accessible via the default Ubuntu repository. So, upgrading the repository is beneficial. After that, install Tkinter.
Đa giác trong Rùa - Python:
Bài học này sẽ dạy chúng ta cách sử dụng một con rùa trong thư viện rùa Python để vẽ một đa giác.use a turtle in Python turtle library to draw a polygon.
Chúng ta nên biết một chút về đa giác trước khi tiếp tục. Một đa giác là một loại hình hình học có hai chiều. Nó có một số lượng nhất định các cạnh được nối với nhau để tạo thành một hình dạng đa giác.polygon is a type of geometric figure that has two dimensions. It has a definite number of sides which are joined together to form a polygonal shape.
Bởi vì tam giác là một hình mặt phẳng, chúng tôi đặt tên cho nó là một đa giác. Tương tự như tam giác, vòng tròn có hình mặt phẳng, nhưng chúng tôi không gọi nó là một đa giác vì nó bị cong và thiếu hai bên.triangle is a plane figure, we named it a polygon. Similar to the triangle, the circle does have a plane figure, but we don't refer to it as a polygon since it is curved and lacks sides.
Khi mọi thứ đã sẵn sàng, chúng tôi chuyển sang ý tưởng mà chúng tôi sẽ sử dụng để sử dụng rùa để tạo ra một đa giác của bất kỳ hình dạng nào. Trước tiên chúng ta phải xác định góc ngoài của đa giác. Nếu chúng ta biết về số lượng của đa giác, việc tìm thấy điều này khá đơn giản.Turtle to create a polygon of any shape. We must first determine a polygon's outer angle. If we are aware of the polygon's number of sides, finding this is rather simple.
Góc bên ngoài của một đa giác = 360/số cạnh.
Sử dụng một minh họa, hiểu nó:illustration, comprehend it :
Góc bên ngoài của một hình vuông bằng 360/4 nếu số cạnh là 4 [tức là, một hình vuông]. Do đó, góc ngoài của hình vuông là 90 độ.exterior angle of a square is equal to 360/4 if the number of sides is 4 [i.e., a square]. Therefore, a square's outer angle is 90 degrees.
Hơn nữa, tất cả những gì chúng ta phải làm trong tình huống này là hỏi người dùng tổng số bên. Để xác định góc ngoài của đa giác, chia tổng số cạnh cho 360 độ. Để vẽ hình thức cần thiết, sử dụng các hàm cho vòng lặp, chuyển tiếp [] và phải [] từ mô -đun rùa. Vậy hãy bắt đầu.ask a user for the total number of sides. To determine a polygon's external angle, divide the total sides by 360 degrees. To draw the necessary form, utilize the for loop, forward[], and right[] functions from the Turtle module. So lets get started.
Món mã Python này được sử dụng để nhập thư viện rùa vào chương trình Python của chúng tôi.import the turtle library into our python program.
Ở đây, người dùng sẽ phải nhập "bên" và "lngth". Trong đó, bên là tham số cho số cạnh của đa giác và lngth là thước đo của mỗi bên của đa giác. Những giá trị này là cần thiết để vẽ đa giác.side" and "lngth". Where, side is the parameter for the polygon's number of sides and lngth is the measure of each side of Polygon. These values are necessary for drawing the Polygon.
Ở đây, chúng tôi đã sử dụng một vòng lặp for, để vẽ đa giác dựa trên các giá trị được cung cấp bởi người dùng. Vòng lặp cho một bộ hướng dẫn nhất định chạy thời gian "bên". tr.forward [lngth] di chuyển pixel "lngth" rùa về phía trước và vẽ chúng theo hướng mà nó đang đối mặt. Sau đó, nó chuyển vị trí bằng 360/độ bên bằng cách gọi tr.right [360/bên]. Ví dụ, nó sẽ xoay 90 độ cho một hình vuông. Do đó, kết quả trong đa giác được vẽ.for loop, for drawing the Polygon based on the values provided by the user. The for loop causes a certain set of instructions to run "side" times. tr.forward[lngth] moves the turtle "lngth" pixels forward and draws them in the direction it is facing. It then turns in position by 360/side degrees by calling tr.right[360/side]. For instance, it would rotate 90 degrees for a square. Hence, results in the Polygon being drawn.
Mã hoàn chỉnh:
Output:
The number of sides the Polygon should have is : 3 The length of each side the Polygon should have is : 60
Đầu ra: trong 5 mặt- For 5 sides-
The number of sides the Polygon should have is : 5 The length of each side the Polygon should have is : 70
Đầu ra: trong 8 mặt For 8 sides
The number of sides the Polygon should have is : 8 The length of each side the Polygon should have is : 60