JClock VII

This alternative clock is not really readable by human. It calculates the first 144¹ primes, assigns 60 of them to every possible second, 60 to every possible minute and 24 to every possible hour.
Multiplying those three primes for a given time results in a composite number representing said time. Using integer factorization, you then can get the three primes back, map them to seconds, minutes and hours, and by doing so calculate the time.

2 minutes of prime time¹This number is the sum of 60 seconds, 60 minutes and 24 hours.


# Python 2.7.7 Code
# Pygame 1.9.1 (for Python 2.7.7)
# Jonathan Frech 13th of November, 2015

# importing needed modules
import pygame, sys, time, math, os, random, datetime
pygame.font.init()

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

""" FUNCTIONS """
# saves the current surface
def savesurf():
	try:
		if not os.path.isdir(main.SAVEPATH):
			os.mkdir(main.SAVEPATH)

		name = "img" + addzeros(len(os.listdir(main.SAVEPATH))) + ".png"
		pygame.image.save(pygame.transform.scale(main.SURF, main.SCALESIZE), main.SAVEPATH + name)
	except:
		pass

# calculate primes up to n
def getprimes(n):
	primes = []
	w = 2
	while len(primes) < n:
		prime = True
		for _ in range(2, w):
			if w % _ == 0:
				prime = False
		if prime:
			primes.append(w)
		w += 1
	return primes

# returns a string with fixed length
def addzeros(n):
	length = 5

	if n > 10 ** (length+1) - 1:
		n = 10 ** (length+1) - 1
	string = str(n)
	while len(string) < length:
		string = "0" + string

	return string

# saves gif (used for animated gif in the article)
def savegif():
	# save all frames for two minutes, starting at 13:00:00
	for m in range(0, 2):
		for s in range(0, 60):
			render(True, s, m, 13)
	quit()

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

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

# render function
def render(_faketime = False, _second = 0, _minute = 0, _hour = 0):
	# fill
	main.SURF.fill([0, 0, 0])
	
	if not _faketime:
		now = datetime.datetime.now()
		_second = now.second
		_minute = now.minute
		_hour = now.hour

	second = main.PRIMES[_second]
	minute = main.PRIMES[_minute+60]
	hour = main.PRIMES[_hour+60+60]
	composite = second * minute * hour
	
	text1 = main.FONTBIG.render(str(composite), 1, [255, 255, 255])
	rect1 = text1.get_rect()
	pos1 = [(main.WIDTH - rect1.right) / 2, (main.HEIGHT - rect1.bottom) / 2]
	
	text2 = main.FONTSMALL.render(str(hour) + " * " + str(minute) + " * " + str(second), 1, [255, 0, 0])
	rect2 = text2.get_rect()
	pos2 = [(main.WIDTH - rect2.right) / 2, ((main.HEIGHT - rect2.bottom) / 2) - rect1.bottom]
	
	text3 = main.FONTSMALL.render(str(_hour) + ":" + str(_minute) + ":" + str(_second), 1, [255, 0, 0])
	rect3 = text3.get_rect()
	pos3 = [(main.WIDTH - rect3.right) / 2, ((main.HEIGHT - rect3.bottom) / 2) + rect1.bottom]
	
	main.SURF.blit(text1, pos1)
	main.SURF.blit(text2, pos2)
	main.SURF.blit(text3, pos3)
	
	if _faketime and composite != main.LASTCOMPOSITE:
		main.LASTCOMPOSITE = composite
		savesurf()

	# blit and flip
	main.SCREEN.blit(pygame.transform.scale(main.SURF, main.SCALESIZE), [0, 0])
	pygame.display.flip()

""" INIT """
# initialize program
def init():
	main.WIDTH, main.HEIGHT = 1080, 720
	main.SIZE = [main.WIDTH, main.HEIGHT]
	main.SURF = pygame.Surface(main.SIZE)

	main.SCALEWIDTH, main.SCALEHEIGHT = 1080, 720
	main.SCALESIZE = [main.SCALEWIDTH, main.SCALEHEIGHT]
	main.SCREEN = pygame.display.set_mode(main.SCALESIZE)
	
	main.CAPTION = "JClock VII"
	main.SAVEPATH = os.getcwd() + "/gif/"

	main.TICKS = 0
	main.KEYSDOWN = []
	
	main.PRIMES = getprimes(60+60+24)
	main.FONTBIG = pygame.font.SysFont(None, 100)
	main.FONTSMALL = pygame.font.SysFont(None, 20)
	main.LASTCOMPOSITE = 0

	# printput the 144 primes
	"""s = "\n"
	for _ in range(0, 60):
		s += str(main.PRIMES[_]) + "; "
	print s
	
	s = "\n"
	for _ in range(0, 60):
		s += str(main.PRIMES[_+60]) + "; "
	print s
	
	s = "\n"
	for _ in range(0, 24):
		s += str(main.PRIMES[_+60+60]) + "; "
	print s
	print
	#"""

	# only used for creating the frames for the animated gif
	#savegif()

	# 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