JClock II

This is an alternative clock design. Version two is even prettier to look at.
Instead of a few clock hands spinning their circles, every hour, minute, second and centisecond is represented as differently colored circles on four imagined circles around the center. That way you have a visualization by quantity, not by position.
21:46:42
This clock face would represent 21:46:42.

How to read the clock

  • 21 green circles
    • represents 21 hours
  • 46 teal circles
    • represents 46 minutes
  • 42 blue circles
    • represents 42 seconds
  • some white circles
    • represents centiseconds (one hundredth of a second, mainly for the look)
  • Current time: 21:46:42

# Python 2.7.7 Code
# Pygame 1.9.1 (for Python 2.7.7)
# Jonathan Frech 18th of May, 2015
#         edited 19th of May, 2015
#         edited 20th of May, 2015

# gets the distance between two points
def getDistance(p1, p2):
	return int(
				math.sqrt((p2[0] - p1[0])**2 + (p2[1] - p1[1])**2)
			)

# validates color integer
# extra feature: _min and _max implementation
def colorValid(_color, _min = 0, _max = 255):
	newColor = math.fabs(_color)
	n = _max - _min
	if newColor > n:
		if int(newColor / n) % 2 == 0:
			newColor = newColor % n
		else:
			newColor = n - (newColor % n)
	
	return int(newColor) + _min

# gets the position on a circle
# circle center                           : '_pos'
# circle radius                           : '_radius'
# angle from center to point on the circle: '_angle'
def getCirclePos(_pos, _radius, _angle):
	# if angle equals 0, it faces to north
	_angle -= 90
	
	return [
				_pos[0] + _radius * math.cos(math.radians(_angle)),
				_pos[1] + _radius * math.sin(math.radians(_angle))
			]

# swaps a boolean
def boolSwap(_bool):
	if _bool:
		return False
	else:
		return True

# returns an integer version of given position
def intPos(_pos):
	return [int(_pos[0]), int(_pos[1])]

# entity class (individiual circles)
class entity():
	# init
	def __init__(self, _type, _num):
		self.type = _type
		self.num = _num
		
		self.nullPos = main.CENTERFLOAT[:]
		self.pos = self.nullPos[:]
		self.destination = self.nullPos[:]
		
		# if self is a...
		if self.type == "hour":
			self.radius = 15
			self.dist = 300
			self.color = [0, 255, 0]
			self.max = 24-1
			self.speed = 200
		elif self.type == "minute":
			self.radius = 10
			self.dist = 200
			self.color = [0, 255, 255]
			self.max = 60-1
			self.speed = 100
		elif self.type == "second":
			self.radius = 5
			self.dist = 100
			self.color = [0, 0, 255]
			self.max = 60-1
			self.speed = 50
		elif self.type == "csecond":
			self.radius = 2
			self.dist = 50
			self.color = [255, 255, 255]
			self.max = 100-1
			self.speed = 5
	
	# tick function
	def tick(self, _date):
		# standard destination
		self.destination = self.nullPos[:]
		
		# calculate if self should be active
		if self.type == "hour":
			if _date.hour > self.num:
				self.destination = getCirclePos(main.CENTERFLOAT, self.dist, 360. / self.max * self.num)
		elif self.type == "minute":
			if _date.minute > self.num:
				self.destination = getCirclePos(main.CENTERFLOAT, self.dist, 360. / self.max * self.num)
		elif self.type == "second":
			if _date.second > self.num:
				self.destination = getCirclePos(main.CENTERFLOAT, self.dist, 360. / self.max * self.num)
		elif self.type == "csecond":
			if _date.microsecond / 10000 > self.num:
				self.destination = getCirclePos(main.CENTERFLOAT, self.dist, 360. / self.max * self.num)
		
		# move to destination
		for _ in range(0, 2):
			dif = self.destination[_] - self.pos[_]
			self.pos[_] += dif / float(self.speed)
	
	# render
	def render(self, _surface):
		# do not render if self is really close to the center
		if getDistance(self.pos, main.CENTERFLOAT) > 1:
			pygame.draw.circle(_surface, self.color, intPos(self.pos), self.radius)

# tick function
def tick():
	# handle events
	for event in pygame.event.get():
		# quit
		if event.type == pygame.QUIT:
				sys.exit()
	
	now = datetime.datetime.now()
	for _ in main.ENTITIES:
		_.tick(now)

# render function
def render():
	# fill
	main.SCREEN.fill(main.BACKGROUNDCOLOR)
	if main.FILL:
		main.SURF.fill(main.COLOR)
	
	for _ in main.ENTITIES:
		_.render(main.SURF)
	
	# blit and flip
	main.SCREEN.blit(pygame.transform.scale(main.SURF, [int(main.WIDTH * main.SCALE), int(main.HEIGHT * main.SCALE)]), [0, 0])
	pygame.display.flip()

# 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

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

# init function
# (create the entities)
def init():
	for _ in range(0, 24):
		main.ENTITIES.append(entity("hour", _))
	for _ in range(0, 60):
		main.ENTITIES.append(entity("minute", _))
	for _ in range(0, 60):
		main.ENTITIES.append(entity("second", _))
	for _ in range(0, 100):
		main.ENTITIES.append(entity("csecond", _))

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

# main dummy
main = dummy()
main.TICKS = 0

main.RES = 1
main.WIDTH = 1080 / main.RES
main.HEIGHT = 720 / main.RES
main.SIZE = [main.WIDTH, main.HEIGHT]
main.CENTER = [main.WIDTH / 2, main.HEIGHT / 2]
main.CENTERFLOAT = [main.WIDTH / 2.0, main.HEIGHT / 2.0]

main.SCALE = 1 * main.RES
main.SCALEDSIZE = [int(main.WIDTH * main.SCALE), int(main.HEIGHT * main.SCALE)]
main.SCALEDSIZEFLOAT = [main.WIDTH * main.SCALE, main.HEIGHT * main.SCALE]

main.SURF = pygame.Surface(main.SIZE)

main.SCREEN = pygame.display.set_mode(main.SCALEDSIZE)
pygame.display.set_caption("JClock II")

main.BACKGROUNDCOLOR = [0, 0, 0]
main.COLOR = [150, 0, 0]
main.FILL = True

main.ENTITIES = []
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