簡體   English   中英

如何在pygame中設置計時器而不延遲程序

[英]How do I make a timer in pygame without delaying the program

我正在制作一個pygame游戲,需要變量“ img”在多張幻燈片中循環以制作步行動畫,但是我可以找到的所有用python / pygame制成的計時器都會延遲我不需要的程序。

您可以使用Python的時間模塊測量時間。 您可以執行以下操作:

import time

t = time.clock()
deltaT = 1 #time between updates, in seconds
while True: #inside the game loop...
    if (time.clock() - t) > deltaT:
        #change your image
        t = time.clock()

盡管如果要對精靈進行動畫處理,那么為pygame尋找動畫模塊可能會更好。

您可以執行以下操作:

import time, pygame
from pygame.locals import *
images = ['img1.jpg', 'img2.jpg', 'img3.jpg', 'img4.jpg']
pygame.init()
screen = pygame.display.set_mode((1024, 1280))
i = 0

x = time.time()

while True:
    screen.blit(images[i]) #Create the image, this syntax is wrong
    screen.fill((255, 255, 255))
    if i != 3:
        i+=1
    elif i == 4:
        i = 0

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM