这是风火轮编程Python初级教程的第15课,下面是用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()
# 单击鼠标左键开始
spacekey = Key('space') # 空格键
m1 = Mouse() # 鼠标左键
while not m1.down():screen.update()
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)
while not spacekey.down():screen.update()
# 简介
brief ="""
本课主要学习的是坐标和随机数模块,附带学习设定屏幕的背景图片命令
和获取海龟的方向,即heading命令。关于坐标的命令稍微多点,
先记住几个单词,如set是设置的意思,position是位置的意思,
cor是coordinates,即坐标的前三个字母,bg是background的缩写。
而pic则是picture,即图片的前三个字母。
"""
if brief!='': # 如果有课程简介,则生成一个角色,
ftx = ('宋体',18,'normal')
简介 = Sprite(visible=False,pos=(0,-200))
简介.color('white')
简介.write(brief,align='center',font=ftx)
while spacekey.down():screen.update()
while not spacekey.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、屏幕的bgpic命令
2、坐标的概念
3、坐标相关命令
4、随机模块
5、实战小程序(多彩散射条)
6、练习与作业
"""
t.color('white')
t.clear()
t.sety(-260) # 这里修改菜单的显示y坐标
ft = ('楷体',24,'bold')
s = studycontent
while not spacekey.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、屏幕的bgpic命令
海龟画图的屏幕有bgpic命令,能设定屏幕的背景图片。
假设有名为bg.png的图片在d盘根目录和名为screen的屏幕,
那么用screen.bgpic('d:/bg.png')就能设置背景图片。
注意图片要为png或gif格式,不支持jpg和jpeg扩展名的图像。
"""
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、坐标的概念
坐标是用来精确表示位置的一对数据。
坐标有两根轴,一根是水平轴叫x轴,一根是垂直轴叫y轴。
它们的交叉点叫原点,原点的坐标是(0,0)。
每个点的x值表示到y轴的最短距离,y值表示到x轴的最短距离。
"""
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、坐标相关命令
setx:设定x坐标,
sety:设定y坐标,
setposition:设置x,y坐标,别名是setpos和goto,
xcor:获取x坐标, ycor:获取y坐标
position:获取x,y坐标,别名是pos
"""
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、随机模块
除了time模块和turtle模块,Python还有个叫random的模块。
这个模块可以用来产生随机数。如randint命令能产生某范围内随机
的一个整数。用法为randint(起始值,结束值)。
还有choice命令能从序列中随机挑一个数据。用法为choice(序列)。
使用随机数模块有基本的两种方式:
导入方式一,import random
此时使用randint命令的形式为:random.randint(起始,结束)
随机模块导入方式二,from random import *
这个时候可以直接使用randint命令,如randint(10,20)
能产生从10到20之间的随机整数。
"""
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、实战小程序(多彩散射条)
编制程序让海龟随机选择一个方向,随机选择一种颜色,
然后用fd命令随机移动一定的距离再倒回来, 如此重复100次。
"""
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、练习与作业
★ 编制程序产生10个随机数,打印它们的平均数。
★ 编制程序产生随机的两个数字,打印它们的乘积,把此过程重复10次。
★ 获取海龟的方向用heading命令。假设有名为turtle的海龟对象,
那么如turtle.heading()就能获取当前海龟的方向值。
编制程序,给屏幕设置一张背景图,并且要用fd命令画正方形。
当海龟移动一次就打印出它的坐标和方向。
"""
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资源。)

