gpt4 book ai didi

Python/Pygame - 创建 "End credits"就像电影结尾处的那样

转载 作者:太空宇宙 更新时间:2023-11-03 16:47:32 24 4
gpt4 key购买 nike

我正在尝试使用 pygame 创建“片尾字幕”,就像电影结尾处的片尾字幕一样。我已经在 google 上搜索过使用 python 实现此目的的其他方法,但还没有找到。

我几乎通过以下代码实现了这一点: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。

提前非常感谢!

阿尔布雷希特

最佳答案

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

  • 每帧调用 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()

关于Python/Pygame - 创建 "End credits"就像电影结尾处的那样,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36164524/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com