Text Spinner

This program lets you input text and spins it around the center!

Controls

  • Text input (alphabet, space and punctuation) inputs text
  • F1 takes a screenshot

Hello world.JBlog!a**2 + b**2 = c**2

 


# Python 2.7.7 Code
# Pygame 1.9.1 (for Python 2.7.7)
# Jonathan Frech 17th of November, 2015
#         edited 20th 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 """
# gets the position on a circle with a position, radius and an angle
def getCirclePos(_pos, _radius, _angle):
	return [
				_pos[0] + _radius * math.cos(math.radians(_angle)),
				_pos[1] + _radius * math.sin(math.radians(_angle))
			]

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

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

# 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()
		
		if event.type == pygame.KEYUP:
			if event.key in main.KEYSDOWN:
				main.KEYSDOWN.remove(event.key)
		
		if event.type == pygame.KEYDOWN:
			if event.key not in main.KEYSDOWN:
				main.KEYSDOWN.append(event.key)

			if event.key == pygame.K_F1:
				savesurf()

			if event.key == pygame.K_BACKSPACE:
				if len(main.STR) > 0:
					main.STR = main.STR[0:-1]

			if event.unicode.lower() in "abcdefghijklmnopqrstuvwxyz" + " .,!?'()*/+-=" + "0123456789":
				main.STR += event.unicode

	main.ANGLE += 1

# render function
def render():
	# fill
	main.SURF.fill([0, 0, 0])
	
	s = main.STR
	if (main.TICKS/30) % 2 == 0:
		s += "|"
	else:
		s += " "
	s += "   "

	for r in range(0, 5):
		radius = (50*r)
		color = [255. / 5 * r, 0, 0]
		for _ in range(0, len(s)):
			txt = main.FONT.render(s[_], 1, color)
			rct = txt.get_rect()
			pos = getCirclePos(main.CENTER, radius, 360. / len(s) * _ + main.ANGLE)
			pos = [pos[0] - rct.right/2, pos[1] - rct.bottom/2]
			main.SURF.blit(txt, pos)

	# 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 = "Text Spinner"
	main.SAVEPATH = os.getcwd() + "/out/"

	main.TICKS = 0
	main.KEYSDOWN = []

	main.STR = "Hello world."
	main.FONT = pygame.font.SysFont(None, 50)
	main.CENTER = [main.WIDTH / 2, main.HEIGHT / 2]
	main.ANGLE = 0
	
	# 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