贪吃蛇原理动画演示教学档.py

贪吃蛇原理动画演示教学档.py

李兴球按空格键感受贪吃蛇最基本原理动画
为了让同事们能理解贪吃蛇这个游戏的精髓,特编此小程序,以下是所有代码。
Python精灵模块安装方法见最下面。

"""
  贪吃蛇原理动画演示教学档.py
  这个程序是用于萍乡乐学创客教育编程学校师资培训的一个交互演示程序。
"""
import time
from sprites import Sprite,Key

def press_space_continue():
    """按空格键继续"""
    while not space.downup():screen.update()
    
turtle  = Sprite(shape='square')
screen = turtle.getscreen()
screen.setup(800,400)
screen.listen()

space = Key('space')       # 新建空格键
turtle.speed(1)
screen.delay(20)
turtle.bk(360)             # 海龟倒退360个单位
turtle.speed(0)
screen.delay(0) 

for _ in range(10):
    turtle.wait(0.5)
    turtle.stamp()
    turtle.wait(0.5)
    turtle.fd(25)

ft = ('',22,'normal')
w = Sprite(visible=False)
w.sety(100)
w.write('请按空格键',align='center',font=ft)
while True:
    press_space_continue() # 按空格键继续 
    turtle.clearstamps(1)  # 清除最早那个
    w.clear()
    w.write('清除了最早盖的那个图章',align='center',font=ft)
    turtle.update()        # 更新显示
    
    press_space_continue()
    turtle.stamp()         # 在海龟当前位置再盖章,此时海龟和图章重叠
    w.clear()
    w.write('在海龟当前位置再盖章,此时海龟和图章重叠',align='center',font=ft)

    press_space_continue()
    turtle.fd(25)          # 海龟前进25个单位,(移开了)
    w.clear()
    w.write('海龟前进25个单位,海龟移开了',align='center',font=ft)

本程序需要Python精灵模块支持,如果没有安装是无法运行的,安装方法有如下两个:

第1种方法: 用cmd命令打开管理员窗口,输入 pip install sprites –upgrade

第2种方法: 从链接 https://www.lixingqiu.com/pms.rar 下载压缩包,解压后得到 “安装精灵模块等常见模块.bat”批处理文件,
双击这个批处理文件即可安装,在pip命令正确安装及联网的情况下能安装一些常见的模块。

李兴球

李兴球的博客是Python创意编程原创博客