0
点赞
收藏
分享

微信扫一扫

python:游戏倒计时器

目标践行者 2022-04-06 阅读 51
python
#仅供参考

#绑定界面
cdtimer = CountDown(root)
#设置倒计时长
cdtimer.start_timer(120)

class CountDown(object):
    def __init__(self, master=None):
        self.root = master
        self.total_sec = 99999
        self.root.resizable(width=False, height=False)
        self.updatetime()

    def updatetime(self):
        self.labelA = Label(self.root, text='')
        self.labelA.place(x=0, y=0)#设置位置,现在表示在root窗口x=0,y=0位置
        self.labelA.pack()
        
    def updateA(self):
        self.labelA.place(x=0, y=0)#设置位置
        self.total_sec -= 1
        time_str = ""
        if self.total_sec > 0:
            time_str = '倒计时:' + str(self.total_sec)
            self.root.after(1000, self.updateA)
        else:
            pygame.mixer.Sound.play(finishSound)
            messagebox.showinfo(title='提示',message='时间到,游戏结束')

        self.labelA.configure(text=time_str)
    
    def start_timer(self,total_sec):
        self.total_sec = total_sec
        self.updateA()
举报

相关推荐

0 条评论