Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- #
- # pygame (empty) template - by furas
- #
- # ---------------------------------------------------------------------
- __author__ = 'Bartlomiej "furas" Burek'
- __webpage__ = 'http://blog.furas.pl'
- # ---------------------------------------------------------------------
- import pygame
- # === CONSTANS === (UPPER_CASE names)
- BLACK = ( 0, 0, 0)
- WHITE = (255, 255, 255)
- RED = (255, 0, 0)
- GREEN = ( 0, 255, 0)
- BLUE = ( 0, 0, 255)
- SCREEN_WIDTH = 600
- SCREEN_HEIGHT = 400
- # === CLASSES === (CamelCase names)
- import pygame.font
- class Button():
- def __init__(self, ai_settings, screen, msg):
- """Inicjalizacja atrybutów przycisku."""
- self.screen = screen
- self.screen_rect = screen.get_rect()
- #Zdefiniowanie wymiarów i właściwości przycisku.
- self.width, self.height = 200, 50
- self.button_color = (0, 255, 0)
- self.text_color = (100, 255, 100)
- self.font = pygame.font.SysFont(None, 48)
- #Utworzenie prostokąta przycisku i wyświetlenie go.
- self.rect = pygame.Rect(0, 0, self.width, self.height)
- self.rect.center = self.screen_rect.center
- self.prep_msg(msg)
- def prep_msg(self, msg):
- """Umieszczenie komunikatu w wygenerowanym obrazie i
- wyśrodkowanie tekstu na przycisku"""
- self.msg_image = self.font.render(msg, True, self.text_color,
- self.button_color)
- self.msg_image_rect = self.msg_image.get_rect()
- self.msg_image_rect.center = self.rect.center
- def draw_button(self):
- #Wyświtlenie pustego przycisku, a następnie komunikatu na nim.
- self.screen.fill(self.button_color, self.rect)
- self.screen.blit(self.msg_image, self.msg_image_rect)
- # === FUNCTIONS === (lower_case names)
- # empty
- # === MAIN === (lower_case names)
- # --- (global) variables ---
- # --- init ---
- pygame.init()
- screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
- screen_rect = screen.get_rect()
- # --- objects ---
- b = Button(None, screen, "Hello")
- # --- mainloop ---
- clock = pygame.time.Clock()
- is_running = True
- while is_running:
- # --- events ---
- for event in pygame.event.get():
- # --- global events ---
- if event.type == pygame.QUIT:
- is_running = False
- elif event.type == pygame.KEYDOWN:
- if event.key == pygame.K_ESCAPE:
- is_running = False
- # --- objects events ---
- # empty
- # --- updates ---
- # empty
- # --- draws ---
- screen.fill(BLACK)
- b.draw_button()
- pygame.display.update()
- # --- FPS ---
- clock.tick(25)
- # --- the end ---
- pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement