Text 在pygame中如何在屏幕上显示文本
我的问题是我想做的就是在pygame的屏幕上显示文本。如果有人知道怎么做,请告诉我 我的代码Text 在pygame中如何在屏幕上显示文本,text,pygame,Text,Pygame,我的问题是我想做的就是在pygame的屏幕上显示文本。如果有人知道怎么做,请告诉我 我的代码 import time import pygame from pygame.locals import * pygame.init blue = (0,0,255) WINDOW_WIDTH = 500 WINDOW_HEIGHT = 500 WINDOW = pygame.display.set_mode((WINDOW_WIDTH,WINDOW_HEIGHT)) pygame.display.set
import time
import pygame
from pygame.locals import *
pygame.init
blue = (0,0,255)
WINDOW_WIDTH = 500
WINDOW_HEIGHT = 500
WINDOW = pygame.display.set_mode((WINDOW_WIDTH,WINDOW_HEIGHT))
pygame.display.set_caption("Text")
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit
exit()
font = pygame.font.SysFont(None, 25)
def show_text(msg,color):
text = font.render(msg,True,color)
WINDOW.blit(text,[WINDOW_WIDTH/2,WIDTH_HEIGHT/2])
show_text("This is a message!", blue)
pygame.display.update()
我只想制作一条文字,上面写着“这是一条信息!”。就这些你已经很接近了。要呈现文本,您需要首先定义
字体
,然后使用它来呈现()。这将创建一个包含文本的位图,该位图必须是窗口的blit()
所有必要的部分都在问题代码中,它们只是有点混淆
import time
import pygame
from pygame.locals import *
# Constants
blue = (0,0,255)
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()
WINDOW.fill( ( 255, 255, 255 ) ) # fill screen with white background
show_text("This is a message!", blue)
pygame.display.update()
哦,谢谢你最近回答了我很多问题,我真的很感激!你给我的代码真的很好很简单,我真的很喜欢!