Langton’s Ant

This program is a simulation of Langton’s Ant. The following image shows the ant on its highway.

Controls

  • F1 takes a screenshot
  • Space stops and resumes the ant’s movement

The highway is infinite...


# Python 2.7.7 Code
# Pygame 1.9.1 (for Python 2.7.7)
# Jonathan Frech 27th of October, 2015
#         edited 29th of October, 2015

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

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

""" FUNCTIONS """
# validates a color integer
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 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))
			]

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

# basic vector functions
def vecConvert(p1, p2):
	return [p2[0] - p1[0], p2[1] - p1[1]]
def vecLen(vec):
	return math.sqrt( (vec[0]**2) + (vec[1]**2) )
def vecMultiply(vec, n):
	return [vec[0] * n, vec[1] * n]
def vecGetPoint(vec, point):
	return [point[0] + vec[0], point[1] + vec[1]]
def vecAdd(vec1, vec2):
	return [vec1[0] + vec2[0], vec1[1] + vec2[1]]

# 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.SCREENSIZE) , 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.KEYDOWN:
			if event.key == pygame.K_SPACE:
				main.RUNNING = not main.RUNNING

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

	for _ in range(0, 10):
	#if main.TICKS % 60 == 0:
		if not main.FINISHED and main.RUNNING:
			if main.ANTSURF.get_at(main.ANTPOS) == (0, 0, 0, 255):
				main.ANTDIR = (main.ANTDIR+1) % 4
				main.ANTSURF.set_at(main.ANTPOS, [255, 255, 255])
			else:
				main.ANTDIR = (main.ANTDIR-1) % 4
				main.ANTSURF.set_at(main.ANTPOS, [0, 0, 0])

			if main.ANTDIR == 0:
				main.ANTPOS[1] += 1
			elif main.ANTDIR == 1:
				main.ANTPOS[0] -= 1
			elif main.ANTDIR == 2:
				main.ANTPOS[1] -= 1
			elif main.ANTDIR == 3:
				main.ANTPOS[0] += 1

			main.STEPS += 1
			pygame.display.set_caption(main.CAPTION + " (" + str(main.STEPS) + " steps)")

			if main.ANTPOS[0] < 0 or main.ANTPOS[0] >= main.WIDTH or main.ANTPOS[1] < 0 or main.ANTPOS[1] >= main.HEIGHT:
				main.FINISHED = True

# render function
def render():
	# blit
	main.SURF.blit(main.ANTSURF, [0, 0])
	main.SURF.set_at(main.ANTPOS, [255, 0, 0])

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

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

	main.SCREENSIZE = [1080, 720]
	main.SCREEN = pygame.display.set_mode(main.SCREENSIZE)
	
	main.CAPTION = "Langton's Ant"
	main.TICKS = 0
	main.SAVEPATH = os.getcwd() + "/out/"

	main.ANTSURF = pygame.Surface(main.SIZE)
	main.ANTPOS = [main.WIDTH/2, main.HEIGHT/2]
	main.ANTDIR = 0
	main.FINISHED = False
	main.RUNNING = True
	main.STEPS = 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