[Python] Chapter 03-01 turtle graphics (creating a turtle)

[Python] Chapter 03-01 Creating a turtle

In this chapter, I would like to use the knowledge of variables and methods that I have learned so far to create graphics within the range I have learned.

In the future, we will be deeply asked about object-oriented stories and methods learned in Python. I think that using graphics will deepen your understanding in learning them.

Turtle graphics are often used in graphics. Turtle can be written if you know the basic grammar of Python.

Programming is essential even in elementary and junior high schools

From 2020, programming has become mandatory in elementary and junior high schools. A programming tool called scratch is often used there. The aim of this programming tool is to develop the ability of the algorithm (*) </ font>.

The (*) </ font> algorithm is a processing procedure performed by a program or the like. I will omit the details, but it will be important for writing the program.

If you are interested, you can operate it, so please give it a try. (free) Scratch site

As with that scratch, scratch is a tool used in elementary and junior high schools, as mentioned above. The graphics are not particularly difficult to operate, but let's consider working with Python here.

Make a turtle

There are some program codes that I haven't learned, but I'll add comments and details later, so for the time being ** I don't think it's magic right now **.

For this program, I would like to create the program code with an editor and execute it. Create a folder chap03 </ font> and create a file name 03-01-01.py </ font> in it.

First, enter the following code at the top.

03-01-01.py


import turtle

This means that the parts of the program (called ** module **) are loaded (imported) from the outside when executing the turtle program. Details will be described later in a later chapter. For now, you can recognize that ** you are importing the program from the outside to run the turtle program **.

03-01-01.py


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

The next one will be like a spell, but ** you have to make the essential turtle **. Therefore, create a turtle as follows. It might be a good idea to give the turtle a name. (I named it taro this time, but it doesn't matter if you are here)

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()

Then try it. Then, I think that a white screen appeared for a moment. If this is the case, the execution result will be closed immediately, so write turtle.done () in the following places.

This allows you to stop the program once at turtle.done (). This is ** written at the end. ** **

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()

#Stop the program here.
turtle.done()

When executed, the result is as follows. image.png

This is the shape of an arrow, not the shape of a turtle. Also, since turtles are originally green, I would like to change their shape and color.

Processing instructions for turtles

Now I would like to change the shape and color of the turtle. Enter the code below.

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()

When you run it, you should be able to change the shape and color of the turtle as shown below. image.png

To close it, click the "x" button on the upper right.

By the way, have you seen these two added lines somewhere? Recall that we dealt with string methods in Chapter 2.

str.count('r')
str.lower()

I think there was a method like that. Actually, these are the processing instructions for str and I mentioned in Chapter 2, but this time as well, I am giving some processing instructions to the turtle called taro.

This time, we are issuing a processing command to "set the shape to'turtle'and the color to'green'".

I would like to actually move it next time. However, in the end it is the method that works, so I would like to learn the basic method for running turtle next time.

Finally

There is some unfamiliar program code this time around, but these are important and will always be discussed in later chapters. There are many other shapes and colors for the taro.shape () and taro.color () methods that we learned this time. There are details on the following sites, so please try changing the shape and color. shape () method

color () method

Return to [Table of Contents Link]

Recommended Posts