
Python's inbuilt module, Turtle, is a fun and interactive way to introduce programming concepts to beginners. It lets users create simple drawings and animations using a turtle that moves around the screen. One can also fill the shapes drawn by the turtle with colours. Turtle provides three functions for this:
- fillcolor(): This function is used to choose the colour for filling the shape. It takes the input parameter as the colour name or hex value of the colour and fills the upcoming closed geographical objects with the chosen colour.
- begin_fill(): This function tells the turtle that all upcoming closed graphical objects need to be filled by the chosen colour.
- end_fill(): This function tells the turtle to stop filling the upcoming closed graphical objects.
To draw a triangle with a colour fill, one can use the following code:
python
import turtle
Set the colour
turtle.fillcolor(blue)
Begin filling
turtle.begin_fill()
Draw the triangle
for i in range(3):
turtle.forward(150)
turtle.left(120)
End filling
turtle.end_fill()
Stop the turtle
turtle.exitonclick()
Characteristics | Values |
---|---|
Module | Inbuilt |
Module Name | Turtle |
Module Purpose | Drawing |
Drawing Tools | Screen (cardboard) and Turtle (pen) |
Functions | forward(), backward(), etc. |
Color Functions | fillcolor(), begin_fill(), end_fill() |
Color Input | Color name or hex value |
What You'll Learn
Using the fillcolor() function to select the colour
The `fillcolor()` function is used to select the colour to fill the shape. It takes the input parameter as the colour name or hex value of the colour and fills the upcoming closed geographical objects with the chosen colour. Basic colour names include red, blue, green, and orange. The hex value of a colour is a string of hexadecimal numbers (beginning with "#"), such as #RRGGBB. The three hexadecimal numbers are R, G, and B.
Python
Import turtle
Create a turtle pen
Ttl = turtle.Turtle()
Set the colour
Ttl.fillcolor("blue")
Begin filling the triangle
Ttl.begin_fill()
Draw the triangle
For _ in range(3):
Ttl.forward(100)
Ttl.left(120)
End filling the triangle
Ttl.end_fill()
Hide the turtle
Ttl.hideturtle()
In this example, we first import the `turtle` module and create a turtle pen. We then set the colour to blue using the `fillcolor()` function. Next, we call the `begin_fill()` function to start filling the triangle with colour. We draw the triangle by moving the turtle forward 100 pixels and turning left 120 degrees three times. Finally, we call the `end_fill()` function to stop filling the colour and hide the turtle.
You can also use the `fillcolor()` function to fill other shapes, such as squares, rectangles, and circles. Simply replace the code that draws the triangle with the code for your desired shape and adjust the colour as needed.
Creating Trees with Turtle Graphics: A Step-by-Step Guide
You may want to see also
Using the begin_fill() function to start filling the triangle
The begin_fill() function is one of the three functions provided by the turtle module in Python to fill colours in the drawn shapes. The other two functions are fillcolor() and end_fill(). The begin_fill() function is used to inform the turtle that all the upcoming closed graphical objects need to be filled with the chosen colour.
The begin_fill() function is used in conjunction with the fillcolor() function, which is used to choose the colour for filling the shape. The fillcolor() function takes the input parameter as the colour name or hex value of the colour. The hex value of a colour is a string of hexadecimal numbers starting with '#', such as #RRGGBB, where R, G, and B are the hexadecimal numbers (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F).
Python
Import turtle
Create a turtle pen
Ttl = turtle.Turtle()
Set the colour
Ttl.fillcolor("blue")
Begin filling the triangle with the chosen colour
Ttl.begin_fill()
Draw the triangle
For _ in range(3):
Ttl.forward(100)
Ttl.left(120)
End filling the triangle
Ttl.end_fill()
Hide the turtle
Ttl.hideturtle()
In this example, we first import the turtle module and create a turtle pen. We then set the colour to "blue" using the fillcolor() function. Next, we call the begin_fill() function to start filling the upcoming closed graphical objects with the chosen colour. We draw the triangle by moving the turtle forward and turning it left three times. Finally, we call the end_fill() function to stop filling the colour and hide the turtle.
The output will be a blue triangle drawn on a new drawing board.
How to Determine If a Box Turtle Is Sick: A Comprehensive Guide
You may want to see also
Drawing the triangle using forward() and left() methods
Drawing a triangle using the forward() and left() methods is a straightforward process in Python Turtle. Here's a step-by-step guide:
- Import the Turtle Module: Begin by importing the turtle module, which is built into Python. This module provides the necessary functions for drawing and movement.
- Create a Turtle Object: Create a turtle object, which serves as the on-screen pen that you will use for drawing. You can name it whatever you like, for example, "turtle" or "ttl".
- Set the Colour: Before you start drawing the triangle, you need to set the colour you want to fill it with. Use the fillcolor() function followed by the desired colour name or hex value. For example, `turtle.fillcolor("blue")` or `turtle.fillcolor("#0000FF")` for a blue triangle.
- Begin Filling: Call the begin_fill() function to indicate that you want to start filling the subsequent lines with the chosen colour.
- Draw the Triangle: Use a for loop to iterate three times, which corresponds to the three sides of the triangle. In each iteration, use the forward() method to move forward by a specified distance, and then use the left() method to turn left by 120 degrees (the internal angle of an equilateral triangle).
- End Filling: After drawing the triangle, call the end_fill() function to stop filling subsequent lines with colour.
- Hide the Turtle (Optional): If you don't want the turtle to be visible on the screen, you can use the hideturtle() method to hide it.
Here's an example code snippet that demonstrates these steps:
Python
Import turtle
Create a turtle object
Ttl = turtle.Turtle()
Set the fill colour to blue
Ttl.fillcolor("blue")
Begin filling
Ttl.begin_fill()
Draw the triangle
For _ in range(3):
Ttl.forward(100) # Move forward by 100 pixels
Ttl.left(120) # Turn left by 120 degrees
End filling
Ttl.end_fill()
Hide the turtle (optional)
Ttl.hideturtle()
This code will draw an equilateral triangle filled with the colour blue. You can adjust the distance passed to the `forward()` method to change the size of the triangle. Additionally, you can experiment with different colours by passing different values to the `fillcolor()` function.
Who Manufactures Turtles Candy?
You may want to see also
Using the end_fill() function to stop filling the triangle
The `end_fill()` function in Python's turtle module is used to stop filling upcoming closed graphical objects. It is called after the `begin_fill()` function, which initiates the filling operation, and before the `fillcolor()` function, which defines the colour to fill the shape with.
The `end_fill()` function doesn't take any arguments and is used to indicate the end of the filling operation. It is important to place the drawing instructions between the `begin_fill()` and `end_fill()` functions for the filling to take effect.
Python
Import turtle
Define the filling colour
Turtle.fillcolor("pink")
Start the filling operation
Turtle.begin_fill()
Draw the triangle
For i in range(3):
Turtle.forward(150)
Turtle.left(120)
Stop the filling operation
Turtle.end_fill()
Keep the turtle window open
Turtle.exitonclick()
In this code, the `end_fill()` function is placed after drawing the triangle to indicate that the filling operation is complete. The triangle will be filled with the colour pink, as specified by the `fillcolor()` function.
It is important to note that the order of the `color()` and `begin_fill()` functions matters. If `color()` is placed after `begin_fill()`, the `fillcolor()` will be ignored, and the filling colour will be the same as the drawing colour.
Creating Turtles in Lines: A Beginner's Guide
You may want to see also
Hiding the turtle using hideturtle()
Hiding the turtle using the hideturtle() function in Python is a straightforward process. The hideturtle() function is used to make the turtle invisible during a drawing session. This is particularly useful when working on complex drawings, as hiding the turtle can speed up the rendering process.
Python
Import turtle
Hide the turtle
Turtle.hideturtle()
Your drawing code here
Show the turtle again
Turtle.showturtle()
It is worth noting that the default state of the turtle is to be visible. Therefore, if you choose to hide the turtle during your drawing, you will need to use the showturtle() function to bring it back into view.
Additionally, you can check the visibility of the turtle using the isvisible() function, which returns True if the turtle is visible and False if it is hidden.
Python
Import turtle
Hide the turtle
Turtle.hideturtle()
Check if the turtle is hidden
If turtle.isvisible():
Print("Turtle is visible")
Else:
Print("Turtle is hidden")
Show the turtle again
Turtle.showturtle()
The act of hiding the turtle icon can be particularly useful when you want to improve the visibility or aesthetics of your drawing. By hiding the turtle, you can focus solely on the lines and shapes being drawn without the distraction of the turtle icon moving around the screen.
Furthermore, the turtle module in Python provides turtle graphics primitives in both object-oriented and procedure-oriented ways. This means that you can use the hideturtle() function in either approach without any issues.
In conclusion, the hideturtle() function in Python is a valuable tool when creating complex drawings with the turtle module. It allows you to hide the turtle icon to improve rendering speed and the overall visibility of your artwork. Remember to use the showturtle() function to bring the turtle back into view when needed.
The Appropriate Number of Balls to Give a Baby Water Turtle
You may want to see also
Frequently asked questions
To make a triangle using Python Turtle, you can use the following code:
```python
import turtle
Set the colour of the triangle
turtle.fillcolor("blue")
Begin filling the triangle
turtle.begin_fill()
Draw the triangle
for i in range(3):
turtle.forward(150)
turtle.left(120)
End filling the triangle
turtle.end_fill()
Keep the turtle window open
turtle.exitonclick()
```
To change the colour of the triangle, you can modify the `fillcolor function and pass the desired colour as an argument. For example, to make the triangle red, you can use `turtle.fillcolor("red").
To make the triangle bigger or smaller, you can adjust the value passed to the `forward function inside the for loop. For example, to make the triangle bigger, you can use `turtle.forward(200) instead of `turtle.forward(150).
To fill the triangle with multiple colours, you can create separate triangles with different colours and overlay them on top of each other. You can also create a function that takes the colour as an argument and use it to draw the triangle.