[Python] Chapter 03-02 turtle graphics (turtle behavior)

[Python] Chapter 03-02 Turtle Behavior

Last time, in Chapter 3-01, I created a turtle and finished. This time I would like to move that turtle.

The program handled in Chapter 3-01 will be used. Last time, I created a file with the file name 03-01-01.py </ font> in chap03 </ font>, and the following I wrote the code for.

03-01-01.py


#Loading a program from the outside to run the turtle program
import turtle

#Make a turtle with the name taro.
taro = turtle.Turtle()

#Make the shape a turtle and the color green
taro.shape('turtle')
taro.color('green')

#Stop the program here.
turtle.done()

This time, in the chapter, put the program in chap03 </ font> with the file name 03-02-01.py </ font>. I would like to create it. Create 03-02-01.py </ font>, copy the program of 03-01-01.py </ font>, and Paste it in 03-02-01.py </ font>.

03-02-01.py


#Loading a program from the outside to run the turtle program
import turtle

#Make a turtle with the name taro.
taro = turtle.Turtle()

#Make the shape a turtle and the color green
taro.shape('turtle')
taro.color('green')

#Stop the program here.
turtle.done()

Move the turtle

Now let's move the turtle (named taro). Before that, let's check one. I think you learned functions in junior high school math, but taro is in the (0, 0) position first.

And, as I did last time, it is facing right as shown below, so it is facing the x-axis direction (right direction). image.png

Therefore, when you first move it, it will move to the right. Let's actually move it to the right. Add the following code.

03-02-01.py


#Loading a program from the outside to run the turtle program
import turtle

#Make a turtle with the name taro.
taro = turtle.Turtle()

#Make the shape a turtle and the color green
taro.shape('turtle')
taro.color('green')

#Move the turtle 200 in the x-axis direction
taro.forward(200)   #<<<< Addition

#Stop the program here.
turtle.done()

Then try it. I think taro has moved 200 to the right.

image.png

The taro.forward (* distance *) method allows you to enter a distance in * distance * and move it forward by that amount. For details, see the manual for the forward method below.

  • Forward () can also be done with fd ().

forward () method

Then, change the value of * distance * to see if the distance traveled changes.

Slow down

I think I've confirmed the movement of taro now, but it's a little faster (even though it's a turtle), so I'd like to move it slowly.

Let's enter the following speed () method before the forward () method that we entered earlier.

#Slow down the turtle
taro.speed(speed=1) 

speed () method

[From the manual] Set the turtle speed to an integer in the range 0-10. If no arguments are given, the current speed is returned.

Change the direction of the turtle

I was able to move the turtle, but it's only moving in one direction. Consider changing direction.

03-02-01.py


#Loading a program from the outside to run the turtle program
import turtle

#Make a turtle with the name taro.
taro = turtle.Turtle()

#Make the shape a turtle and the color green
taro.shape('turtle')
taro.color('green')

#Slow down
taro.speed(speed=1) 

#Move the turtle 200 in the x-axis direction
taro.forward(200)
taro.left(90)    #<Addition

#Stop the program here.
turtle.done()

Then, at the end, I think that the turtle rotated 90 ° counterclockwise. image.png

The taro.left (* angle *) method allows you to enter an angle in * angle * and rotate it by that amount. For details, see the manual for the left method below.

left () method

[From the manual] Turn the turtle counterclockwise by angle. (The default unit is degrees, but you can set it using the degrees () and radians () functions.) The angle orientation depends on the mode of the turtle. See mode ().

  • Left () can also be used with lt ().

In addition, although I will not deal with it this time, if there is a left side, there is also a right side, so please try it if you like.

right () method [From the manual] Turn the turtle clockwise by angle. (The default unit is degrees, but you can set it using the degrees () and radians () functions.) The angle orientation depends on the mode of the turtle. See mode ().

  • Right can also be rt ().

Make a square

Now that we've created it, let's create a square. Enter the code below.

#Loading a program from the outside to run the turtle program
import turtle

#Make a turtle with the name taro.
taro = turtle.Turtle()

#Make the shape a turtle and the color green
taro.shape('turtle')
taro.color('green')

#Slow down
taro.speed(speed=1)

#Move the turtle 200 in the x-axis direction
taro.forward(200)
taro.left(90)
taro.forward(200)   #<Addition
taro.left(90)   #<Addition
taro.forward(200)   #<Addition
taro.left(90)   #<Addition
taro.forward(200)   #<Addition
taro.left(90)   #<Addition

#Stop the program here.
turtle.done()

Then, a square will be created as shown below. image.png

Practice problem

-Create a program that draws the following equilateral triangles. (One side is 200) </ font> image.png

-Create a program that draws the following equilateral triangles. (One side is 100, the thickness of the pen is 5.) There's one method I haven't learned, but let's look at it. Method link image.png

Finally

This time I actually moved the turtle. As you can see from the manual, I think there were various processes. At first, there may be some terms that you may not understand even if you look at the manual, but as you read them, you will get used to them. Actually, it can be said that most of the work such as reading a manual and examining it is more than writing a program in practice **. Read the manual now and get used to it. Programmers think it's their job to find out.

Then read the manual and try different movements.

Return to [Table of Contents Link]

Recommended Posts