是男人就下一百层测试程序(生成gif合成gif)

是男人就下一百层测试程序(生成gif合成gif)

李兴球Python是男人就下一百层测试程序

"""
   是男人就下一百层测试程序(生成gif合成gif)
   本程序需要gameturtle0.2版支持,
   操作方法:上左右键操作竖直的矩形不断下自动层递即可,
   本程序会自动截屏,所以运行速度较慢。
   最后会生成gif图片。
"""
from gameturtle import *
from random import randint

def gotobottom(r):
     x,y = randint(0,480),360
     r.goto(x,y)

class Key:
    def __init__(self,cv,key):
        self._canvas = cv
        self._key = key
        self._down = False
        self._canvas.bind("" % key,self._press)        
        self._canvas.bind("" % key,self._release)
        
    def _press(self,event):
        self._down = True

    def _release(self,event):
        self._down = False

    def isdown(self):
      return self._down
    
root = Tk()
root.title('是男人就下一百层测试程序')

cv = Canvas(width=480,height=360,bg='cyan')
cv.pack()

square = Image.new("RGBA",(100,10),color='green')

rects = []
for _ in range(6):
    x,y = randint(0,480),randint(180,660)
    rects.append( GameTurtle(cv,square,pos=(x,y),tag='ground'))
rects[0].home()

manpic1 = Image.new("RGBA",(10,50),color='blue')
manpic2 = Image.new("RGBA",(15,90),color='blue')
man = GameTurtle(cv,(manpic1,manpic2))
man.center()
man.addy(-100)

up_key = Key(cv,"Up")
right_key = Key(cv,"Right")
left_key = Key(cv,"Left")
cv.focus_force()

dy = 0
frames = []
for _ in range(600):
    [r.addy(-1) for r in rects]
    [gotobottom(r) for r in rects if r.ycor() < 0]     
    man.addy(dy)
    if man.bottom_collide((0,255,0)):
        dy = 0
        man.addy(-1)
        if up_key.isdown():dy = -6
    else:
        dy = dy + 0.1
    if right_key.isdown():man.addx(2)
    if left_key.isdown():man.addx(-2)
    cv.update()
    if c % 10 == 0 :    frames.append(cv.grab())
    time.sleep(0.01)
frames[0].save('demo.gif', save_all=True,quality=70,
               append_images=frames[1:], duration=0.1)

下面是程序截屏后自动生成的动图demo.gif,

李兴球Python tkinter截gif动态图片

李兴球

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