Step-by-step analysis of the problem:
- Error Identification: The error message indicates that the
get_font method of the GameState class is being called with 3 positional arguments, but it only accepts 2.
- Method Signature: The
get_font method is likely defined with a signature similar to def get_font(self, font_name), where self is the implicit reference to the instance of the class and font_name is the expected argument.
- Error Location: The error occurs in the
show_message_box function when calling gs.get_font('main', FONT_SIZE).
- Cause of Error: The
get_font method does not accept a font size as an argument, but FONT_SIZE is being passed as a second argument.
Fixed solution:
def show_message_box(gs, text, delay=500, redraw_background_func=None, redraw_args=()):
"""Показывает всплывающее сообщение."""
if redraw_background_func:
redraw_background_func(*redraw_args)
# Assuming FONT_SIZE is a constant or variable defined elsewhere
main_font = gs.get_font('main') # Removed FONT_SIZE from here
main_font = pygame.font.SysFont(main_font.name, FONT_SIZE) # Set font size here
small_font = gs.get_font('small')
small_font = pygame.font.SysFont(small_font.name, FONT_SIZE // 2) # Assuming smaller font size for 'small'
text_surface = main_font.render(text, True, COLORS['BLACK'])
text_w, text_h = text_surface.get_size()
padding_x, padding_y = 20, 15
box_width = text_w + 2 * padding_x
max_box_width = gs.WIDTH - 40
if box_width > max_box_width:
text_surface = small_font.render(text, True, COLORS['BLACK'])
text_w, text_h = text_surface.get_size()
box_width = text_w + 2 * padding_x
final_width = min(box_width, max_box_width)
final_height = text_h + 2 * padding_y
box_rect = pygame.Rect(0, 0, final_width, final_height)
box_rect.center = (gs.WIDTH // 2, gs.HEIGHT - 70)
box_rect.clamp_ip(gs.screen.get_rect())
pygame.draw.rect(gs.screen, COLORS['MESSAGE_BOX_BG'], box_rect, border_radius=10)
pygame.draw.rect(gs.screen, COLORS['BLACK'], box_rect, 2, border_radius=10)
gs.screen.blit(text_surface, text_surface.get_rect(center=box_rect.center))
pygame.display.flip()
pygame.time.delay(delay)
Explanation of changes:
- Removed FONT_SIZE from
get_font call: The get_font method does not accept a font size, so FONT_SIZE was removed from the method call.
- Set font size after getting font: The font size is set after getting the font using . This assumes that returns a font name or a font object that can be used to create a new font object with the desired size.