Python基于pygame實現(xiàn)的font游戲字體(附源碼)
本文實例講述了Python基于pygame實現(xiàn)的font游戲字體。分享給大家供大家參考,具體如下:
在pygame游戲開發(fā)中,一個友好的UI中,漂亮的字體是少不了的
今天就給大伙帶來有關pygame中字體的一些介紹說明
首先我們得判斷一下我們的pygame中有沒有font這個模塊
如果有的話才可以進行接下來的操作:-)
我們可以這樣使用pygame中的字體:
當然也可以使用系統(tǒng)中自帶的字體:
參數(shù)一:字體名稱
參數(shù)二:字體大小
比較一下上面兩個方法,一個是自定義的字體,一個是系統(tǒng)自帶的字體,相對而言
自定義的字體要好一點,因為在pygame打包的過程中,可以把自定義的字體打包進去
這樣就可以進行很好的移植;而系統(tǒng)自帶的字體, 畢竟不是每個系統(tǒng)都有相應的字體,
所以他的移植性不是很好,依賴性很大。
如果定義好了字體,那么我們應該把字體顯示到suiface上面去,我們應該這樣操作:
參數(shù)一:顯示的內(nèi)容
參數(shù)二:是否開啟抗鋸齒,就是說True的話字體會比較平滑,不過相應的速度有一點點影響
參數(shù)三:字體顏色
參數(shù)四:字體背景顏色(可選)即可以這樣:
下面給出一個demo,說說pygame中字體的使用

在demo中,玩家可以使用鍵盤上的:上,下,左,右四個方向鍵進行控制青蛙的移動,
在移動的過程中,左下角會動態(tài)記錄青蛙的位置情況。
代碼部分如下:
#python font
import os, pygame
from pygame.locals import *
from sys import exit
__author__ = {'name' : 'Hongten',
'mail' : 'hongtenzone@foxmail.com',
'Version' : '1.0'}
if not pygame.font: print('Warning, fonts disabled')
pygame.init()
SCREEN_DEFAULT_SIZE = (500, 500)
BG_IMAGE_NAME = 'bg.gif'
FROG_IMAGE_NAME = 'frog.gif'
TORK_FONT_NAME = 'TORK____.ttf'
bg_image_path = os.path.join('data\\image', BG_IMAGE_NAME)
frog_image_path = os.path.join('data\\image', FROG_IMAGE_NAME)
tork_font_path = os.path.join('data\\font', TORK_FONT_NAME)
if not os.path.exists(bg_image_path):
print('Can\'t found the background image:', bg_image_path)
if not os.path.exists(frog_image_path):
print('Can\'t fount the frog image:', frog_image_path)
if not os.path.exists(tork_font_path):
print('Can\'t fount the font:', tork_font_path)
screen = pygame.display.set_mode(SCREEN_DEFAULT_SIZE, 0, 32)
bg = pygame.image.load(bg_image_path).convert()
frog = pygame.image.load(frog_image_path).convert_alpha()
tork_font = pygame.font.Font(tork_font_path, 20)
frog_x, frog_y = 0, 0
frog_move_x, frog_move_y = 0, 0
while 1:
for event in pygame.event.get():
if event.type == QUIT:
exit()
elif event.type == KEYDOWN:
if event.key == K_LEFT:
frog_move_x = -1
elif event.key == K_UP:
frog_move_y = -1
elif event.key == K_RIGHT:
frog_move_x = 1
elif event.key == K_DOWN:
frog_move_y = 1
elif event.type == KEYUP:
frog_move_x = 0
frog_move_y = 0
frog_x += frog_move_x
frog_y += frog_move_y
#print(frog_x, frog_y)
screen.blit(bg, (0, 0))
position_str = 'Position:' + str(frog_x) + ',' + str(frog_y)
position = tork_font.render(position_str, True, (255, 255,255), (23, 43,234))
screen.blit(position, (0, 480))
screen.blit(frog, (frog_x, frog_y))
pygame.display.update()
完整實例代碼代碼點擊此處本站下載。
希望本文所述對大家Python程序設計有所幫助。
相關文章
Python中列表和字符串常用的數(shù)據(jù)去重方法總結(jié)
關于數(shù)據(jù)去重,咱們這里簡單理解下,就是刪除掉重復的數(shù)據(jù),應用的場景比如某些產(chǎn)品產(chǎn)生的大數(shù)據(jù),有很多重復的數(shù)據(jù),為了不影響分析結(jié)果,我們可能需要對這些數(shù)據(jù)進行去重,所以本文給大家總結(jié)了Python中列表和字符串常用的數(shù)據(jù)去重方法,需要的朋友可以參考下2023-11-11
pytorch中的model=model.to(device)使用說明
這篇文章主要介紹了pytorch中的model=model.to(device)使用說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-05-05
使用Python串口實時顯示數(shù)據(jù)并繪圖的例子
今天小編就為大家分享一篇使用Python串口實時顯示數(shù)據(jù)并繪圖的例子,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-12-12

