我正在尝试制作口袋妖怪风格的文本显示,当您单击某个键时,下一行文本会逐一显示。但是,我的代码似乎没有等待我在退出序列之前按下该键,并且序列的整个退出函数不会运行。有办法实现这一点吗?
在访问列表中的文本时是否还有一种方法可以做到这一点? 编辑:抱歉,我在程序之外重新创建了代码来显示问题。抱歉,如果第一个代码有点困惑:/
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()
最佳答案
这是一个有趣的问题,这类事情非常复杂 - 字体大小、自动换行、易读性等等如何?
我想了一会儿,但我无法想出一种好的方法将其合并到主循环中,而无需到处都有各种状态处理代码。因此,尽管我反对多个事件循环,但这里有一个以类似方式实现您建议的示例。 playMessage() 函数有自己的事件循环,可以处理足够多的消息,但也可以将重要事件(如退出)发送回主循环进行处理。
无论如何,以这种方式处理文本的关键点是计算按键次数,在本例中为 空格,并且每次按键时仅显示新行。 (按下最后一行后也退出。)
当通过 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()
关于python - 类似口袋妖怪的文字显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60536457/