这是风火轮编程Python初级教程的第九课,下面是用python制作的学习大纲。
用Python制作的幻灯片类型的作品,本课程主要学习如何用python的海龟模块制作一个孙悟空的如意金箍棒,以下是完整源代码。
本程序需要sprites模块支持,安装方法为在命令提示符下输入以下命令安装:
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple sprites –upgrade
"""
第九课 如意金箍棒(屏幕与方向)
"""
from sprites import *
s = '第九课 如意金箍棒(屏幕与方向)'
screen = Screen()
screen.bgcolor('blue')
screen.titlebar(False)
root = screen._root # 窗口对象
root.wm_attributes('-alpha',0.7) # 设置窗口为全透明(0到1.0)
screen.setup(800,800)
screen.tracer(0,0)
# 下面的代码让窗口可以拖动.
oldx = 0
oldy = 0
def startmove(event):
global oldx,oldy
oldx = event.x
oldy = event.y
def stopmove(event):
global oldx,oldy
oldx = 0
oldy = 0
def movewindow(event):
global oldx,oldy
dx = event.x - oldx
dy = event.y - oldy
root.move(dx,dy)
screen.cv.bind("", startmove)
screen.cv.bind("", stopmove)
screen.cv.bind("",movewindow)
# 下面的代码按方向箭头则窗口能上下左右移动
screen.onkeypress(lambda:root.move(10),'Right')
screen.onkeypress(lambda:root.move(-10),'Left')
screen.onkeypress(lambda:root.move(0,-10),'Up')
screen.onkeypress(lambda:root.move(0,10),'Down')
screen.listen()
ft_title = ('楷体',30,'bold')
ft_context = ('宋体',18,'normal')
t = Sprite(visible=False,pos=(-500,100))
t.color('magenta')
clock = Clock()
for x in range(100):
t.clear()
t.write(s,align='center',font=ft_title)
t.wait()
t.fd(5)
clock.tick(60)
spacekey = Key('space') # 空格键
m1 = Mouse() # 鼠标左键
while not m1.down():screen.update()
# 简介
brief ="""
这节课我们要学习如何画一根如意金箍棒。
更重要的是,还要知道海龟画图的起源。
还有屏幕与设置屏幕与方向的相关命令。
"""
if brief!='': # 如果有课程简介,则生成一个角色,
ftx = ('宋体',18,'normal')
简介 = Sprite(visible=False,pos=(0,-100))
简介.color('white')
简介.write(brief,align='center',font=ftx)
while m1.down():screen.update()
while not m1.down():screen.update()
for x in range(66): # 标题向右消失
t.clear()
t.write(s,align='center',font=ft_title)
t.fd(10)
if brief!='':
简介.clear()
简介.write(brief,align='center',font=ftx)
简介.bk(10)
clock.tick(60)
简介.clear()
#以下是显示学习的内容段
studycontent = """
主要学习内容
1、海龟画图
2、屏幕与画布
3、背景色和宽高
4、方向与设定方向
5、如意金箍棒
6、作业与练习
"""
t.color('white')
t.clear()
t.sety(-260) # 这里修改菜单的显示y坐标
ft = ('楷体',24,'bold')
s = studycontent
while not m1.down():screen.update()
# 下面的代码显示主菜单
for x in range(130):
t.clear()
t.write(s,align='center',font=ft)
t.bk(5)
clock.tick(60)
def slow_write(t,string):
"""
t:角色,string:要显示的字
本函数慢慢的显示字。
"""
string = string.split('\n') # 换成列表
oldxy = t.position() # 记录老的坐标
t.goto(-360,330) # 定位到标题的坐标
# 显示标题,注意string[0]是一个空行
t.write(string[1],font=ft_title)
t.addy(-50)
for line in string[2:]: # 每一行字
for char in line: # 每一个字符
t.write(char,align='center',font=ft_context)
t.wait(0.1)
cd = len(bytes(char,'gb2312'))
if cd == 1:
t.addx(16)
else:
t.addx(24)
t.setx(-360)
t.addy(-30)
t.goto(oldxy)
s1 = """
1、海龟画图
海龟画图这个名字源于上个世纪60年代的logo计算
机语言。logo计算机语言是至今还在使用的教编程
入门的一种计算机语言。它通过输入命令指挥一只
小海龟移动,从而能够画出漂亮的图形而得名。如,
输入fd 100,就能让小海龟前进100个单位。
"""
def press1():
t.clear()
slow_write(t,s1)
while not spacekey.down():screen.update()
t.clear()
t.write(s,align='center',font=ft)
screen.onkeypress(press1,'1')
s2 = """
2、屏幕与画布
海龟移动所在的地方叫屏幕。在屏幕上有画布,
有滚动条,海龟是在画布上进行绘画的。
当屏幕变小时,滚动条才会自动出现。
获取屏幕可以用getscreen命令。
下面的命令就能获取屏幕对象,我们把它取名为sc。
import turtle
sc = turtle.getscreen()
"""
def press2():
t.clear()
slow_write(t,s2)
while not spacekey.down():screen.update()
t.clear()
t.write(s,align='center',font=ft)
screen.onkeypress(press2,'2')
s3 = """
3、背景色和宽高
我们可以给屏幕设定背景颜色及屏幕的宽度和高度。
设定背景色的命令为bgcolor,
假设屏幕对象的名称为screen,那么
screen.bgcolor('green') 就能把背景色设为绿色。
设定宽高的命令为setup,用法为screen.setup(宽度,高度)。
如screen.setup(480,360),会设定屏幕的宽高为480x360。
"""
def press3():
t.clear()
slow_write(t,s3)
while not spacekey.down():screen.update()
t.clear()
t.write(s,align='center',font=ft)
screen.onkeypress(press3,'3')
s4 = """
4、方向与设定方向
在海龟画图中,海龟默认的方向为向右。
它的方向值为0度。可以用left命令让它向左旋转90度,
这个时候它的方向值为90度。
如果继续用left命令让它向左旋转90度,
它的方向值就变成了180度。
继续向左旋转90度,则方向值变成了270度。
再继续向左旋转90度,则方向值为360度。
这个时候它回到了初始的方向。
360度的方向也就是0度的方向。
我们可以看出当用left旋转海龟的时候,
海龟的方向值是增加的。反过来,
用right命令旋转海龟的时候,方向值是减小的。
如果要设定方向,可以用setheading命令,
简写形式为seth。用法:
海龟.setheading(方向值)
"""
def press4():
t.clear()
slow_write(t,s4)
while not spacekey.down():screen.update()
t.clear()
t.write(s,align='center',font=ft)
screen.onkeypress(press4,'4')
s5 = """
5、如意金箍棒
编制程序,画孙悟空的如意金箍棒。
我们将要从下往上画这个图形,以下是主要步骤:
导入海龟模块
获取屏幕对象
设定屏幕为黑
设定屏幕宽高
设定画笔粗细
设定画笔为向上
让海龟退100个单位
设定画笔颜色为橙色
前进50个单位
设定画笔颜色为红色
前进150个单位
设定画笔颜色为橙色
前进50个单位
"""
def press5():
t.clear()
slow_write(t,s5)
while not spacekey.down():screen.update()
t.clear()
t.write(s,align='center',font=ft)
screen.onkeypress(press5,'5')
s6 = """
6、作业与练习
★ 你能说说屏幕和窗口的区别吗?
★ -90度的方向是朝向上、下、左、右中的哪一个方向呢?
★ 修改如意金箍棒程序,让它变成斜的,
颜色、长度和背景色都不一样。
"""
def press6():
t.clear()
slow_write(t,s6)
while not spacekey.down():screen.update()
t.clear()
t.write(s,align='center',font=ft)
screen.onkeypress(press6,'6')
byebye = """
下次再见!
"""
def pressq():
t.clear()
t.color('cyan')
t.home()
t.write(byebye,align='center',font=('宋体',38,'bold'))
while not spacekey.down():screen.update()
screen.bye()
screen.onkeypress(pressq,'q')
screen.mainloop()
以下是付费内容,包括播放视频、python源代码及素材等等。
需要浏览更多吗?
成为会员后,登陆才能继续浏览!联系微信scratch8即可办理会员。
(会员专属:能浏览所有文章,下载所有带链接的Python资源。)

