Advertisement
furas

Pygame Button test

Apr 24th, 2019
304
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.80 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. #
  4. # pygame (empty) template - by furas
  5. #
  6.  
  7. # ---------------------------------------------------------------------
  8.  
  9. __author__  = 'Bartlomiej "furas" Burek'
  10. __webpage__ = 'http://blog.furas.pl'
  11.  
  12. # ---------------------------------------------------------------------
  13.  
  14. import pygame
  15.  
  16. # === CONSTANS === (UPPER_CASE names)
  17.  
  18. BLACK = (  0,   0,   0)
  19. WHITE = (255, 255, 255)
  20.  
  21. RED   = (255,   0,   0)
  22. GREEN = (  0, 255,   0)
  23. BLUE  = (  0,   0, 255)
  24.  
  25. SCREEN_WIDTH  = 600
  26. SCREEN_HEIGHT = 400
  27.  
  28. # === CLASSES === (CamelCase names)
  29.  
  30. import pygame.font
  31.  
  32. class Button():
  33.  
  34.     def __init__(self, ai_settings, screen, msg):
  35.         """Inicjalizacja atrybutów przycisku."""
  36.         self.screen = screen
  37.         self.screen_rect = screen.get_rect()
  38.  
  39.         #Zdefiniowanie wymiarów i właściwości przycisku.
  40.         self.width, self.height = 200, 50
  41.         self.button_color = (0, 255, 0)
  42.         self.text_color = (100, 255, 100)
  43.         self.font = pygame.font.SysFont(None, 48)
  44.  
  45.         #Utworzenie prostokąta przycisku i wyświetlenie go.
  46.         self.rect = pygame.Rect(0, 0, self.width, self.height)
  47.         self.rect.center = self.screen_rect.center
  48.         self.prep_msg(msg)
  49.  
  50.     def prep_msg(self, msg):
  51.         """Umieszczenie komunikatu w wygenerowanym obrazie i
  52.        wyśrodkowanie tekstu na przycisku"""
  53.         self.msg_image = self.font.render(msg, True, self.text_color,
  54.         self.button_color)
  55.         self.msg_image_rect = self.msg_image.get_rect()
  56.         self.msg_image_rect.center = self.rect.center
  57.  
  58.     def draw_button(self):
  59.         #Wyświtlenie pustego przycisku, a następnie komunikatu na nim.
  60.         self.screen.fill(self.button_color, self.rect)
  61.         self.screen.blit(self.msg_image, self.msg_image_rect)
  62.        
  63. # === FUNCTIONS === (lower_case names)
  64.  
  65.     # empty
  66.    
  67. # === MAIN === (lower_case names)
  68.  
  69. # --- (global) variables ---
  70.  
  71. # --- init ---
  72.  
  73. pygame.init()
  74.  
  75. screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
  76. screen_rect = screen.get_rect()
  77.  
  78. # --- objects ---
  79.  
  80. b = Button(None, screen, "Hello")
  81.  
  82. # --- mainloop ---
  83.  
  84. clock = pygame.time.Clock()
  85. is_running = True
  86.  
  87. while is_running:
  88.  
  89.     # --- events ---
  90.    
  91.     for event in pygame.event.get():
  92.  
  93.         # --- global events ---
  94.        
  95.         if event.type == pygame.QUIT:
  96.             is_running = False
  97.         elif event.type == pygame.KEYDOWN:
  98.             if event.key == pygame.K_ESCAPE:
  99.                 is_running = False
  100.  
  101.         # --- objects events ---
  102.  
  103.             # empty
  104.        
  105.     # --- updates ---
  106.  
  107.         # empty
  108.    
  109.     # --- draws ---
  110.    
  111.     screen.fill(BLACK)
  112.  
  113.     b.draw_button()
  114.    
  115.     pygame.display.update()
  116.  
  117.     # --- FPS ---
  118.  
  119.     clock.tick(25)
  120.  
  121. # --- the end ---
  122.    
  123. pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement