Python Turtle Basics: Canvas Setup, Pen Control, Drawing, Filling, and Animated Balloons
This tutorial introduces Python's built‑in Turtle graphics library, explaining how to configure the drawing canvas, use pen functions, draw shapes, apply fill colors, write text, and create animated balloon objects with movement and rendering loops for a festive visual program.
The article begins with a brief introduction to the Turtle module, a built‑in Python package for creating graphics, and encourages readers to explore its drawing capabilities.
Turtle Canvas : The canvas size is set using turtle.setup(width, height) , allowing custom dimensions for the drawing area.
Pen Functions : Common pen operations are demonstrated, including turtle.penup() , turtle.pendown() , turtle.pensize() , turtle.pencolor() , and turtle.hideturtle() to control visibility.
Movement Functions : Simple movement commands such as turtle.forward(x) , turtle.backward(x) , turtle.left(n) , turtle.right(n) , and turtle.speed() are presented for positioning the pen.
Filling Shapes : To fill drawn shapes, the sequence turtle.beginfill() , turtle.fillcolor('color') , and turtle.endfill() is used.
Writing Text : Text can be added with turtle.write("text", move, align, font) , where parameters control movement, alignment, and font styling.
Balloon Class : A class Balloon is defined with attributes for radius, position, color, speed, and sinusoidal horizontal motion. The move() method updates the balloon's coordinates, resetting it when it exits the canvas.
Drawing Balloons : The draw() method uses Turtle commands to render each balloon, including pen control, color setting, filling, and additional decorative circles.
Complete Program :
<code>import turtle as tu
import random as ra
import math
tu.setup(1.0, 1.0)
t = tu.Pen()
t.ht()
colors = ['red','skyblue','orange','yellow','lime','pink','violet']
class Balloon():
def __init__(self):
self.r = ra.randint(12,20)
self.x = ra.randint(-1000,1000)
self.y = ra.randint(-500,500)
self.f = ra.uniform(-3.14,3.14)
self.speed = ra.randint(5,10)
self.color = ra.choice(colors)
self.outline = 1
def move(self):
if self.y <= 500:
self.y += self.speed
self.x += self.speed * math.sin(self.f)
self.f += 0.1
else:
self.r = ra.randint(12,20)
self.x = ra.randint(-1000,1000)
self.y = -500
self.f = ra.uniform(-3.14,3.14)
self.speed = ra.randint(5,10)
self.color = ra.choice(colors)
def draw(self):
t.penup()
t.goto(self.x,self.y)
t.pendown()
t.color(self.color)
t.left(45)
t.begin_fill()
t.fillcolor(self.color)
for i in range(2):
t.circle(self.r*2,90)
t.circle(self.r,90)
t.end_fill()
t.hideturtle()
t.circle(self.r,-45)
t.right(90)
t.circle(20,90)
Balloons = []
for i in range(100):
Balloons.append(Balloon())
tu.bgcolor('black')
while True:
tu.tracer(0)
t.clear()
for i in range(50):
Balloons[i].move()
Balloons[i].draw()
tu.penup()
tu.goto(-250,20)
tu.pendown()
tu.color('skyblue')
tu.write('六一快乐!',font=('黑体',80,'italic'))
tu.hideturtle()
tu.update()
tu.mainloop()</code>The script creates 100 balloon objects, animates 50 of them on a black background, and displays a festive "六一快乐!" greeting, demonstrating basic animation, object‑oriented programming, and Turtle graphics techniques.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.