1.
Construct a program that fills a green triangle using begin_fill and end_fill using the example code above as a guide.
TurtleScreen
to be used. This is a significant difference from Python, as you are required to create your own screen before creating a Turtle
object.
ct::TurtleScreen screen;
ct::Turtle turtle(screen);
//Notice how the Screen is given to our Turtle when we create it.
TurtleScreen
works exactly how it does in Python. For this chapter, only bye
is used. Calling it is not completely necessary, as it is also called automatically if it, or an equivalent method, hasn’t been called. When working outside of the textbook, the exitonclick
method is also available.
screen.bye();
TurtleScreen
. This Turtle will follow any command you give it, which consist of telling it to go certain directions, what color of pen to use, when to raise or lower its pen, and others. Below is an outline of commonly used methods when working with turtles.
Method Name | Description |
---|---|
turtle.left | turns the turtle a certain number of units to the left. |
turtle.right | turns the turtle a certain number of units to the right. |
turtle.penup | raises the paint pen on the end of the turtle’s tail. |
turtle.pendown | lowers the paint pen on the end of the turtle’s tail. |
turtle.fillcolor | tells the turtle what color the inside of the shape will be. |
turtle.beginfill | tells the turtle to begin filling a shape as it moves. |
turtle.endfill | tells the turtle to finish filling the shape it has created as it moved. |
turtle.pencolor | tells the turtle what color it will draw with. |
turtle.width | tells the turtle how large of a paint pen to use. |
turtle.speed | tells the turtle how fast it should go, faster or slower than the hare. |
turtle.back | moves the turtle back a number of units. |
turtle.forward | moves the turtle forward a number of units. |
turtle.goto | tells the turtle to move to a specific coordinate. |
turtle.write | tells the turtle to write some kind of text. |
speed
settings you may be familiar with from Python are also available in CTurtle. All speeds are measured on a range of 1 to 10, the latter being the fastest and the former being the slowest. The exception is the fastest speed, TS_FASTEST
, which is set to 0 just as it is for Python’s equivalent "fastest"
. The TS
prefix represents “Turtle Speed”.
Python Turtle Name | C-Turtle Name | Speed |
---|---|---|
“fastest” | TS_FASTEST | 0 |
“fast” | TS_FAST | 10 |
“normal” | TS_NORMAL | 6 |
“slow” | TS_SLOW | 3 |
“slowest” | TS_SLOWEST | 1 |
begin_fill
and end_fill
pattern, which must be called in that specified order to actually fill a shape.