Python代码编程基础第10课 绚烂烟花

知识要点

  1. ondrag()函数
  2. 获取和设置海龟坐标值
  3. 设置海龟造型和大小
  4. 全局变量和局部变量

一、PPT课件

二、知识要点

1.ondrag()函数

绑定海龟和鼠标拖拽事件函数。

1
2
3
4
import turtle as t
def drawingFun(x,y):
t.goto(x+50,y)
t.ondrag(drawingFun)

2.获取和设置海龟坐标值

1
2
3
4
setx()   # 设置海龟横坐标数值
sety() # 设置海龟纵坐标数值
xcor() # 获取海龟横坐标数值
ycor() # 获取海龟纵坐标数值

3. 设置海龟造型和大小

shape() 修改海龟造型

shapesize() 改变海龟大小

1
2
3
4
5
import turtle as t

t.shape('square') # 海龟可选造型:"arrow","turtle","circle","square","triangle","classic"
t.shapesize(2,2)
t.color('blue')

4.全局变量和局部变量

global关键字声明全局变量

1
2
3
4
5
def addNum(x,y):
global answer
answer = x + y
addNum(2,3)
print(answer * 2)

三、代码示例

1.case1

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
# -*- coding: utf-8 -*-

# 展现给学生的烟花范例1

import turtle as t
import random


# 定义函数,实现功能:克隆多个海龟并且存入列表中


def createTurtleList():
for i in range(36):
q = t.Turtle()
q.right(10 * i)
q.pensize(6)
q.hideturtle()
turtlelist.append(q)


# 绘制并擦除图案的过程
def animation():
for j in range(36):
for sg in turtlelist:
c = random.randint(0, 255)
sg.color(c, 20, 30)
sg.right(10)
sg.forward(19)

for sg in turtlelist:
sg.clear()


# 改为RGB颜色模式
t.colormode(255)

# 设置背景色为黑色
t.bgcolor('black')

# 新建空列表
turtlelist = []

# 调用函数
t.tracer(56)
createTurtleList()

# 在循环中重复执行animation()函数实现动画效果
while True:
t.tracer(56)
animation()
t.tracer(0)

2.case2

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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# -*- coding: utf-8 -*-

# 烟花范例1 + colorMixer效果

import turtle as t
import random


# 定义函数,实现功能:克隆多个海龟并且存入列表中
def createTurtleList():
for i in range(36):
q = t.Turtle()
q.right(10 * i)
q.pensize(6)
q.hideturtle()
turtlelist.append(q)


# 绘制并擦除图案的过程
def animation():
for j in range(36):
for sg in turtlelist:
c = random.randint(0, 255)
sg.color(c, green, blue)
sg.right(10)
sg.forward(19)

for sg in turtlelist:
sg.clear()


# 定义createButton函数,功能:创建滑块
def createButton(tButton, x, y, red, green, blue):
tButton.showturtle()
tButton.shape('square')
tButton.shapesize(2, 2)
tButton.pensize(20)
tButton.color(red, green, blue)
tButton.pu()
tButton.goto(x, y)
tButton.left(90)
tButton.pd()
tButton.forward(255)


# 定义changeBlue函数,功能:改变蓝色值
def changeBlue(x, y):
global blue
blueButton.pu()

if y < 0:
yValue = 0
elif y > 255:
yValue = 255
else:
yValue = y

blueButton.sety(yValue)
b = blueButton.ycor()
blue = int(b)
blueButton.color(0, 0, blue)


# 定义changeGreen函数,功能:改变绿色值
def changeGreen(x, y):
global green
greenButton.pu()

if y < 0:
yValue = 0
elif y > 255:
yValue = 255
else:
yValue = y

greenButton.sety(yValue)
g = greenButton.ycor()
green = int(g)
greenButton.color(0, green, 0)


# 改为RGB颜色模式
t.colormode(255)

# 设置背景色为黑色
t.bgcolor(0, 0, 0)

# 新建空列表
turtlelist = []

# 调用函数
t.tracer(56)
createTurtleList()

# 初始化设置变量值
green = 0
blue = 0

# 创建两种颜色的滑块
t.tracer(0)
blueButton = t.Turtle()
createButton(blueButton, -350, 0, 0, 0, 255)

greenButton = t.Turtle()
createButton(greenButton, -300, 0, 0, 255, 0)

# 鼠标事件监听
blueButton.ondrag(changeBlue)
greenButton.ondrag(changeGreen)

# 在循环中重复执行animation()函数实现动画效果
while True:
t.tracer(56)
animation()
t.tracer(0)

3.homework

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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# -*- coding: utf-8 -*-

# 本节课作业参考代码

import turtle as t
import random


# 定义函数,实现功能:克隆多个海龟并且存入列表中
def createTurtleList():
for i in range(36):
q = t.Turtle()
q.right(10 * i)
q.pensize(6)
q.hideturtle()
turtlelist.append(q)


# 绘制并擦除图案的过程
def animation():
global s
for j in range(36):
for sg in turtlelist:
c = random.randint(0, 255)
sg.color(c, green, blue)
sg.right(10)
sg.forward(step)

for sg in turtlelist:
sg.clear()

# 让所有海龟都移动到原点、且转过一定角度
for sg in turtlelist:
sg.pu()
sg.goto(0, 0)
sg.pd()
sg.right(10 * s)
s = s + 1


# 定义createButton函数,功能:创建滑块
def createButton(tButton, x, y, red, green, blue, degree, length):
tButton.showturtle()
tButton.shape('square')
tButton.shapesize(2, 2)
tButton.pensize(20)
tButton.color(red, green, blue)
tButton.pu()
tButton.goto(x, y)
tButton.left(degree)
tButton.pd()
tButton.forward(length)


# 定义changeBlue函数,功能:改变蓝色值
def changeBlue(x, y):
global blue
blueButton.pu()

if y < 0:
yValue = 0
elif y > 255:
yValue = 255
else:
yValue = y

blueButton.sety(yValue)
b = blueButton.ycor()
blue = int(b)
blueButton.color(0, 0, blue)


# 定义changeGreen函数,功能:改变绿色值
def changeGreen(x, y):
global green
greenButton.pu()

if y < 0:
yValue = 0
elif y > 255:
yValue = 255
else:
yValue = y

greenButton.sety(yValue)
g = greenButton.ycor()
green = int(g)
greenButton.color(0, green, 0)


# 定义changeDegree函数,功能:控制烟花里每一条线转过角度的大小
def changeDegree(x, y):
global step
degreeButton.pu()

if x < -300:
xValue = -300
elif x > -100:
xValue = -100
else:
xValue = x

degreeButton.setx(xValue)
step = degreeButton.xcor()
step = int(- step / 10)
degreeButton.color(20, step * 10, 120)


# 改为RGB颜色模式
t.colormode(255)

# 设置背景色为黑色
t.bgcolor(0, 0, 0)

# 新建空列表
turtlelist = []

# 调用函数
t.tracer(56)
createTurtleList()

# 初始化设置变量值
green = 0
blue = 0
step = 10
s = 0

# 创建两种颜色的滑块 + 创建调整角度的滑块
t.tracer(0)
blueButton = t.Turtle()
createButton(blueButton, -350, 0, 0, 0, 255, 90, 255)

greenButton = t.Turtle()
createButton(greenButton, -300, 0, 0, 255, 0, 90, 255)

degreeButton = t.Turtle()
createButton(degreeButton, -100, 350, 0, 200, 0, 180, 200)
degreeButton.pu()
degreeButton.goto(-100, 350)

# 鼠标事件监听
blueButton.ondrag(changeBlue)
greenButton.ondrag(changeGreen)
degreeButton.ondrag(changeDegree)

# 在循环中重复执行animation()函数实现动画效果
while True:
t.tracer(56)
animation()
t.tracer(0)