상세 컨텐츠

본문 제목

pygame 에서 텍스트를 어떻게 표시 하나요?

pygame

by 안졸리냐젤리 2021. 10. 15. 01:25

본문

###질문###

파이게임에서 텍스트를 디스플레이 하는 법을 잘 모르겠습니다.
IDLE 에서 print 하는 방식으로는 할수 없다는걸 잘 알고 있습니다.

 

import pygame, sys
from pygame.locals import *

BLACK = ( 0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = ( 255, 0, 0)

pygame.init()
size = (700, 500)
screen = pygame.display.set_mode(size)

DISPLAYSURF = pygame.display.set_mode((400, 300))
pygame.display.set_caption('P.Earth')
while 1: # main game loop
    for event in pygame.event.get():
        if event.type == QUIT:           
            pygame.display.update() 

import time

direction = ''
print('Welcome to Earth')
pygame.draw.rect(screen, RED, [55,500,10,5], 0)
time.sleep(1)

위의 코드는 제 프로그램의 초반 도입 부분 입니다.

텍스트를 표시 하는 법을 알려 주세요~

 

###답변###

pygame.font.init() # you have to call this at the start, 
                   # if you want to use this module.
myfont = pygame.font.SysFont('Comic Sans MS', 30)
textsurface = myfont.render('Some Text', False, (0, 0, 0))
screen.blit(textsurface,(0,0))

###답변###

import pygame
import pygame.freetype  # Import the freetype module.


pygame.init()
screen = pygame.display.set_mode((800, 600))
GAME_FONT = pygame.freetype.Font("your_font.ttf", 24)
running =  True

while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    screen.fill((255,255,255))
    # You can use `render` and then blit the text surface ...
    text_surface, rect = GAME_FONT.render("Hello World!", (0, 0, 0))
    screen.blit(text_surface, (40, 250))
    # or just `render_to` the target surface.
    GAME_FONT.render_to(screen, (40, 350), "Hello World!", (0, 0, 0))

    pygame.display.flip()

pygame.quit()

#출처  : https://stackoverflow.com/questions/20842801/how-to-display-text-in-pygame

관련글 더보기