pygame示例注释二

#!/usr/bin/env python
#coding=utf-8
"""Here we load a .TTF font file, and display it in
a basic pygame window. It demonstrates several of the
Font object attributes. Nothing exciting in here, but
it makes a great example for basic window, event, and
font management."""


import pygame
from pygame.locals import *
from pygame.compat import unichr_, unicode_
import sys
import locale


if sys.version_info >= (3,):
def print_unicode(s):
e = locale.getpreferredencoding()
print (s.encode(e, 'backslashreplace').decode())
else:
def print_unicode(s):
e = locale.getpreferredencoding()
print (s.encode(e, 'backslashreplace'))

def main():
#initialize
pygame.init()#pygame初始化
resolution = 400, 200
screen = pygame.display.set_mode(resolution)#设置屏幕长宽

## pygame.mouse.set_cursor(*pygame.cursors.diamond)

fg = 250, 240, 230
bg = 5, 5, 5
wincolor = 40, 40, 90

#fill background
screen.fill(wincolor)#用颜色填充背景

#load font, prepare values
font = pygame.font.Font(None, 150)#设置字体大小
text = 'China'#要显示的字体
size = font.size(text)#

#no AA, no transparancy, normal
ren = font.render(text, 0, fg, bg)#用前景色 背景色渲染文本,并返回Surface
screen.blit(ren, (10, 10))#将文字绘制到屏幕

#no AA, transparancy, underline
font.set_underline(1)#设置下划线
ren = font.render(text, 0, fg)#用前景色渲染文本
#screen.blit(ren, (10, 40 + size[1]))#将文本绘制到屏幕
font.set_underline(0)#去掉字体下划线


a_sys_font = pygame.font.SysFont("Arial", 60)#从系统的字体资源创建一个pygame字体


#AA, no transparancy, bold 3
a_sys_font.set_bold(1)#设置粗体
ren = a_sys_font.render(text, 1, fg, bg)#用字体渲染文本
screen.blit(ren, (30 + size[0], 10))#将文本绘制的屏幕
a_sys_font.set_bold(0)#取消字体粗体显示

#AA, transparancy, italic 4
a_sys_font.set_italic(1)
ren = a_sys_font.render(text, 1, fg)
screen.blit(ren, (30 + size[0], 40 + size[1]))
a_sys_font.set_italic(0)


# Get some metrics.
print ("Font metrics for 'Fonty': %s" % a_sys_font.metrics (text))
ch = unicode_("%c") % 0x3060
msg = (unicode_("Font metrics for '%s': %s") %
(ch, a_sys_font.metrics (ch)))
print_unicode(msg)

## #some_japanese_unicode = u"\u304b\u3070\u306b"
##some_japanese_unicode = unicode_('%c%c%c') % (0x304b, 0x3070, 0x306b)

#AA, transparancy, italic
##ren = a_sys_font.render(some_japanese_unicode, 1, fg)
##screen.blit(ren, (30 + size[0], 40 + size[1]))



#show the surface and await user quit
pygame.display.flip()
while 1:
#use event.wait to keep from polling 100% cpu
if pygame.event.wait().type in (QUIT, KEYDOWN, MOUSEBUTTONDOWN):#关闭按钮,按下任意键,按下鼠标
break


if __name__ == '__main__': main()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值