SoFunction
Updated on 2024-11-20

Python Drawing Exercise Case Sharing

1. Polygon drawing case

# Examples of polygon drawing
import turtle
def main():
("green")
# steps represents the drawing of polygons
(50,steps=6)
()
if __name__ == "__main__":
main()

2. Sunflower case

# Gerbera Cases *******************************************************************
import turtle
import time
("red","yellow")
turtle.begin_fill()
for _ in range(50):
(0)
(200)
(170)
turtle.end_fill()
()

3. Color pentagram case

# Color Pentagram Case ******************************************************************
import turtle
import time
(5)
("yellow")
("red")
turtle.begin_fill()
for _ in range(5):
(200)
(144)
turtle.end_fill()
(2)
()
(-150,-120)
("violet")
("Done",font=("Arial"))
()

4. Artistic pictures

# Art Pictures *************************************************************************
import turtle
(0)
(0)
(2)
("black")
colors=["red","blue","yellow","purple"]
for x in range(300):
(colors[x%4])
(2*x)
(91)
()

5. Black hexagon

#blackhexagon *****************************************************************************
import turtle
def bye(x,y):
()
s = ()
("black")
(800,800)
("Class Using")
(bye)
p=()
(0)
()
("red")
(3)
(50,360,6)
()

high energy ahead

6. Drawing clocks

#drawclock ************************************************************************************************
import turtle as tt
from datetime import *
# What day of the week is the current date
def Week(t):
week = ["Monday.", "Tuesday.", "Wednesday.", "Thursday.", "Friday.", "Saturday.", "Sunday."]
return week[()]
# Get the current time
def Date(t):
y = 
m = 
d = 
cur_hour = ;
cur_min = ;
cur_sec = ;
return "%s-%d-%d %d:%02d:%02d" % (y, m, d, cur_hour, cur_min, cur_sec)
# Move the brush with distance
def movePen(distance):
()
(5)
("blue")
(distance)
()
# Drawing the watch hand
def makeHands(name, length):
# Empty the window and reset the turtule state to the initial state
()
movePen(-length * 0.1)
# Start recording the vertices of the polygon
tt.begin_poly()
(length * 1.1)
# Stop recording polygon vertices
tt.end_poly()
# Return the recorded polygon
handForm = tt.get_poly()
tt.register_shape(name, handForm)
# Initialization
def initial():
global secHand, minHand, hurHand, printer
# Reset direction north (up), positive angle is clockwise
("logo")
# Create and initialize the meter pin
makeHands("secHand", 180)
makeHands("minHand", 150)
makeHands("hurHand", 110)
secHand = ()
("secHand")
minHand = ()
("minHand")
hurHand = ()
("hurHand")
for hand in secHand, minHand, hurHand:
(1, 1, 4)
(0)
# Output text
printer = ()
# Hide the brushes
()
()
# Drawing the dial bezel
def drawClock(R):
# Empty the window and reset the turtule state to the initial state
()
# Brush size
(5)
for i in range(60):
movePen(R)
if i % 5 == 0:
(20)
movePen(-R - 20)
movePen(R + 20)
if i == 0:
# Write text
(int(12), align="center", font=("Consolas", 14, "bold"))
elif i == 30:
movePen(25)
(int(i / 5), align="center", font=("Consolas", 14, "bold"))
movePen(-25)
elif (i == 25 or i == 35):
movePen(20)
(int(i / 5), align="center", font=("Consolas", 14, "bold"))
movePen(-20)
else:
(int(i / 5), align="center", font=("Consolas", 14, "bold"))
movePen(-R - 20)
else:
# Draw points of specified radius and color
(5, "red")
movePen(-R)
(6)
# Dynamic display of hands
def handsMove():
t = ()
second =  +  * 0.000001
minute =  + second / 60.0
hour =  + minute / 60.0
(6 * second)
(6 * minute)
(30 * hour)
(False)
(65)
("green")
(Week(t), align="center", font = ("bold.", 14))
(130)
(Date(t), align="center", font = ("Consolas", 14))
# Set the current brush position to the origin with the direction facing east
()
(True)
# After 100ms continue to call the handsMove function
(handsMove, 100)
# Calls defined functions that turn animations on and off, setting delays for updating drawings;
(False)
initial()
drawClock(200)
(True)
handsMove()
()

7. Drawing fractal trees

# Drawing Fractal Trees ******************************************************************************
import turtle
def draw_branch(branch_length):
'''
Drawing fractal trees
'''
if branch_length > 5:
# Draw the right branch
(branch_length)
print("Forward:", branch_length)
(20)
print("Right turn: 20 degrees.")
draw_branch(branch_length - 15)

# Draw the left branch
(40)
print("Left turn: 40 degrees.")
draw_branch(branch_length - 15)
# Return to the previous branch
(20)
print("Right turn: 20 degrees.")
(branch_length)
print("Backwards:", branch_length)
def main():
'''
Main Functions
'''
(0.5)
(3)
(90)
('green')
()
(150)
()
("black")
draw_branch(100)
()
if __name__ == "__main__":
main()

8. Rainbow line drawing case

# Rainbow line drawing examples ***************************************************************************
import turtle as t
from random import randint as rint
("turtle")
(5)
(255)
("black")
(False)
for x in range(700):
(rint(0,255),rint(0,255),rint(0,255))
(2*(1+x/4),5)
(0)
(True)
()

To this point this article on Python drawing practice cases to share the article is introduced to this, more related Python drawing content, please search for my previous posts or continue to browse the following related articles I hope you will support me in the future!