Python/Pygame-创建;结束学分“;就像电影结尾的那些

Python/Pygame-创建;结束学分“;就像电影结尾的那些,python,pygame,Python,Pygame,我正在尝试使用pygame创建电影结尾的“结尾字幕”。我在谷歌上搜索过使用python实现这一点的其他方法,但我还没有找到任何方法 我几乎通过以下代码实现了这一点: 问题是滚动文本在闪烁。这是因为我使用time.sleep()函数来控制滚动的速度。当我使用0.04秒这样的值时,效果很好,但是文本移动太慢,仍然有一些闪烁。当我使用一个低得多的值时,比如:0.001秒,文本以我喜欢的速度移动,但是会有更多的闪烁 我可以用另一个值来调整滚动的速度:移动的像素数。但是当我将其设置为高于1的任何值时,滚动

我正在尝试使用pygame创建电影结尾的“结尾字幕”。我在谷歌上搜索过使用python实现这一点的其他方法,但我还没有找到任何方法

我几乎通过以下代码实现了这一点:

问题是滚动文本在闪烁。这是因为我使用time.sleep()函数来控制滚动的速度。当我使用0.04秒这样的值时,效果很好,但是文本移动太慢,仍然有一些闪烁。当我使用一个低得多的值时,比如:0.001秒,文本以我喜欢的速度移动,但是会有更多的闪烁

我可以用另一个值来调整滚动的速度:移动的像素数。但是当我将其设置为高于1的任何值时,滚动不再平滑

有人知道这个问题的解决办法吗?我不必使用pygame,但我必须使用python

非常感谢


Albrecht

以下是一些您应该遵循的简单规则,它们将帮助您解决问题:

  • 不要每帧多次调用
    pygame.display.flip()
  • 不要使用
    time.sleep()
    来控制应用程序中某些内容的速度
  • 使用
    时钟
    控制帧速率
下面是一个经过清理的最小工作示例:

#!/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()

你应该决定一个合理的帧速率,然后通过改变每帧的像素数来改变速度。非常感谢!这就是我试图实现的目标!我接受这个答案!
#!/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()