如何在python中同时显示图像和文本(如SanctuaryRPG)?

2024-09-29 21:21:21 发布

您现在位置:Python中文网/ 问答频道 /正文

所以,我对python还很陌生,但我目前正在编写一个文本冒险游戏。我想知道如何在屏幕底部显示文本,而在顶部显示ASCII图像,就像SanctuaryRPG


Tags: 图像文本屏幕ascii冒险游戏陌生sanctuaryrpg
2条回答

好的,那么基本上,您想要打印一个带有文本的ASCII图像。这应该不会太难

首先,将ASCII图像设置为变量。例如:

img = '''
                 __,__
        . .  .-"     "-.  . .
       / .. \/  .-. .-.  \/ .. \
      | |  '|  /   Y   \  |'  | |
      | \   \  \ 0 | 0 /  /   / |
       \ '- ,\.-"`` ``"-./, -' /
        `'-' /_   ^ ^   _\ '-'`
        . '|  \._   _./  |' . 
      /`    \   \ `~` /   /    `\
     /       '._ ' -' _.'       \
    /           '~ -~'   |       \
   /        _.             \       \
  /   .'-./`/        .'~'-.|\       \
 /   /    `\:       /      `\'.      \
/   |       ;      |         '.`;    /
\   \       ;      \           \/   /
 '.  \      ;       \       \   `  /
   '._'.     \       '.      |   ;/_
    /__>     '.       \_ _ _/   ,  ' .
   .'   '.   .-~~~~~-. /     | '`~~-.  \
  // / . -'/  .-~~-._/ / / / -..__.'  /
 ((_(_/    /  /      (_(_(_( -.__    .'
           | |     _              `~~`
           | |     \'.
            \ '....' |
             '.,___.'
'''

接下来,将要打印的文本设置为另一个变量

text = "Do you want to say hi to the monkey?"

最后,按如下方式打印两个:

print(img)
print(text)

或:

print(img + "\n\n" + text)

\n只是指一个新行

要执行与使用Pygame的SanctuaryRPG相同的操作,您需要使用每个字符宽度相同的字体(例如Courier):

font = pygame.font.SysFont("Courier", text_height)

splitlines()将文本拆分为几行(请参见How to split a python string on new line characters):

img_text = img.splitlines()

在循环中逐行渲染文本:

for i, line in enumerate(img_text):
    text_surf = font.render(line, True, (255, 255, 0))
    window.blit(text_surf, (50, 20 + i * text_height))

使用以下功能:

def renderTextImage(surf, font, text, x, y, color):
    img_text = text.splitlines()
    for i, line in enumerate(img_text):
        text_surf = font.render(line, True, color)
        surf.blit(text_surf, (x, y + i * text_height))

最简单的例子:

repl.it/@Rabbid76/PyGame-AsciiTextImage

import pygame

img_text = r'''
                 __,__
        . .  .-"     "-.  . .
       / .. \/  .-. .-.  \/ .. \
      | |  '|  /   Y   \  |'  | |
      | \   \  \ 0 | 0 /  /   / |
       \ '- ,\.-"`` ``"-./, -' /
        `'-' /_   ^ ^   _\ '-'`
        . '|  \._   _./  |' . 
      /`    \   \ `~` /   /    `\
     /       '._ ' -' _.'       \
    /           '~ -~'   |       \
   /        _.             \       \
  /   .'-./`/        .'~'-.|\       \
 /   /    `\:       /      `\'.      \
/   |       ;      |         '.`;    /
\   \       ;      \           \/   /
 '.  \      ;       \       \   `  /
   '._'.     \       '.      |   ;/_
    /__>     '.       \_ _ _/   ,  ' .
   .'   '.   .-~~~~~-. /     | '`~~-.  \
  // / . -'/  .-~~-._/ / / / -..__.'  /
 ((_(_/    /  /      (_(_(_( -.__    .'
           | |     _              `~~`
           | |     \'.
            \ '....' |
             '.,___.'
'''

def renderTextImage(surf, font, text, x, y, color):
    img_text = text.splitlines()
    for i, line in enumerate(img_text):
        text_surf = font.render(line, True, color)
        surf.blit(text_surf, (x, y + i * text_height))

pygame.init()
window = pygame.display.set_mode((500, 500))
clock = pygame.time.Clock()

text_height = 16 
font = pygame.font.SysFont("Courier", text_height)

run = True
while run:
    clock.tick(60)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False          

    window.fill(0)
    renderTextImage(window, font, img_text, 50, 20, (255, 255, 0))
    pygame.display.flip()

pygame.quit()
exit()

相关问题 更多 >

    热门问题