求学者

Python练习题(二)-画图形

题目

来源: 优达学城–编程基础:python
内容:使用 turtle 画出你想象。
效果演示:
python_show2.png

mindstorms.gif

我的解法

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
61
62
63
64
65
66
67
68
69
70
71
72
73
import turtle
#def draw_name():
def draw_h(pen):
pen.down()
pen.right(90)
pen.forward(100)
pen.backward(50)
pen.left(90)
pen.forward(50)
pen.right(90)
pen.forward(50)
pen.backward(100)
pen.left(90)
def draw_j(pen):
pen.up()
pen.forward(25)
pen.down()
pen.forward(50)
pen.backward(25)
pen.right(90)
pen.forward(100)
pen.right(90)
pen.forward(25)
pen.right(90)
pen.forward(25)
pen.right(90)
def draw_y(pen):
x = pen.xcor()
y = pen.ycor()
pen.goto(x+25,y-50)
pen.goto(x+50,y)
pen.goto(x+25,y-50)
pen.right(90)
pen.forward(50)
pen.left(90)
def draw_flower(pen):
for i in range(0,36):
for i in range(0,2):
pen.forward(50)
pen.right(60)
pen.forward(50)
pen.right(120)
pen.right(10)
pen.right(90)
pen.forward(200)
def draw_art():
pen = turtle.Turtle()
pen.color("yellow")
pen.shape("turtle")
pen.speed(8)
window = turtle.Screen()
window.bgcolor("red")
draw_h(pen)
pen.up()
pen.forward(10)
draw_j(pen)
pen.up()
pen.goto(170,0)
pen.down()
draw_y(pen)
pen.up()
pen.goto(-100,100)
pen.down()
draw_flower(pen)
window.exitonclick()
draw_art()
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
import turtle
def draw_circle(angie):
angie.circle(50)
def draw_triangle(triangle):
for y in range(0,3):
triangle.forward(100)
triangle.right(120)
def draw_square(turtle):
for i in range(0,4):
turtle.forward(100)
turtle.right(90)
def draw_art():
window = turtle.Screen()
window.bgcolor("red")
brad = turtle.Turtle()
brad.shape("turtle")
brad.speed(15)
# 循环方形
for i in range(0,36):
brad.color("pink")
draw_triangle(brad)
brad.color("blue")
draw_circle(brad)
brad.color("yellow")
draw_square(brad)
brad.right(10)
brad.right(90)
brad.forward(200)
window.exitonclick()
# 方形画圆
draw_art()
# 添加音乐

步骤

  1. 获取画布turtle.Screen(),设置背景色
  2. 获取画笔 turtle.Turtle(),设置画笔形状颜色速度等
  3. 画出想要的图案

源码地址

Python-Exercies