首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >游戏文本没有使用blit或sysfont显示

游戏文本没有使用blit或sysfont显示
EN

Stack Overflow用户
提问于 2022-09-06 12:30:11
回答 1查看 38关注 0票数 0

我一直试图将文本放在一个sprite (我导入的对话框)上,但是没有出现任何文本。当前的代码是我试图将文本混合到屏幕上,但它不起作用。代码工作在一个单独的文档上,而不是在这个文档上。我现在有它创建的颜色,然后一个框为文本显示。在此之后,盒子被制作成适合它的环境,一个字体是通过SysFont初始化的。然后,我的文本是blit到屏幕上的一个display.update,以保持它不断地在顶层的层。即使在散列创建精灵和blits的代码之后,文本也不会显示出来,相反,鼠标坐标、键盘和鼠标按钮将显示在文本框中的屏幕下面。任何东西都有帮助,因为我刚开始编写代码。

代码语言:javascript
运行
复制
import pygame
import os
import random
import sys
pygame.init()
pygame.font.init()

# Create Screen
FrameHeight = 3000
FrameWidth = 10000
screen = pygame.display.set_mode((FrameWidth, FrameHeight))

#Create Sprites
flag = True
background = pygame.image.load("fantasy-village-bg.jpg")
icon1 = pygame.image.load("Elder.png")
Dialogue = pygame.image.load("Dialogue-box.png")

def village():
#Repeating loop for beginning shot of the game
  while flag == True:

 #Background loops

   screen.fill((23, 234, 80))
   screen.blit(background, (0, 0))
  
 #Village elder loops
   screen.blit(icon1, (0, 0))
  
 #Dialogue box loops
 
   screen.blit(Dialogue, (-100, 400))
   pygame.display.flip()
  
 
village()
# PYGAME FRAME WINDOW and documentation for keylogging and mouse tracking
pygame.mouse.set_visible(0)
pygame.display.set_caption("Riftka: Adventure Awaits")

#Dealing with the event of a crash"""
crashed = False

while not crashed:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            crashed = True
        print(event)
    pygame.display.update()
    
    
#First set of dialogue 

black = (0,0,0)
WINDOW_WIDTH  = 500
WINDOW_HEIGHT = 500

def show_text( msg, color, x=WINDOW_WIDTH//2, y=WINDOW_WIDTH//2 ):
    global WINDOW
    text = font.render( msg, True, color)
    WINDOW.blit(text, ( x, y ) )

pygame.init()
WINDOW = pygame.display.set_mode((WINDOW_WIDTH,WINDOW_HEIGHT))
pygame.display.set_caption("Text")

# Create the font (only needs to be done once)
font = pygame.font.SysFont(None, 25)

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            exit()

    show_text("We have been waiting for you traveller", blue)

    pygame.display.update()
EN

回答 1

Stack Overflow用户

发布于 2022-09-06 13:25:05

图像是按它们在屏幕上的顺序分层的。

我已经将您的显示文本循环分开,并添加了一些绘图函数来显示分层

代码语言:javascript
运行
复制
import pygame
pygame.init()

black = (0,0,0)
blue = pygame.Color("dodgerblue")
WINDOW_WIDTH  = 500
WINDOW_HEIGHT = 500

def show_text( window, font, msg, color,):
    text = font.render( msg, True, color)
    # center the text on the window
    text_rect = text.get_rect(center=window.get_rect().center)
    window.blit(text, text_rect)

pygame.init()
WINDOW = pygame.display.set_mode((WINDOW_WIDTH,WINDOW_HEIGHT))
pygame.display.set_caption("Text")

# Create the font (only needs to be done once)
font = pygame.font.SysFont(None, 25)
clock = pygame.time.Clock()
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            exit()
    WINDOW.fill(black)
    pygame.draw.rect(WINDOW, pygame.Color("purple"), [250, 200, 200, 100])
    show_text(WINDOW, font, "We have been waiting for you traveller", blue)
    pygame.draw.rect(WINDOW, (pygame.Color("red")), [320, 220, 50, 60])
    pygame.display.update()
    clock.tick(30)  # limit FPS

这个应该是这样的:

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73622099

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档