2016-03-22 51 views
1

我試圖使用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。

非常感謝提前!

阿爾佈雷希

+0

你應該確定一個合理的幀速率,然後通過改變像素數更多的每幀改變速度。 – Jezzamon

回答

1

下面是你應該遵循這將幫助你解決問題的一些simpe規則:

  • 別叫超過每幀
  • 一次pygame.display.flip()更不要使用time.sleep()來控制應用中某物的速度
  • 使用Clock來控制幀率

這裏有一個清理,最小工作示例:

#!/usr/bin/python 
import pygame 
from pygame.locals import * 

pygame.init() 
pygame.display.set_caption('End credits') 
screen = pygame.display.set_mode((800, 600)) 
screen_r = screen.get_rect() 
font = pygame.font.SysFont("Arial", 40) 
clock = pygame.time.Clock() 

def main(): 

    credit_list = ["CREDITS - The Departed"," ","Leonardo DiCaprio - Billy","Matt Damon - Colin Sullivan", "Jack Nicholson - Frank Costello", "Mark Wahlberg - Dignam", "Martin Sheen - Queenan"] 

    texts = [] 
    # we render the text once, since it's easier to work with surfaces 
    # also, font rendering is a performance killer 
    for i, line in enumerate(credit_list): 
     s = font.render(line, 1, (10, 10, 10)) 
     # we also create a Rect for each Surface. 
     # whenever you use rects with surfaces, it may be a good idea to use sprites instead 
     # we give each rect the correct starting position 
     r = s.get_rect(centerx=screen_r.centerx, y=screen_r.bottom + i * 45) 
     texts.append((r, s)) 

    while True: 
     for e in pygame.event.get(): 
      if e.type == QUIT or e.type == KEYDOWN and e.key == pygame.K_ESCAPE: 
       return 

     screen.fill((255, 255, 255)) 

     for r, s in texts: 
      # now we just move each rect by one pixel each frame 
      r.move_ip(0, -1) 
      # and drawing is as simple as this 
      screen.blit(s, r) 

     # if all rects have left the screen, we exit 
     if not screen_r.collidelistall([r for (r, _) in texts]): 
      return 

     # only call this once so the screen does not flicker 
     pygame.display.flip() 

     # cap framerate at 60 FPS 
     clock.tick(60) 

if __name__ == '__main__': 
    main() 
+0

非常感謝!這是我試圖完成的!我接受這個答案! – Albrecht