Данная функция облегчит форматирование текста в пайгейм
благодаря ней теперь:
def blit_text(surface = screen, text = 'input text', pos = (0,0), font = pygame.font.SysFont("impact", 50), color=pygame.Color('red'), button = False, FX = 'None'):
words = [word.split(' ') for word in text.splitlines()] # 2D array where each row is a list of words.
space = font.size(' ')[0] # The width of a space.
max_width, max_height = surface.get_size()
x, y = pos
for line in words:
for word in line:
word_surface = font.render(word, 0, color)
word_width, word_height = word_surface.get_size()
if x word_width >= max_width:
x = pos[0]
y = word_height
#if debug:
# pygame.draw.rect(surface, (64, 128, 255), (x, y, word_width, word_height), 8)
if button:
mouse_x, mouse_y = pygame.mouse.get_pos()
if x < mouse_x < x word_width and y < mouse_y < y word_height:
if FX == 'console>':
FX_surface = font.render('>', 0, color)
FX_width, FX_height = FX_surface.get_size()
x = x - FX_width
word = '>' word
if FX == 'move':
x = x 15
word_surface = font.render(word, 0, (0 if color[0] <= 50 else color[0] - 50, 0 if color[1] <= 50 else color[1] - 50, 0 if color[2] <= 50 else color[2] - 50))
if pygame.mouse.get_pressed()[0]:
surface.blit(word_surface, (x, y))
x = word_width space
return True
surface.blit(word_surface, (x, y))
x = word_width space
x = pos[0]
y = word_height
Пример использования:
if blit_text(text = 'Выход',pos = (50,175), font = FontVRC, color = (100, 255, 100), button = True, FX='console>'): game_active = False
здесь создаётся кнопка выхода которая выключает цикл игры
тут пропущена переменная canvas которая отвечает за то на каком канвасе разместить текст дальше уже по порядку идёт text в который вводится текст нужный вам далее pos где в картеже записываются координаты левого верхнего угла
в переменной font вводится шрифт пайгейма
в color цвет картежом из 3ёх чисел до 255
в button записывается буллевое значение True или False
В FX пока пишется только 'console>' или 'move' для эффектов при наведений (можно не вводить и тогда кнопки будут просто затемнятся

