李興球Python是男人就下一百層測試程式tkinter遊戲

scratch發表於2020-10-30

"""
   是男人就下一百層測試程式(生成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)
本作品採用《CC 協議》,轉載必須註明作者和本文連結
python精靈模組

相關文章