Plant Mimic

I was trying to mimic plant root-growth behavior. It all starts with one root, which then has a chance to get offspring (0 – 4). If it gets offspring, the offspring also gets a chance for more offspring. To reduce lag there is a maximum to the currently growing roots (100). Due to the random behavior, it is possible for the plant to die at the start or – theoretically – in an advanced state.
Plant Mimic 2The Third ExamplePlant Mimic 7


# Python 2.7.7 Code
# Pygame 1.9.1 (for Python 2.7.7)
# Jonathan Frech 11th of April, 2015

# 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):
	return [
				int(_pos[0] + _radius * math.cos(math.radians(_angle))),
				int(_pos[1] + _radius * math.sin(math.radians(_angle)))
			]

# 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

# entity class
class entity():
	# init
	def __init__(self, _pos, _distance, _angle):
		self.pos = _pos
		self.distance = _distance
		self.angle = _angle
		self.DEAD = False
		
		self.color = [255, 255, 255]
		#self.color = [random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)]
		
		self.destination = getCirclePos(self.pos, self.distance, self.angle)
	
	# tick function
	def tick(self):
		# calculate distance to destination (for smooth movement)
		dist = [
			self.destination[0] - self.pos[0],
			self.destination[1] - self.pos[1]
		]
		
		# move to destination (/ 5.0 defines the speed)
		self.pos[0] += dist[0] / 20.0
		self.pos[1] += dist[1] / 20.0
		
		# if destination is reached, die and create offspring
		if dist[0] < .1 and dist[1]  100:
		main.ENTITIES.pop(random.randint(0, len(main.ENTITIES) - 1))

# render function
def render():
	# fill
	main.SCREEN.fill([0, 0, 0])
	
	# render entities
	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

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

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

main.resolution = 1
main.WIDTH = 1080 / main.resolution
main.HEIGHT = 720 / main.resolution
main.SCALE = 1 * main.resolution

main.SIZE = [main.WIDTH, main.HEIGHT]
main.SCALEDSIZE = [int(main.WIDTH * main.SCALE), int(main.HEIGHT * main.SCALE)]

main.SURF = pygame.Surface(main.SIZE)
main.SURF.fill([0, 0, 0])

main.SCREEN = pygame.display.set_mode(main.SCALEDSIZE)
pygame.display.set_caption("Plant Mimic")

# entity start
main.ENTITIES = [
	entity([main.WIDTH / 2, 0], random.randint(10, 20), random.randint(181, 359))
	]

# 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