#!/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()
分享到:
相关推荐
一个pygame的小游戏示例代码, 里面包括资源和必要的安装步骤
rapt-pygame-example, 使用RAPT封装 Pygame(_sdl2) 游戏的示例 RAPT Pygame示例这就是如何在Android上实现简单Pygame游戏的一个例子。步骤 0: 准备游戏。请确保你的游戏的主文件名为 main.py.请确保你的游戏将以...
基于pygame框架实现简单的弹球示例,让新手更好的理解pygame的运行方法和初步了解pygame的使用方法,要求对python语言有一定的基础,对游戏开发有兴趣。欢迎大家的下载
pygame中文文档
内容包含:Pygame官方中文文档(v1.9.2).pdf 下载中包括pygame文档的...以及一个pygame示例文件夹(大约43个)。 还有PDF格式的pygame文档 以及文章的源代码。 网页连接:https://www.pygame.org/docs/ref/surface.html
Pygame是 跨平台 Python模块,专为电子游戏设计。 包含图像、声音。建立在SDL基础上,允许实时电子游戏研发而无需被低级语言,如C语言束缚。基于这样一个设想,所有需要的游戏...这个文档是Pygame 2的文档,可以看一看
安装pygame与pip
由Python语言,基于模块pygame开发的简易3D引擎
基于python+pygame的外星人入侵小游戏源码+超详细注释.zip基于python+pygame的外星人入侵小游戏源码+超详细注释.zip基于python+pygame的外星人入侵小游戏源码+超详细注释.zip基于python+pygame的外星人入侵小游戏...
Pygame写的2D小游戏 import time import random import os import sys import pygame from pygame.locals import *
Python pygame游戏25个...2. 直接运行即可,部分游戏带了参考文档,如贪吃蛇、飞机大战等。 3. 适合用于课程设计,或者pygame游戏开发的学习。 4. 附带了pygame中文教程和一些资料,欢迎下载,有问题可以私信与我交流。
自己一个字一个字敲出来的pygame笔记,翻译注释了常用的模块.以及常见的坑.涵盖了pygame文档中大部分模块的基础功能.配合本人之前上传的pygame飞机打陨石小游戏的多版本迭代源码观看更佳.祝pygame爱好者少走弯路!
python烟花代码;基于Python的pygame实现的烟花代码示例
pygame多字拼音输入法,需要pygame和pinyin2hanzi
pygame 源码及安装包 1.9.1 for python 2.7 开发python游戏开源模块 Pygame is a set of Python modules designed for writing games. Pygame adds functionality on top of the excellent SDL library. This ...
pygame没有特定的输入框,不易实现登陆界面,我们采用存储按下按键的信息,再将其展示出来,通过不断地重绘实现与tkinter模块实现登录界面近似的效果,甚至更佳。通过添加列表存储用户名和密码,可以实现特定用户和...
pygame-1.9.1release pygame-1.9.1release pygame-1.9.1release
这是辛星写的pygame教程的第二份,希望对大家有所帮助。
Pygame 是一组用来开发游戏软件的 Python 程序模块,基于 SDL 库的基础上开发。允许你在 Python 程序中创建功能丰富的游戏和多媒体程序,Pygame 是一个高可移植性的模块可以支持多个操作系统。 《pygame中文手册》...
python基于pygame的俄罗斯方块小游戏源码。python基于pygame的俄罗斯方块小游戏源码。python基于pygame的俄罗斯方块小游戏源码。python基于pygame的俄罗斯方块小游戏源码。python基于pygame的俄罗斯方块小游戏源码。...