- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试使用 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/
C 中的字符串或字符数组必须以空字符结尾才能知道它们在哪里结束。为什么相同的规则不适用于其他类型的数组? 例如。计算机如何知道整数数组在哪里结束?整数/浮点数/ double 数组在内存中的表示方式有
我有一堆文件需要查看。这些文件中的所有行都以 ^M (\x0D) 结尾,但最后一行以 ^M^J (\x0D\x0A) 结尾。 显然,Vim 将文件类型确定为 DOS,效果是文件的整个内容是 显示在一行
以下是来自 Why's Poignant Guide to Ruby Chapter 6 的 Ruby 代码片段,他试图在 Ruby 中演示元编程: # Get a metaclass for thi
我有一个编程问题,希望有人能帮我解决。我正在尝试为一项工作任务学习 C 编程,我为自己设定了一个小项目,其中包括读取文件树,其中包括获取有关每个文件的信息的所有子目录。 我遇到的问题是我的程序不会忽略
其他章节请看: react 高效高质量搭建后台系统 系列 尾篇 本篇主要介绍 表单查询 、 表单验证 、 通知 (WebSocket)、 自动构建
我是 FFmepg 的新手,所以如果术语有误,请原谅我。 我想将文本和音频添加到现有的视频文件中。我链接在一起 2 drawText s,然后在不同的过滤器中,我想合并原始源视频的音轨和一些背景音乐。
使用grep我可以使用以下命令搜索单词的开头和结尾 grep -e '\' 这将找到I have a leg.,但找不到play allegro here。 Ripgrep(0.10.0)似乎不支持
$.ajax({ type:'GET', cache: 'false', url:"/foo/", data:{"id": pid}, success:func
我想在我的 iPhone 应用程序中处理旋转手势并在此期间旋转 imageView。在手势结束时,我想将 imageView 旋转到固定位置。所以,即。如果我将 imageView 从 0 弧度旋转到
我知道检测是一种将跟踪代码动态添加到方法中以启用跟踪和调试的技术。 我想知道这是否只是一个“跟踪”选项,硬编码到 CLR 中以仅添加跟踪代码,或者是否能够向方法添加任何代码? 例如,我想检查某个类中每
我正在查看一些专有源代码:使用库的示例程序。 代码是用 C 和 C++ 编写的,使用 make 构建系统。 每个文件都以注释掉的 [] 结尾:/*[]*/ 源文件和 #[]#对于生成文件。这可能是什么
是否可以循环文件中的每一行并检查它是如何结束的(LF/CRLF): using(StreamReader sr = new StreamReader("TestFile.txt")) { st
我从 JSF 网站的编程开始。目前所有文件都以 .xhtml 结尾。当我去http://localhost:8080/myProject/start.jsf一切正常。但是当我将文件从 start.xh
这是一个多站点问题。我有很多网站都带有.htaccess文件,其中包含多行,类似于: rewriterule ^(page-one|page-two|page-three)/?$ /index.php
真的没有简单的映射来跳转到段落的第一行或最后一行吗? { 和 } 是“独占”命令,因此它们会跳转到段落前后的空行如果有的话,但对于段落的实际第一行或最后一行(即,当段落位于缓冲区的顶部或底部时)。 这
我已经尝试了该站点上所有可能的解决方案。我以前使用过改造,过去已经解决了这个问题,但这次我无法解决。我正在使用 StackOverflow API 来获取问题及其详细信息。 API : https:/
我正在做模式匹配如下。但我没有得到正确的输出。请建议正确的代码以获得正确的输出。 代码 #! /usr/bin/perl -w my $subString = "1.3.6.1.2.1.26.2.1.
如何创建 if 语句以检查整数是否以 0 结尾? 例如,我想要一个这样的 if 语句: var test = 107; //107 is an example it'should some unkno
这个问题已经有答案了: javascript how to tell if one number is a multiple of another (6 个回答) 已关闭 3 年前。 每当秒 var
我正在遍历文件树以识别所有 .DYLIB 文件。 #!/bin/bash #script to recursively travel a dir of n levels function traver
我是一名优秀的程序员,十分优秀!