Python:pygame界面显示文字详解
再简单的游戏界面中均涉及文字处理,本节主要解读一下pygame模块中对文字及字体的处理方式。 同样,以实例进行讲解,先看看代码:
-
#!/usr/bin/env python
-
# -*- coding: utf-8 -*-
-
import sys
-
import os
-
import pygame
-
from pygame.locals import *
-
def load_image(pic_name):
-
'''
-
Function:图片加载函数
-
Input:pic_name 图片名称
-
Output: NONE
-
author: dyx1024
-
blog:http://blog.****.net/dyx1024
-
date:2012-04-15
-
'''
-
#获取当前脚本文件所在目录的绝对路径
-
current_dir = os.path.split(os.path.abspath(__file__))[0]
-
#指定图片目录
-
path = os.path.join(current_dir, 'image', pic_name)
-
#加载图片
-
return pygame.image.load(path).convert()
-
def init_windows():
-
'''
-
Function:窗口初始化
-
Input:NONE
-
Output: NONE
-
author: dyx1024
-
blog:http://blog.****.net/dyx1024
-
date:2012-04-21
-
'''
-
pygame.init()
-
display_surface = pygame.display.set_mode((600, 500))
-
pygame.display.set_caption('游戏中的文字处理(http://blog.****.net/dyx1024)')
-
return display_surface
-
def exit_windows():
-
'''
-
Function:退出处理
-
Input:NONE
-
Output: NONE
-
author: dyx1024
-
blog:http://blog.****.net/dyx1024
-
date:2012-04-21
-
'''
-
pygame.quit()
-
sys.exit()
-
def main():
-
'''
-
Function:字体处理
-
Input:NONE
-
Output: NONE
-
author: dyx1024
-
blog:http://blog.****.net/dyx1024
-
date:2012-04-21
-
'''
-
screen_surface = init_windows()
-
back_image = load_image('mengqiqi.jpg')
-
color_red = (255, 0, 0)
-
color_green = (0, 255, 0)
-
color_blue = (0, 0, 255)
-
#第一组文字
-
#创建一个Font对象,其中LOWRBI__.TTF为下载的字体库
-
fontObj = pygame.font.Font('LOWRBI__.TTF', 32)
-
#创建一个存放文字surface对象,
-
textSurfaceObj = fontObj.render(u'HELLO MONCHHICHI', False, color_green)
-
#文字图像位置
-
textRectObj = textSurfaceObj.get_rect()
-
#第二组文字
-
fontObj2 = pygame.font.Font('simkai.TTF', 20)
-
#添加下画线
-
fontObj2.set_underline(True)
-
textSurfaceObj2 = fontObj2.render(u'很萌,有木有!', False, color_red)
-
textRectObj2 = textSurfaceObj2.get_rect()
-
textRectObj2.center = (80, 480)
-
#第三组文字
-
#使用系统字体
-
fontObj3 = pygame.font.SysFont('宋体', 20)
-
#加粗
-
fontObj3.set_bold(True)
-
#斜体
-
fontObj3.set_italic(True)
-
#文字具有蓝色背景
-
textSurfaceObj3 = fontObj3.render(u'又到凌晨了,睡', True, color_red, color_blue)
-
textRectObj3 = textSurfaceObj3.get_rect()
-
textRectObj3.center = (500, 10)
-
while True:
-
#绘图
-
screen_surface.blit(back_image, (0, 0))
-
screen_surface.blit(textSurfaceObj, textRectObj)
-
screen_surface.blit(textSurfaceObj2, textRectObj2)
-
screen_surface.blit(textSurfaceObj3, textRectObj3)
-
for event in pygame.event.get():
-
if event.type == QUIT:
-
exit_windows()
-
pygame.display.update()
-
if __name__ == '__main__':
-
main()
运行:
做一些解释:
1、fontObj = pygame.font.Font('LOWRBI__.TTF', 32)
此句创建了一个Font类的对象,原型为:pygame.font.Font(filename, size): return Font
其中filename为字体文件,windows下字体文件所在目录为:C:\WINDOWS\Fonts,以TTF后续结尾。
注意,最好将你要用到的文件复制一份到脚本所在目录下,这样在游戏发布时,可以一起打包,解决了目标机器上可能没有所使用字体的问题,另,如果需要特殊字体,可从网上下载,使用方法一样。
第二个参数size为字体大小。
2、textSurfaceObj = fontObj.render(u'HELLO MONCHHICHI', False, color_green)
text = font.render(u"Hello 我爱你", 1, (10, 10, 10)) #显示内容必须转换成Unicode,否则中文不能正常显示
此句为Font对象fontObj加载内容,并可设置颜色,格式等。
原型如下:Font.render(text, antialias, color, background=None): return Surface
参数解释:
text :要显示的文字内容,仅支持单行,即不能使用\n进行换行,如要打印多行,要建立多个font对象。
antialias:字体的边缘是否平滑,true表示平滑,false表示带有毛边。
color:文字颜色,取值RGB
background:文字背景色,可选。
3、textRectObj = textSurfaceObj.get_rect()
为testSurface创建一个Rect对象,大小由所有显示的文本高度及宽度决定。
4、设置字体加粗、下画线、斜体等属性,使用以下方法。
Font.set_bold(bool): return None
Font.set_underline(bool): return None
Font.set_italic (bool): return None