Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- #
- # pygame (simple) template - by furas
- #
- # https://github.com/furas/my-python-codes/tree/master/pygame/__template__/
- #
- # ---------------------------------------------------------------------
- __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
- BLOCK_SIZE = 50
- # === CLASSES === (CamelCase names)
- '''
- class Button():
- '''
- # === 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 ---
- '''
- button = Button(...)
- '''
- rects = []
- for x in range(10):
- rects.append( pygame.Rect(x*(BLOCK_SIZE+5), BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE) )
- selected = None
- # --- 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
- elif event.type == pygame.MOUSEBUTTONDOWN:
- if event.button == 1:
- for i, r in enumerate(rects):
- if r.collidepoint(event.pos):
- selected = i
- selected_offset_x = r.x - event.pos[0]
- selected_offset_y = r.y - event.pos[1]
- elif event.type == pygame.MOUSEBUTTONUP:
- if event.button == 1:
- selected = None
- elif event.type == pygame.MOUSEMOTION:
- if selected is not None: # selected can be `0` so `is not None` is required
- # move object
- rects[selected].x = event.pos[0] + selected_offset_x
- rects[selected].y = event.pos[1] + selected_offset_y
- # --- objects events ---
- '''
- button.handle_event(event)
- '''
- # --- updates ---
- # empty
- # --- draws ---
- screen.fill(BLACK)
- '''
- button.draw(screen)
- '''
- # draw rect
- for r in rects:
- pygame.draw.rect(screen, RED, r)
- pygame.display.update()
- # --- FPS ---
- clock.tick(25)
- # --- the end ---
- pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement