Last active
December 13, 2015 17:39
-
-
Save craigmjohnston/4949663 to your computer and use it in GitHub Desktop.
Draws a pretty block using pygame's draw functions. Written up for this tutorial: http://superbase.tumblr.com/post/43037844981/presenting-pretty-blocks-programmatically-in-python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pygame | |
import sys | |
def lightened(colour, amount): | |
newcolour = pygame.Color(colour.r, colour.g, colour.b, colour.a) | |
h, s, l, a = newcolour.hsla | |
if l+amount > 100: l = 100 | |
elif l+amount < 0: l = 0 | |
else: l += amount | |
newcolour.hsla = (h, s, l, a) | |
return newcolour | |
def draw_block(surface, x, y, size, colour): | |
base_rect = pygame.Rect(x, y, size, size) | |
inner_rect = pygame.Rect(y+(size*3.0/10)/2, y+(size*3.0/10)/2, size*7/10, size*7/10) | |
top_triangle = [(base_rect.topleft[0], base_rect.topleft[1]-1), | |
(base_rect.topright[0]-1, base_rect.topright[1]-1), | |
base_rect.center] | |
bottom_triangle = [(base_rect.bottomleft[0], base_rect.bottomleft[1]-1), | |
(base_rect.bottomright[0]-1, base_rect.bottomright[1]-1), | |
base_rect.center] | |
pygame.draw.rect(surface, colour, base_rect) | |
pygame.draw.polygon(surface, lightened(colour, 10), top_triangle) | |
pygame.draw.polygon(surface, lightened(colour, -10), bottom_triangle) | |
pygame.draw.rect(surface, lightened(colour, 5), inner_rect) | |
pygame.init() | |
screen = pygame.display.set_mode((320, 320)) | |
screen.fill(0x363743) | |
draw_block(screen, 50, 50, 200, pygame.Color(0x88C10000)) | |
pygame.display.update() | |
while True: | |
for event in pygame.event.get(): | |
if event.type == pygame.QUIT: | |
pygame.quit() | |
sys.exit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment