下面介绍Info类,界面的显示大部分都是由它来完成,init函数中create_info_labels函数创建通用的信息,create_state_labels函数对于不同的状态,会初始化不同的信息。
class Info():
def __init__(self, game_info, state):
self.coin_total = game_info[c.COIN_TOTAL]
self.total_lives = game_info[c.LIVES]
self.state = state
self.game_info = game_info
self.create_font_image_dict()
self.create_info_labels()
self.create_state_labels()
self.flashing_coin = FlashCoin(280, 53)
1
2
3
4
5
6
7
8
9
10
11
create_font_image_dict函数从之前加载的图片GFX[‘text_images’]中,截取字母和数字对应的图形,保存在一个set中,在后面创建文字时会用到。
def create_font_image_dict(self):
self.image_dict = {}
image_list = []
image_rect_list = [# 0 - 9
(3, 230, 7, 7), (12, 230, 7, 7), (19, 230, 7, 7),
(27, 230, 7, 7), (35, 230, 7, 7), (43, 230, 7, 7),
(51, 230, 7, 7), (59, 230, 7, 7), (67, 230, 7, 7),
(75, 230, 7, 7),
# A - Z
(83, 230, 7, 7), (91, 230, 7, 7), (99, 230, 7, 7),
(107, 230, 7, 7), (115, 230, 7, 7), (123, 230, 7, 7),
(3, 238, 7, 7), (11, 238, 7, 7), (20, 238, 7, 7),
(27, 238, 7, 7), (35, 238, 7, 7), (44, 238, 7, 7),
(51, 238, 7, 7), (59, 238, 7, 7), (67, 238, 7, 7),
(75, 238, 7, 7), (83, 238, 7, 7), (91, 238, 7, 7),
(99, 238, 7, 7), (108, 238, 7, 7), (115, 238, 7, 7),
(123, 238, 7, 7), (3, 246, 7, 7), (11, 246, 7, 7),
(20, 246, 7, 7), (27, 246, 7, 7), (48, 246, 7, 7),
# -*
(68, 249, 6, 2), (75, 247, 6, 6)]
character_string = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ -*'
for character, image_rect in zip(character_string, image_rect_list):
self.image_dict[character] = get_image(GFX['text_images'],
*image_rect, (92, 148, 252), 2.9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
get_image函数从一个大的Surface sheet 中按照 area(x, y, width, height)截取出部分图片 放入Surface image对应的起始位置(0,0),并按照scale参数调整大小。
pygame的 blit 函数介绍如下
pg.Surface.blit(source, dest, area=None, special_flags=0) -> Rect
draw one image onto another
1
2
def get_image(sheet, x, y, width, height, colorkey, scale):
image = pg.Surface([width, height])
rect = image.get_rect()
image.blit(sheet, (0, 0), (x, y, width, height))
image.set_colorkey(colorkey)
image = pg.transform.scale(image,
(int(rect.width*scale),
int(rect.height*scale)))
return image
1
2
3
4
5
6
7
8
9
10
看一下create_info_labels函数中其中一个字符串’MARIO’是如何在界面上显示的。
create_label函数参数 (x, y) 表示字符串在界面上的起始位置,从self.image_dict中根据字符获取对应的Surface 对象。
set_label_rects函数会设置字符串中每一个Surface 对象 rect 的(x, y)值。
pygame.Rect 对象中常用的成员变量(x,y),表示这个Surface的左上角的位置。
top, bottom: 表示Surface 在y轴上最上边和最下边的值, 所以top和y 值是一样的
left, right: 表示Surface 在x轴上最左边和最右边的值,所以left 和x 值是一样的
1
2
3
下面的坐标图可以看到,在左上角是整个屏幕的原点(0,0), 图中标识了长方形rect的四个顶点的坐标。
def create_info_labels(self):
...
self.mario_label = []
...
self.create_label(self.mario_label, 'MARIO', 75, 30)
def create_label(self, label_list, string, x, y):
for letter in string:
label_list.append(Character(self.image_dict[letter]))
self.set_label_rects(label_list, x, y)
def set_label_rects(self, label_list, x, y):
for i, letter in enumerate(label_list):
letter.rect.x = x + ((letter.rect.width + 3) * i)
letter.rect.y = y
if letter.image == self.image_dict['-']:
letter.rect.y += 7
letter.rect.x += 2