我試圖使用pygame在電影結尾處創建「結尾信用」。我已經用其他方法來實現這個使用python,但我還沒有找到任何。Python/Pygame - 在電影結尾處創建「End credits」
我幾乎用下面的代碼來實現這一點:http://pastebin.com/nyjxeDYQ
#!/usr/bin/python
import time
import threading
import pygame
from pygame.locals import *
# Initialise pygame + other settings
pygame.init()
pygame.fastevent.init()
event_get = pygame.fastevent.get
pygame.display.set_caption('End credits')
screen = pygame.display.set_mode((1920, 1080))
background = pygame.Surface(screen.get_size())
background = background.convert()
background.fill((255, 255, 255))
fontsize = 40
font = pygame.font.SysFont("Arial", fontsize)
x = 0
def main():
global x
credit_list = ["CREDITS - The Departed"," ","Leonardo DiCaprio - Billy","Matt Damon - Colin Sullivan", "Jack Nicholson - Frank Costello", "Mark Wahlberg - Dignam", "Martin Sheen - Queenan"]
going = True
while going:
events = event_get()
for e in events:
if e.type in [QUIT]:
going = False
if e.type in [KEYDOWN] and e.key == pygame.K_ESCAPE:
going = False
# Loop that creates the end credits
ypos = screen.get_height()
while ypos > (0 - len(credit_list)*50) and x == 0: # Loop through pixel by pixel, screenheight + height of all the textlines combined
drawText(credit_list,ypos)
ypos = ypos - 1
x = 1
pygame.quit()
def drawText(text,y):
for line in text:
text = font.render(line, 1, (10, 10, 10))
textpos = text.get_rect()
textpos.centerx = background.get_rect().centerx
background.blit(text, (textpos.x,y))
y = y + 45
# Blit all the text
screen.blit(background, (0, 0))
pygame.display.flip()
time.sleep(0.0001) # Sleep function to adjust speed of the end credits
# Blit white background (else all the text will stay visible)
background.fill((255, 255, 255))
screen.blit(background, (0, 0))
pygame.display.flip()
if __name__ == '__main__': main()
的問題是,滾動文本閃爍。這是因爲我使用了time.sleep()函數來控制滾動的速度。當我使用0.04秒的數值時,效果很好,但文字移動速度太慢,仍然有點閃爍。當我使用更低的值時,如:0.001秒,文本以我喜歡的速度移動,但閃爍的頻率更高。
我可以使用另一個值來調整滾動的速度:要移動的像素數。但是當我把它設置爲高於1的值時,滾動不再平滑。
有沒有人知道這個問題的解決方案?我不一定非要使用pygame,但我必須使用python。
非常感謝提前!
阿爾佈雷希
你應該確定一個合理的幀速率,然後通過改變像素數更多的每幀改變速度。 – Jezzamon