雜亂大全16-(Python基礎系列)turtle繪圖練習

  • 前言:本次為turtle的基本練習
tags: 六角學院

主題:

雜亂大全16-(Python基礎系列)turtle繪圖練習

本篇重點:

  • 練習簡單的turtle繪圖

安裝模組

1
pip install PythonTurtle

使用

1
import turtle

練習-奧運五環

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#畫一個奧運五環
import turtle
turtle.speed(10)
turtle.up()
turtle.goto(50,0)
turtle.down()
turtle.pensize(8)
turtle.circle(100)

turtle.up()
turtle.goto(-170,0)
turtle.down()
turtle.pencolor("blue")
turtle.circle(100)

turtle.up()
turtle.goto(270,0)
turtle.down()
turtle.pencolor("red")
turtle.circle(100)

turtle.up()
turtle.goto(160,-120)
turtle.down()
turtle.pencolor("green")
turtle.circle(100)

turtle.up()
turtle.goto(-60,-120)
turtle.down()
turtle.pencolor("yellow")
turtle.circle(100)
turtle.done()

執行結果

練習-畫個青蛙臉

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# -*- coding: utf-8 -*-
"""
Spyder Editor

This is a temporary script file.
"""

# -*- coding: utf-8 -*-
"""
Created on Wed Oct 14 20:51:19 2020

@author: X509
"""
import turtle
a=turtle.Turtle()
turtle.bgcolor("pink")
a.goto(0,-100)
turtle.pensize(30)
#畫綠臉
a.begin_fill()
a.color("green")
a.circle(100)
a.end_fill()
#畫右邊的眼睛
a.penup()
a.goto(35,0)
a.begin_fill()
a.color("white")
a.circle(25)
a.end_fill()
#畫右邊的瞳孔
a.begin_fill()
a.color("black")
a.circle(17)
a.end_fill()
#畫左邊的眼睛
a.penup()
a.goto(-35,0)
a.begin_fill()
a.color("white")
a.circle(25)
a.end_fill()
#畫邊的瞳孔
a.begin_fill()
a.color("black")
a.circle(17)
a.end_fill()
a.penup()
#畫嘴
a.goto(-30,-30)
a.pendown()
a.begin_fill()
a.color("black")
a.forward(60)
a.right(120)
a.forward(60)
a.right(120)
a.forward(60)
a.end_fill()
turtle.mainloop()

練習-六邊形

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# -*- coding: utf-8 -*-
"""
Created on Mon Dec 7 13:01:27 2020

@author: liaozz
"""

import turtle

colors=["red","blue","green","yellow","pink","purple"]
#要處理的事情
shelly = turtle.Turtle()
turtle.speed('fastest')
turtle.bgcolor('black') # turn background black
# make 36 hexagons, each 10 degrees apart
for n in range(36):
# make hexagon by repeating 6 times
for i in range(6):
shelly.color(colors[i]) # pick color at position i
shelly.forward(100)
shelly.left(60)
# add a turn before the next hexagon
shelly.right(10)

# get ready to draw 36 circles
shelly.penup()

#結束

shelly.color('white')
# repeat 36 times to match the 36 hexagons
for i in range(4):
shelly.forward(220)
shelly.pendown()
shelly.circle(5)
shelly.penup()
shelly.backward(220)
shelly.right(10)


turtle.mainloop()

後續:

因為程式版本問題,Spyder在編譯2次後
turtle會出現沒有反應的現象
下篇將會講解處理辦法!