欢迎访问 生活随笔!

生活随笔

当前位置: 首页 >

python2clock_控制fps的时钟Clock类源码

发布时间:2023/12/1 35 豆豆
生活随笔 收集整理的这篇文章主要介绍了 python2clock_控制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))

如需要查看完整源代码,请扫码付款:

VIP免费查看

当前隐藏内容需要支付

60元

已有0人支付

立即购买

总结

以上是生活随笔为你收集整理的python2clock_控制fps的时钟Clock类源码的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。