【问题标题】:Text not displaying in PygamePygame 中不显示文本
【发布时间】:2018-11-20 19:28:38
【问题描述】:

问题 - 我正在尝试为 simon 说游戏制作开始菜单并尝试首先显示一些文本,但是除了游戏屏幕变白(应该会发生)之外什么都没有显示。我看过类似的问题,但它们似乎没有帮助。

程序应该只在屏幕上用黑色字体显示“西蒙说”

代码:

import pygame,random,pickle,time

WHITE = (255,255,255)
BLACK = (0,0,0)
RED = (255,0,0)
GREEN = (0,255,0)
BLUE = (0,0,255)
YELLOW = (255,255,0)
PURPLE = (128,0,128)
CYAN = (0,255,255)
ORANGE = (255,165,0)
PINK = (255,192,203)

pygame.init()
logo = pygame.image.load("logo.png")
pygame.display.set_icon(logo)
pygame.display.set_caption("Simon Says")

screen = pygame.display.set_mode((1280,720))

def textObjects(text,font):
   textSurface = font.render(text,True,BLACK)
   return textSurface, textSurface.get_rect()

def displayText(text):
    fontInfo = pygame.font.Font(None,100)
    textSurf, textSize = textObjects(text,fontInfo)
    textSize.center = ((1280/2),(720/2))
    screen.blit(textSurf,textSize)
    pygame.display.update()

def startMenu():
    displayText('Simon Says')
def main():
    startMenu() 
    running = True
    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
        screen.fill(WHITE)
        pygame.display.update()
        pygame.display.flip()

main()

也尝试使用 pygame.display.flip() ,但也没有用

【问题讨论】:

    标签: python text pygame game-development


    【解决方案1】:

    实际上一切正常,但你犯了一个简单的错误。 请注意,您正在调用的第一行

    startMenu()
    

    函数,它基本上将文本“西蒙说”绘制到屏幕上。 BUT 同样,在此之后您将屏幕填充为白色。 因此,它绘制文本的表面并将其渲染到屏幕上,然后立即将屏幕变为白色(基本上它在文本表面上绘制白色)。 因此,如果您希望它出现,只需将该功能放在此行之后:

    screen.fill(WHITE)
    

    【讨论】:

      猜你喜欢
      • 2021-07-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-06
      • 2014-01-17
      • 2020-02-29
      • 2017-04-03
      相关资源
      最近更新 更多