Info class

Here Info class, interface shows that the majority are done by it, init function create_info_labels function to create generic information, create_state_labels functions for different states, different information will be initialized.

Info class ():
DEF the __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 is)
. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
10
. 11
create_font_image_dict function from the previously loaded image GFX [ 'text_images'], the interception of letters and numbers corresponding graphics, stored in a set, the text will be used when creating later.

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 function from a large Surface sheet in accordance with the area (x, y, width, height) into the cut out portion of the image corresponding to the Surface image start position (0,0), and adjust the size according to the scale parameter.
The functions described below 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 (the colorkey)
Image pg.transform.scale = (Image,
(int (* Scale rect.width),
int ( * Scale rect.height)))
return Image
. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
10
look create_info_labels a function where string 'MARIO' is how to display on the interface.
create_label function parameters (x, y) represents the start position of the string at the interface, to obtain a corresponding object from self.image_dict Surface according characters.
set_label_rects function sets a value for each string of Surface objects rect (x, y).

pygame.Rect common object member variables (x, y), it represents the position of the upper left corner of the Surface.
top, bottom: Surface y-axis represents the uppermost and lowermost values, the top and y values are the same as
left, right: Surface x-axis represents the leftmost and rightmost value, the left and the value of x is same
. 1
2
. 3
the following graph it can be seen in the upper left corner of the entire screen is the origin (0,0), are identified in FIG coordinates of four vertices of a rectangle 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

Guess you like

Origin www.cnblogs.com/hyhy904/p/11535778.html