控制fps的时钟Clock类源码

控制fps的时钟Clock类源码

"""
   控制fps的时钟Clock类,
   本程序用来在循环中控制fps。
   如何在海龟画图中控制fps?这是一个比较重要的问题,否则程序可能有时候快有时候慢。
"""
import time
import colorsys
from turtle import *
from random import * 

class Clock:

    def __init__(self):
       self._old_start_time = time.perf_counter()
       self._start_time = time.perf_counter()

    def tick(self,fps=0):
        
        end_time = time.perf_counter()
        pass
    
    def getfps(self):
        """得到fps"""
        t = time.perf_counter() - self._old_start_time
        return round(1/t,2)


# 类定义好了,以下是测试Clock类的代码:

def makecolors(n=128):
        
    """产生颜色表,这种颜色表中的颜色更鲜艳"""
    cs = []
    pass
    return cs

colorlist = makecolors()
screen = Screen()
screen.tracer(0,0)
screen.setup(480,360)
screen.title("控制fps的时钟类")

fps = 10                # 设定fps
index = 0               # 颜色表索引
clock = Clock()         # 建立时钟对象

while 1:    
    screen.bgcolor(colorlist[index])
    index = index + 1
    index = index % len(colorlist)
    t = clock.tick(fps)
    screen.title("fps=" + str(clock.getfps())+ ":逝去的时间:" + str(t))

如需要查看完整源代码,请

成为会员后,登陆才能继续浏览!联系微信scratch8即可办理会员。
(会员专属:能浏览所有文章,下载所有带链接的Python资源。)

李兴球

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

评论已关闭。