JClock IV

Combining the built-in pygame function pygame.draw.arc() with time, I came up with this alternative clock design.
The red arc represents hours, the green one minutes, the blue seconds and the white arc represents centiseconds (a hundredth of a second).
Although it is a clock, you read it anti-clockwise.

The clockface


# Python 2.7.7 Code
# Pygame 1.9.1 (for Python 2.7.7)
# Jonathan Frech 2nd of August, 2015

# importing needed modules
import pygame, sys, time, math, os, random, datetime

""" CLASSES """
# dummy class for global variables
class dummy():
	pass

""" FUNCTIONS """
# quits the program
def quit():
	sys.exit()

""" TICK; RENDER """
# tick function
def tick():
	# handle events
	for event in pygame.event.get():
		# quit
		if event.type == pygame.QUIT:
			quit()

# render function
def render():
	# fill
	main.SURF.fill([0, 0, 0])
	
	# variables
	now = datetime.datetime.now()
	shift = math.pi/2
	width = main.WIDTH / (2*4)
	circle = (2 * math.pi)
	
	# draw the four arcs
	pygame.draw.arc(main.SURF, [255, 0,   0  ], [0 * width, 0 * width, main.WIDTH - 2 * 0 * width, main.WIDTH - 2 * 0 * width], shift, circle / 24.  * now.hour                + shift, width)
	pygame.draw.arc(main.SURF, [0,   255, 0  ], [1 * width, 1 * width, main.WIDTH - 2 * 1 * width, main.WIDTH - 2 * 1 * width], shift, circle / 60.  * now.minute              + shift, width)
	pygame.draw.arc(main.SURF, [0,   0,   255], [2 * width, 2 * width, main.WIDTH - 2 * 2 * width, main.WIDTH - 2 * 2 * width], shift, circle / 60.  * now.second              + shift, width)
	pygame.draw.arc(main.SURF, [255, 255, 255], [3 * width, 3 * width, main.WIDTH - 2 * 3 * width, main.WIDTH - 2 * 3 * width], shift, circle / 100. * now.microsecond / 10000 + shift, width)
	
	# blit and flip
	main.SCREEN.blit(main.SURF, [0, 0])
	pygame.display.flip()
	
	# update caption
	if main.SHOWTIME:
		pygame.display.set_caption(main.CAPTION + " (" + str(now.hour) + ":"  + str(now.minute) + ":"  + str(now.second) + ")")

""" INIT """
# initialize program
def init():
	main.WIDTH = 400
	main.SIZE = [main.WIDTH, main.WIDTH]
	main.SCREEN = pygame.display.set_mode(main.SIZE)
	main.SURF = pygame.Surface(main.SIZE)
	
	main.CAPTION = "JClock IV"
	main.TICKS = 0
	
	# shows time in screen's caption
	main.SHOWTIME = False
	
	# functions
	pygame.display.set_caption(main.CAPTION)

""" RUN """
# run function (uses tick() and render())
def run():
	ticksPerSecond = 60
	lastTime = time.time() * 1000000000
	nsPerTick =  1000000000.0 / float(ticksPerSecond)
	
	ticks = 0
	frames = 0
	
	lastTimer = time.time() * 1000
	delta = 0.0
	
	while True:
		now = time.time() * 1000000000
		delta += float(now - lastTime) / float(nsPerTick)
		lastTime = now
		shouldRender = False
				
		while delta >= 1:
			ticks += 1
			main.TICKS += 1
			tick()
			delta -= 1
			shouldRender = True
		
		if shouldRender:
			frames += 1
			render()
		
		if time.time() * 1000 - lastTimer >= 1000:
			lastTimer += 1000
			
			# debug
			# print("Frames: " + str(frames) + ", ticks: " + str(ticks))
			
			frames = 0
			ticks = 0

# main variable
main = dummy()
init()

# start program
run()
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s