pygame系列 font遊戲字型

2022-07-30 01:30:17 字數 3738 閱讀 7432

在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__ =

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()

pygame中font模組方法詳解

目錄 系統方法 pygame.font.init pygame.font.quit pygame.font.get init pygame.font.get default font pygame.font.get fonts pygame.font.match font pygame.font.s...

pygame遊戲 Pygame遊戲製作教程

pygame遊戲設計第一彈 pygame程式設計框架 www.bilibili.com pygame繪製圖形前,首先要建立乙個視窗 import pygame 使用pygame的第一步是將pygame庫匯入到python程式中 from pygame.locals import 然後需要引入pyga...

css系列 font文字

可以定義多個字型,系統會依次查詢,比如 courier new 字型不存在將使用courier以此類推。要使用通用字型,比如你電腦裡有後盾人宋體在你電腦可以正常顯示,但不保證在其他使用者電腦可以正常,因為他們可能沒有這個字型。font family courier new courier,monos...