1. 程式人生 > >python中利用pygame模組輸出文字

python中利用pygame模組輸出文字

import pygame,sys
from pygame.locals import *
pygame.init() #初始化pygame模組
DISPLAYSURF = pygame.display.set_mode((400,300)) #長400,寬300
pygame.display.set_caption('Hello World!')
BLACK = (0,0,0)
GREEN = (0,255,0)
BLUE = (0,0,128)

fontObj = pygame.font.Font('freesansbold.ttf',32)
#render方法返回Surface物件
textSurfaceObj = fontObj.render('Hello world!',True,GREEN,BLUE)
#get_rect()方法返回rect物件
textRectObj = textSurfaceObj.get_rect()
textRectObj.center=(200,150)

while True:
	DISPLAYSURF.fill(BLACK)
	DISPLAYSURF.blit(textSurfaceObj,textRectObj)
	for event in pygame.event.get():
		if event.type==QUIT:
			pygame.quit()
			sys.exit()
	pygame.display.update()