Last active
November 1, 2017 17:57
-
-
Save Roadmaster/ea08f8893170df59a4d0477fea7be4f6 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#!/usr/bin/env python | |
import pygame | |
from random import randint, choice | |
def main(): | |
pygame.init() | |
screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN) | |
info = pygame.display.Info() | |
clock = pygame.time.Clock() | |
font = pygame.font.SysFont("comicsansms", int(info.current_h / 1.25)) | |
radius = 15 | |
mode = 'blue' | |
points = [] | |
text = "" | |
letter = 'A' | |
known_letters = ['A', 'E', 'I', 'O', 'U', 'Y', 'B'] | |
while True: | |
pressed = pygame.key.get_pressed() | |
alt_held = pressed[pygame.K_LALT] or pressed[pygame.K_RALT] | |
ctrl_held = pressed[pygame.K_LCTRL] or pressed[pygame.K_RCTRL] | |
for event in pygame.event.get(): | |
# determin if X was clicked, or Ctrl+W or Alt+F4 was used | |
if event.type == pygame.QUIT: | |
return | |
if event.type == pygame.KEYDOWN: | |
if event.key == pygame.K_w and ctrl_held: | |
return | |
if event.key == pygame.K_F4 and alt_held: | |
return | |
if event.key == pygame.K_ESCAPE: | |
return | |
# determine if a letter key was pressed | |
if event.key == pygame.K_r: | |
mode = 'red' | |
elif event.key == pygame.K_g: | |
mode = 'green' | |
elif event.key == pygame.K_b: | |
mode = 'blue' | |
try: | |
newletter = chr(event.key) | |
except ValueError: | |
newletter = letter | |
finally: | |
letter = newletter.upper() | |
if letter not in known_letters: | |
letter = choice(known_letters) | |
text = font.render(letter, True, (0, 180, 128)) | |
if event.type == pygame.MOUSEBUTTONDOWN: | |
if event.button == 1: # left click grows radius | |
radius = min(200, radius + 1) | |
elif event.button == 3: # right click shrinks radius | |
radius = max(1, radius - 1) | |
if event.type == pygame.MOUSEMOTION: | |
# if mouse moved, add point to list | |
position = event.pos | |
points = points + [position] | |
points = points[-256:] | |
# draw all points | |
i = 0 | |
while i < len(points) - 1: | |
drawLineBetween(screen, i, points[i], points[i + 1], radius, mode) | |
i += 1 | |
screen.fill((0, 0, 0)) | |
if text: | |
xpos = (info.current_w / 2) - text.get_width() // 2 | |
ypos = (info.current_h / 2) - text.get_height() // 2 | |
screen.blit(text, (xpos, ypos)) | |
pygame.display.flip() | |
clock.tick(60) | |
def drawLineBetween(screen, index, start, end, width, color_mode): | |
c1 = max(0, min(255, 2 * index - 256)) | |
c2 = max(0, min(255, 2 * index)) | |
if color_mode == 'blue': | |
color = (c1, c1, c2) | |
elif color_mode == 'red': | |
color = (c2, c1, c1) | |
elif color_mode == 'green': | |
color = (c1, c2, c1) | |
dx = start[0] - end[0] | |
dy = start[1] - end[1] | |
iterations = max(abs(dx), abs(dy)) | |
for i in range(iterations): | |
progress = 1.0 * i / iterations | |
aprogress = 1 - progress | |
x = aprogress * start[0] + progress * end[0] | |
y = aprogress * start[1] + progress * end[1] | |
pygame.draw.circle(screen, color, (int(x), int(y)), width) | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment