在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中字體的使用
在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()
源碼下載:http://files.cnblogs.com/hongten/pygame_font_frog.rar
========================================================
More reading,and english is important.
I'm Hongten
大哥哥大姐姐,覺得有用打賞點哦!多多少少沒關系,一分也是對我的支持和鼓勵。謝謝。
Hongten博客排名在100名以內。粉絲過千。
Hongten出品,必是精品。
E | hongtenzone@foxmail.com B | http://www.cnblogs.com/hongten
========================================================