这是风火轮编程Python初级教程的第19课,下面是用python制作的学习大纲。
用Python制作的幻灯片类型的作品。本课主要讲解一个经典的小游戏,叫猜测数小游戏。
以下是完整源代码。(并非猜数小游戏的代码,是用于演示教学的程序)
本程序需要sprites模块支持,安装方法为在命令提示符下输入以下命令安装:
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple sprites –upgrade
"""
第十九课 猜数游戏
"""
from sprites import *
s = '第十九课 猜数游戏'
screen = Screen()
screen.bgcolor('green')
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()
# 下面的TK是就是tkinter
popup = TK.Menu(screen._root, tearoff=0)
popup.add_command(label="关于本程序",command=lambda:showinfo('关于','本程序由李兴球开发\n\nQQ:406273900\n\nwww.lixingqiu.com'))
popup.add_command(label="打开主页",command=lambda:os.system('explorer https://www.lixingqiu.com'))
popup.add_command(label="退出本程序",command=lambda:screen.bye())
def do_popup(event):
"""显示弹出菜单"""
try:
popup.tk_popup(event.x_root, event.y_root + 10, 0)
finally:
# 确保释放按键
popup.grab_release()
screen.cv.bind("", do_popup) # 画布绑定鼠标右键
# 单击鼠标左键开始
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,300))
t.color('yellow')
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 ="""
猜数小游戏是一个非常经典的小游戏。
它的运行过程是计算机出一个数字。
然后是让用户从键盘输入一个数来猜计算机出了什么数字。
如果用户输入的数字比计算机出的数小,那么计算机就会提示小了。
如果用户输入的数字比计算机出的数大,那么计算机就会提示大了。
如果用户输入的数字和计算机出的数一样大,说明猜对了。
这时候就退出这个程序。
由于这个程序不知道用户会猜多少次,所以一般采用while循环
来不断地让用户输入数据。当用户输入了数据,则进行相应的判断...
以下是练习与作业
1、把猜数游戏用for循环重新编制。
2、用while循环打印星号,第一行打印一个星号,第二行打印两个星号,
第三行打印三个星号,一直到第十行打印十个星号。
"""
if brief!='': # 如果有课程简介,则生成一个角色,
ftx = ('宋体',18,'normal')
简介 = Sprite(visible=False,pos=(0,-300))
简介.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()
byebye = """
下次再见!
"""
t.clear()
t.color('cyan')
t.home()
t.write(byebye,align='center',font=('宋体',38,'bold'))
screen.mainloop()
以下是付费内容,包括播放视频、python源代码及幻灯片与素材等等。
需要浏览更多吗?
成为会员后,登陆才能继续浏览!联系微信scratch8即可办理会员。
(会员专属:能浏览所有文章,下载所有带链接的Python资源。)

