暂无描述

button.py 1.2KB

12345678910111213141516171819202122232425262728293031323334
  1. import pygame.font
  2. class Button():
  3. def __init__(self, ai_settings, screen, msg):
  4. """Initialize button attributes."""
  5. self.screen = screen
  6. self.screen_rect = screen.get_rect()
  7. # Set the dimensions and properties of the button.
  8. self.width, self.height = 200, 50
  9. self.button_color = (0, 255, 0)
  10. self.text_color = (255, 255, 255)
  11. self.font = pygame.font.SysFont(None, 48)
  12. # Build the button's rect object, and center it.
  13. self.rect = pygame.Rect(0, 0, self.width, self.height)
  14. self.rect.center = self.screen_rect.center
  15. # The button message only needs to be prepped once.
  16. self.prep_msg(msg)
  17. def prep_msg(self, msg):
  18. """Turn msg into a rendered image, and center text on the button."""
  19. self.msg_image = self.font.render(msg, True, self.text_color,
  20. self.button_color)
  21. self.msg_image_rect = self.msg_image.get_rect()
  22. self.msg_image_rect.center = self.rect.center
  23. def draw_button(self):
  24. # Draw blank button, then draw message.
  25. self.screen.fill(self.button_color, self.rect)
  26. self.screen.blit(self.msg_image, self.msg_image_rect)