"""
控制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人支付
立即購買