import pygame
from random import randint
from apscheduler.schedulers.blocking import BlockingScheduler
import datetime
class random_square(object):
def __init__(self, max_x_location, max_y_location):
self.x_loc = randint(0, max_x_location)
self.y_loc = randint(0, max_y_location)
max_color_value = 255
red = randint(0, max_color_value)
green = randint(0, max_color_value)
blue = randint(0, max_color_value)
self.color = [red, green, blue]
class clock(object):
def __init__(self, initial_count, max_count, screen_w, screen_h):
self.max_count = max_count
self.screen_w = screen_w
self.screen_h = screen_h
# create the screen object, force pygame fullscreen mode
self.screen = pygame.display.set_mode([screen_w, screen_h], pygame.FULLSCREEN)
# the screen's width in pixels is stored in the 0th element of the array
self.square_size = screen_w / 200
# create the list of squares, initially as empty
self.squares = []
# fill the squares with the inital seconds until midnight
for second in range(initial_count):
self.squares.append(random_square(screen_w, screen_h))
# starts ticking the clock
def start(self):
scheduler = BlockingScheduler()
scheduler.add_job(self.tick, 'interval', seconds=1)
try:
scheduler.start()
except (KeyboardInterrupt, SystemExit):
pass
# this occurs once every time a unit of time elapses
def tick(self):
# this will happen once per "day"
if len(self.squares) == 0:
# fill the list of squares to be drawn
for second in range(self.max_count):
self.squares.append(random_square(self.screen_w, self.screen_h))
# draw a blank screen
self.screen.fill([0, 0, 0])
# draw the squares
for square in self.squares:
rect = (square.x_loc, square.y_loc, self.square_size, self.square_size)
pygame.draw.rect(self.screen, square.color, rect, 0)
pygame.display.update()
# remove a single square from the list as one tick has elapsed
self.squares.pop()
# initialize pygame
pygame.init()
# figure out the parameters of the display we're connected to
screen_width = pygame.display.Info().current_w
screen_height = pygame.display.Info().current_h
screen_size = screen_width, screen_height
# determine the number of seconds until midnight
seconds_in_a_day = 86400
now = datetime.datetime.now()
midnight = now.replace(hour=0, minute=0, second=0, microsecond=0)
seconds_until_midnight = seconds_in_a_day - (now - midnight).seconds
# create and start the clock!
cl = clock(seconds_until_midnight, seconds_in_a_day, screen_width, screen_height)
cl.start()