Skip to content

Instantly share code, notes, and snippets.

@jjst
Created November 15, 2014 16:01
Show Gist options
  • Save jjst/5e256b07b5603e2dbb49 to your computer and use it in GitHub Desktop.
Save jjst/5e256b07b5603e2dbb49 to your computer and use it in GitHub Desktop.
pi-heart
import itertools
import re
import pygame
import sys
import colorsys
import numpy as np
WHITE = ( 255, 255, 255)
if __name__ == '__main__':
pygame.init()
# Set the width and height of the screen [width, height]
screen_width = 1200
screen_height = 800
size = (screen_width, screen_height)
screen = pygame.display.set_mode(size)
# Loop until the user clicks the close button.
done = False
# Used to manage how fast the screen updates
clock = pygame.time.Clock()
# For hue, generate floats between 1 -> 0. We want a lot of points
# as we want hue to move slowly.
hue_values = np.linspace(1, 0, 2000)
# For brightness, we want a sinusoid that changes fast. We also want
# the brightness to stay long, and the darkness to be short-lived, so
# we'll generate an uneven sinusoid using log values between 0 and pi,
# with the most values concentrated closer to 0 where the cos is greater.
base = 3
log_values = (np.logspace(0, 1, base=base) - 1)/(base - 1)
log_values = np.pi * np.append(log_values[::-1], log_values)
sinusoid = np.cos(log_values)
# We adjust the sinusoid to that it varies between 0 and 1
brightness_values = (sinusoid+ 1)/2.
hsv_variations = itertools.izip(
itertools.cycle(hue_values),
itertools.cycle(brightness_values))
# -------- Main Program Loop -----------
while not done:
# --- Main event loop
for event in pygame.event.get(): # User did something
if event.type == pygame.QUIT: # If user clicked close
done = True # Flag that we are done so we exit this loop
# --- Game logic should go here
# --- Drawing code should go here
# First, clear the screen to white. Don't put other drawing commands
# above this, or they will be erased with this command.
hue, brightness = next(hsv_variations)
print "hue=",hue,"brightness=",brightness
color = colorsys.hsv_to_rgb(hue, 1, brightness)
color = [int(c * 255) for c in color]
print color
screen.fill(color)
# --- Go ahead and update the screen with what we've drawn.
pygame.display.flip()
# --- Limit to 60 frames per second
clock.tick(20)
# Close the window and quit.
# If you forget this line, the program will 'hang'
# on exit if running from IDLE.
pygame.quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment