欢迎光临
我们一直在努力

pygame系列_font游戏字体_源码下载

pygame游戏开发中,一个友好的UI中,漂亮的字体是少不了的

今天就给大伙带来有关pygame中字体的一些介绍说明

首先我们得判断一下我们的pygame中有没有font这个模块

1 if not pygame.font: print('Warning, fonts disabled')

如果有的话才可以进行接下来的操作:-)

我们可以这样使用pygame中的字体:

1 tork_font = pygame.font.Font('data\\font\\TORK____.ttf', 20)

当然也可以使用系统中自带的字体:

1 my_font = pygame.font.SysFont("arial", 10)

参数一:字体名称

参数二:字体大小

比较一下上面两个方法,一个是自定义的字体,一个是系统自带的字体,相对而言

自定义的字体要好一点,因为在pygame打包的过程中,可以把自定义的字体打包进去

这样就可以进行很好的移植;而系统自带的字体, 毕竟不是每个系统都有相应的字体,

所以他的移植性不是很好,依赖性很大。

如果定义好了字体,那么我们应该把字体显示到suiface上面去,我们应该这样操作:

1 position = tork_font.render('Hello,I\'m Hongten', True, (255, 255,255), (23, 43,234))

参数一:显示的内容

参数二:是否开启抗锯齿,就是说True的话字体会比较平滑,不过相应的速度有一点点影响

参数三:字体颜色

参数四:字体背景颜色(可选)即可以这样:

1 position = tork_font.render('Hello,I\'m Hongten', True, (255, 255,255))

下面给出一个demo,说说pygame中字体的使用

pygame系列_font游戏字体_源码下载

在demo中,玩家可以使用键盘上的:上,下,左,右四个方向键进行控制青蛙的移动,

在移动的过程中,左下角会动态记录青蛙的位置情况。

=============================================

代码部分:

=============================================

 1 #python font  2  3 import os, pygame  4 from pygame.locals import *  5 from sys import exit  6  7 __author__ = {'name' : 'Hongten',  8 'mail' : 'hongtenzone@foxmail.com',  9 'blog' : 'http://www.cnblogs.com/hongten', 10 'Version' : '1.0'} 11 12 if not pygame.font: print('Warning, fonts disabled') 13 14 pygame.init() 15 SCREEN_DEFAULT_SIZE = (500, 500) 16 BG_IMAGE_NAME = 'bg.gif' 17 frog_IMAGE_NAME = 'frog.gif' 18 TORK_FONT_NAME = 'TORK____.ttf' 19 20 bg_image_path = os.path.join('data\\image', BG_IMAGE_NAME) 21 frog_image_path = os.path.join('data\\image', FROG_IMAGE_NAME) 22 tork_font_path = os.path.join('data\\font', TORK_FONT_NAME) 23 24 if not os.path.exists(bg_image_path): 25 print('Can\'t found the background image:', bg_image_path) 26 if not os.path.exists(frog_image_path): 27 print('Can\'t fount the frog image:', frog_image_path) 28 if not os.path.exists(tork_font_path): 29 print('Can\'t fount the font:', tork_font_path) 30 31 screen = pygame.display.set_mode(SCREEN_DEFAULT_SIZE, 0, 32) 32 bg = pygame.image.load(bg_image_path).convert() 33 frog = pygame.image.load(frog_image_path).convert_alpha() 34 tork_font = pygame.font.Font(tork_font_path, 20) 35 36 frog_x, frog_y = 0, 0 37 frog_move_x, frog_move_y = 0, 0 38 39 while 1: 40 for event in pygame.event.get(): 41 if event.type == QUIT: 42  exit() 43 elif event.type == KEYDOWN: 44 if event.key == K_LEFT: 45 frog_move_x = -1 46 elif event.key == K_UP: 47 frog_move_y = -1 48 elif event.key == K_RIGHT: 49 frog_move_x = 1 50 elif event.key == K_DOWN: 51 frog_move_y = 1 52 elif event.type == KEYUP: 53 frog_move_x = 0 54 frog_move_y = 0 55 frog_x += frog_move_x 56 frog_y += frog_move_y 57 #print(frog_x, frog_y) 58  screen.blit(bg, (0, 0)) 59 position_str = 'Position:' + str(frog_x) + ',' + str(frog_y) 60 position = tork_font.render(position_str, True, (255, 255,255), (23, 43,234)) 61 screen.blit(position, (0, 480)) 62  screen.blit(frog, (frog_x, frog_y)) 63 pygame.display.update()

源码下载:https://files.cnblogs.com/hongten/pygame_font_frog.rar

 

 

========================================================

 

More reading,and english is important.

 

I'm Hongten

 

pygame系列_font游戏字体_源码下载 

 

大哥哥大姐姐,觉得有用打赏点哦!多多少少没关系,一分也是对我的支持和鼓励。谢谢。
Hongten博客排名在100名以内。粉丝过千。
Hongten出品,必是精品。

 

E | hongtenzone@foxmail.com  B | http://www.cnblogs.com/hongten

 

========================================================

 

  • 海报
海报图正在生成中...
赞(0) 打赏
声明:
1、本博客不从事任何主机及服务器租赁业务,不参与任何交易,也绝非中介。博客内容仅记录博主个人感兴趣的服务器测评结果及一些服务器相关的优惠活动,信息均摘自网络或来自服务商主动提供;所以对本博客提及的内容不作直接、间接、法定、约定的保证,博客内容也不具备任何参考价值及引导作用,访问者需自行甄别。
2、访问本博客请务必遵守有关互联网的相关法律、规定与规则;不能利用本博客所提及的内容从事任何违法、违规操作;否则造成的一切后果由访问者自行承担。
3、未成年人及不能独立承担法律责任的个人及群体请勿访问本博客。
4、一旦您访问本博客,即表示您已经知晓并接受了以上声明通告。
文章名称:《pygame系列_font游戏字体_源码下载》
文章链接:https://www.456zj.com/37016.html
本站资源仅供个人学习交流,请于下载后24小时内删除,不允许用于商业用途,否则法律问题自行承担。

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址