我正在尝试制作一种Pokemon样式的文本显示,在该文本显示中,当您单击按键时,一行一行显示下一行文本。但是,我的代码似乎并没有等待我在退出序列之前按下键,并且序列的整个退出功能都没有运行。有没有办法做到这一点?
在访问列表中的文本时,还有其他方法吗?编辑:抱歉,我在程序外重新创建了代码以显示问题。抱歉,如果第一个代码有点混乱:/
import pygame
pygame.init()
screen = pygame.display.set_mode((640, 480))
text_bg = pygame.Surface((640, 180))
game_messages = pygame.font.Font("fonts/Connection.otf", 30)
text1 = game_messages.render("Text number 1", False, (255,0,0))
text2 = game_messages.render("text number 2", False, (255,0,0))
text_list = [text1, text2]
running = 1
playing_text = 2
quits = 0
current_state = 1
def draw_text(text):
screen.blit(text, (40, 360))
while current_state != quits:
if current_state == running:
screen.fill((255,255,255))
pygame.display.flip()
print("update")
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
current_state = playing_text
screen.blit(text_bg, (0,300))
for text in text_list: # iterates over the list with all the messages
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
quit()
elif event.key == pygame.K_SPACE:
draw_text(text)
pygame.time.delay(50)
screen.blit(text_bg, (0, 300))
pygame.display.flip()
for t in range(len(text_list)):
screen.blit(text_bg, (0, 300))
draw_text(text)
pygame.display.flip()
pygame.event.clear(pygame.KEYDOWN)
if event.type == pygame.KEYDOWN:
print("exit")
if event.key == pygame.K_SPACE:
current_state = 1
pygame.display.flip()
这是一个有趣的问题,这种事情有很多复杂性-字体大小,自动换行,易读性等如何?
我考虑了一会儿,我想不出一种nice的方法来将其合并到主循环中,而没有到处放置各种状态处理代码。因此,尽管我反对多个事件循环,但这里有一个示例,以类似的方式实现了您建议的内容。 playMessage()
函数具有其自己的事件循环,不仅可以处理消息中的所有内容,而且还可以将重要事件(如Quit)发送回主循环以进行处理。
无论如何,以这种方式处理文本的关键是计算按键的次数,在这种情况下是space,并且每次按键仅显示一个新行。 (也在按最后一行之后退出。)
[每行文本,当通过pygame.font.render()
渲染为图像时,将具有其自己的高度,随着绘图沿着屏幕向下移动,需要对其进行汇总。因此,该代码实际上具有两个“游标”:文本行列表中的位置和显示器上的Y像素位置。
由于代码阻止了主窗口事件循环,因此它将获取原始屏幕内容的副本,并使用它来重新绘制背景。此背景变暗并变平滑以使文本更清晰(尽管我选择了字体)。
import pygame
# Window size
WINDOW_WIDTH = 700
WINDOW_HEIGHT = 300
WINDOW_SURFACE = pygame.HWSURFACE|pygame.DOUBLEBUF|pygame.RESIZABLE
DARK_BLUE = ( 3, 5, 54 )
WHITE_ISH = ( 230, 230, 197 )
def playMessage( window, font, message, start_point=( 20, 20 ) ):
line_separation = 3 # pixels between lines
line_cursor = 0
# Make a blurred copy of the background for updating, by shrinking then
# expanding the current content of the window, oh and darken it too
# for better constrast
skrinked = pygame.transform.smoothscale( window, ( window.get_width()//4, window.get_height()//4 ) )
dark = pygame.Surface( ( skrinked.get_width(), skrinked.get_height() ), flags=pygame.SRCALPHA )
dark.fill( ( 100, 100, 100, 0 ) )
skrinked.blit( dark, (0,0), special_flags=pygame.BLEND_RGBA_SUB )
background = pygame.transform.smoothscale( skrinked, ( window.get_width(), window.get_height() ) )
# cleanup messages, remove blank lines, et.al
message_lines = []
for line in message.split( '\n' ):
line = line.strip()
if ( len( line ) > 0 ):
message_lines.append( line )
# Make every text line into a bitmap
for i,line in enumerate( message_lines ):
message_lines[i] = font.render( line, True, WHITE_ISH )
# Start the render
clock = pygame.time.Clock()
done = False
while not done:
window.blit( background, ( 0,0 ) )
x_pos, y_pos = start_point
for i in range( 0, line_cursor ):
text_rect = message_lines[i].get_rect()
text_rect.x = x_pos
text_rect.y = y_pos
window.blit( message_lines[i], text_rect )
# offset next line
y_pos += text_rect.height + line_separation
pygame.display.flip()
# Handle user-input
for event in pygame.event.get():
if ( event.type == pygame.QUIT ):
pygame.event.post( pygame.event.Event( pygame.QUIT ) ) # re-post this to handle in the main loop
done = True
elif ( event.type == pygame.KEYDOWN ):
if ( event.key == pygame.K_ESCAPE ):
done = True
elif ( event.key == pygame.K_SPACE ):
line_cursor += 1
if ( line_cursor > len( message_lines ) ):
done = True # space at end to dismiss
clock.tick_busy_loop( 16 ) # don't need big FPS for read
### initialisation
pygame.init()
pygame.mixer.init()
window = pygame.display.set_mode( ( WINDOW_WIDTH, WINDOW_HEIGHT ), WINDOW_SURFACE )
pygame.display.set_caption("text player")
### Message Text For Displaying
poke_font = pygame.font.Font( 'Pokemon Solid.ttf', 24 ) # ref: https://fontmeme.com/fonts/pokmon-font/
message = "You were eated all up by a Wild Wampus!\nAnd you never found the Key in the Dark Forest!\nRedo From Start."
### Background image
grassy_background = pygame.image.load( "grassy.jpg" ) # ref: http://www.plaintextures.com/
grassy_background = pygame.transform.smoothscale( grassy_background, ( WINDOW_WIDTH, WINDOW_HEIGHT ) )
### Main Loop
clock = pygame.time.Clock()
done = False
while not done:
# Handle user-input
for event in pygame.event.get():
if ( event.type == pygame.QUIT ):
done = True
elif ( event.type == pygame.MOUSEBUTTONUP ):
# On mouse-click
playMessage( window, poke_font, message )
# Movement keys
#keys = pygame.key.get_pressed()
#if ( keys[pygame.K_UP] ):
# print("up")
# Update the window, but not more than 60fps
window.blit( grassy_background, ( 0, 0 ) )
pygame.display.flip()
# Clamp FPS
clock.tick_busy_loop(60)
pygame.quit()
如果要在按Enter键后逐行,只需添加