Bartłomiej 'furas' Burek
furas.pl
# prywatne notatki - Python, Linux, Machine Learning, etc.

PyGame rysowanie tła w szachownicę

python pygame - draw background checkerboard
import pygame

# --- constants --- (UPPER_CASE_NAMES)

SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600

FPS = 25

BLACK  = (0, 0, 0)
WHITE  = (255, 255, 255)
GREEN  = (0, 255, 0)
RED    = (255, 0, 0)
RED    = (255, 0, 0)
BLUE   = (0, 0, 255)
YELLOW = (255,255,0)

GRAY_1 = (128,128,128)
GRAY_2 = (192,192,192)

# --- main ---

pygame.init()

screen = pygame.display.set_mode( (SCREEN_WIDTH, SCREEN_HEIGHT) )

# ---

background = pygame.surface.Surface(screen.get_size())

color = GRAY_1
for x in range(0, SCREEN_WIDTH, 40):
    for y in range(0, SCREEN_HEIGHT, 40):
        pygame.draw.rect(background, color, [x, y, 40, 40])
        if color == GRAY_1:
            color = GRAY_2
        else:
            color = GRAY_1

# ---

surface = pygame.surface.Surface(screen.get_size())
surface.set_colorkey([0,0,0])

pygame.draw.polygon(surface, RED,    [(300,100), (400,300), (500,100)])
pygame.draw.polygon(surface, RED,    [(300,100), (400,300), (500,100)])
pygame.draw.polygon(surface, GREEN,  [(300,300), (400,300), (500,300)])
pygame.draw.polygon(surface, BLUE,   [(300,100), (400,300), (300,300)])
pygame.draw.polygon(surface, YELLOW, [(500,100), (400,300), (500,300)])

lower = True
alpha = 255

# --- mainloop ---

clock = pygame.time.Clock()

running = True
while running:

    # --- events ---

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

        elif event.type == pygame.KEYUP:
            if event.key == pygame.K_ESCAPE:
                running = False

    # --- changes ---

    if lower:
        alpha -= 10
        if alpha <= 10:
            lower = False
    else: 
        alpha += 10
        if alpha >= 250:
            lower = True

    surface.set_alpha(alpha)

    # --- draws ---

    screen.blit(background, (0,0))

    screen.blit(surface, (0,0))

    pygame.display.flip()

    # --- FPS ---

    ms = clock.tick(FPS)
    #pygame.display.set_caption('{}ms'.format(ms)) # 40ms for 25FPS, 16ms for 60FPS
    fps = clock.get_fps()
    pygame.display.set_caption('FPS: {}'.format(fps))

# --- end ---

pygame.quit()
Książki: python-dla-kazdego-podstawy-programowania python-wprowadzenie python-leksykon-kieszonkowy python-receptury python-programuj-szybko-i-wydajnie python-projekty-do-wykorzystania black-hat-python-jezyk-python-dla-hackerow-i-pentesterow efektywny-python-59-sposobow-na-lepszy-kod tdd-w-praktyce-niezawodny-kod-w-jezyku-python aplikacje-internetowe-z-django-najlepsze-receptury