[Python]pygame勉強中 60FPS以下に抑える
- 2005/09/19
- 03:09
使った関数 time.clock()
time.clock()で起動してからの時間を取得できます
精度はwindowsでμ秒、unixで1/100秒
import timeで使えるようになります
処理 60FPSにするには
60FPSの場合は1フレーム約0.016秒なので1フレームが
それ以上の長さになるようにします
前回計った時との0.016秒以下なら入力と描画をしない、超えたらします。
import pygame
import time
pygame.init()
screen = pygame.display.set_mode([640, 480])
image = pygame.Surface([32, 32])
image.fill([255,255,255])
black = pygame.Surface([32, 32])
black.fill([0,0,0])
cx=0
cy=0
BeforeTime=time.clock()
while pygame.event.poll().type != pygame.QUIT:
NowTime=time.clock()
if NowTime-BeforeTime < 0.0166667:
continue
BeforeTime=NowTime
uprect=[]
uprect.append([cx,cy,32,32])
screen.blit(black, [cx,cy])
keystate = pygame.key.get_pressed()
if keystate[pygame.K_LEFT] and cx>0:
cx -= 1
if keystate[pygame.K_RIGHT] and cx<607:
cx += 1
if keystate[pygame.K_UP] and cy>0:
cy -= 1
if keystate[pygame.K_DOWN] and cy<447:
cy += 1
uprect.append([cx,cy,32,32])
screen.blit(image, [cx,cy])
pygame.display.update(uprect)
pygame.quit()
time.clock()で起動してからの時間を取得できます
精度はwindowsでμ秒、unixで1/100秒
import timeで使えるようになります
処理 60FPSにするには
60FPSの場合は1フレーム約0.016秒なので1フレームが
それ以上の長さになるようにします
前回計った時との0.016秒以下なら入力と描画をしない、超えたらします。
import pygame
import time
pygame.init()
screen = pygame.display.set_mode([640, 480])
image = pygame.Surface([32, 32])
image.fill([255,255,255])
black = pygame.Surface([32, 32])
black.fill([0,0,0])
cx=0
cy=0
BeforeTime=time.clock()
while pygame.event.poll().type != pygame.QUIT:
NowTime=time.clock()
if NowTime-BeforeTime < 0.0166667:
continue
BeforeTime=NowTime
uprect=[]
uprect.append([cx,cy,32,32])
screen.blit(black, [cx,cy])
keystate = pygame.key.get_pressed()
if keystate[pygame.K_LEFT] and cx>0:
cx -= 1
if keystate[pygame.K_RIGHT] and cx<607:
cx += 1
if keystate[pygame.K_UP] and cy>0:
cy -= 1
if keystate[pygame.K_DOWN] and cy<447:
cy += 1
uprect.append([cx,cy,32,32])
screen.blit(image, [cx,cy])
pygame.display.update(uprect)
pygame.quit()
- テーマ:プログラミング
- ジャンル:コンピュータ
- カテゴリ:オレ的学習
- CM:0
- TB:0