Our Fifth Class!
Updated: Aug 2, 2020
July/29 was our fifth class, and we finally started to learn about Python Turtle library.
Below is the powerpoint we used for class!
Review of our fifth class:
1. What is Python Turtle?
Python Turtle is a built-in Python library that lets users create shapes and drawings using vector graphics.
In order to use the pre-defined functions in Turtle library, you need to import the library.
In Python, import is as simple as this:
Then, you can create an instance of turtle object by doing:
t = turtle.Turtle()
You can still run your functions without creating the object, but if you want to make use of multiple turtle objects, it is useful to do so.
NOTE: object in computer science refers to anything with values. Functions, variables, etc. are all objects.
This is similar to giving names to individual human beings. For example, there is Rachael, who is one instance of humans, and there is humans, which is a bigger class like turtle.
2. Running Python Turtle
Put the two lines into a new file and run it.
You should see a new window popping up. The window should say "Python Turtle Graphics," and you would see a small arrow at the center of a square window.
3. Python Turtle functions
There are numerous functions you can run using Turtle library.
Below are just some examples.
When you want to run these functions on specific instance of turtle object, you do:
t.shape("turtle") t.forward(100) t.xcor()
If you haven't declared your own instance, you can also simply run it on turtle itself:
4. Setting background color
By creating an instance of Screen object, you can set the background color.
import turtle s = turtle.Screen() s.bgcolor("orange")
5. Filling your shape
You can tell Turtle to fill your shape.
Specify color with .fillcolor("color") and .color("color"). Tell Python to start filling using .begin_fill(). and to stop using .end_fill().
t.color("yellow") t.fillcolor("yellow") t.begin_fill() for x in range(3): t.forward(100) t.left(120) t.end_fill()
There is also a good article for you to read, thanks to Mana!
Check it out!