Pattern

Using colors and entities, this program can generate some nice-looking pictures. The entities move randomly across the screen, always staying in a grid layout. Their color is defined by an increasing tick variable, which gives them a nice gradient.

Controls

  • Space spawns in new entities
  • ‘m’ pauses and resumes the movement of entities

Only a few entites Diamond shape Really messy


# Python 2.7.7 Code
# Pygame 1.9.1 (for Python 2.7.7)
# Jonathan Frech 21st of May, 2015
#         edited 28th of July, 2015 
#          * commenting
#          * moving entity.tick() into render()
#          * removing unnecessary functions
#         edited 29th of July, 2015
#          * commenting
#          * improvement of code

# 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):
		# variables
		self.pos = _pos
		
		self.color = [0, 0, 0]
		self.lastChanged = 0
		self.direction = random.randint(0, 3)
		
		# if true, entity will be removed
		self.dead = False
	
	# render permanently (body)
	def renderDraw(self, _surface):
		# calculate color
		self.color = [colorValid( main.COLORTICKS * 1 ), colorValid( main.COLORTICKS * 2 ), colorValid( main.COLORTICKS * 3 )]
		
		# draw
		pygame.draw.circle(_surface, self.color, self.pos, 10/2)
	
	# render head
	def render(self, _surface):
		pygame.draw.circle(_surface, self.color, self.pos, 10, 1)
	
	def tick(self):
		# main.MOVING defines if entities move
		if main.MOVING:
			# defines when it is time to turn
			self.lastChanged += 1
			
			# up, down, left, right
			n = 5
			if self.direction == 0:
				self.pos[1] -= n
			elif self.direction == 1:
				self.pos[1] += n
			elif self.direction == 2:
				self.pos[0] -= n
			elif self.direction == 3:
				self.pos[0] += n
			
			# display bounds
			if self.pos[1] < 0:
				self.pos[1] = 0
			if self.pos[1] > main.HEIGHT:
				self.pos[1] = main.HEIGHT
			if self.pos[0] < 0:
				self.pos[0] = 0
			if self.pos[0] > main.WIDTH:
				self.pos[0] = main.WIDTH
			
			# change direction
			if self.lastChanged > 10:
				if random.randint(0, 30) == 0:
					self.lastChanged = 0
					if self.direction in [0, 1]:
						self.direction = random.randint(2, 3)
					elif self.direction in [2, 3]:
						self.direction = random.randint(0, 1)
				
			# die off randomly (removed)
			#if random.randint(0, 200) == 0:
			#	self.dead = True

# tick function
def tick():
	# handle events
	for event in pygame.event.get():
		# quit
		if event.type == pygame.QUIT:
			sys.exit()
		
		# keyup
		if event.type == pygame.KEYUP:
			# handle 'main.KEYSDOWN'
			if event.key in main.KEYSDOWN:
				main.KEYSDOWN.remove(event.key)
		
		# keydown
		if event.type == pygame.KEYDOWN:
			# handle 'main.KEYSDOWN'
			if event.key not in main.KEYSDOWN:
				main.KEYSDOWN.append(event.key)
			
			# toggle movement
			if event.key == pygame.K_m:
				main.MOVING = not main.MOVING
			
	
	# spawn in entities
	if pygame.K_SPACE in main.KEYSDOWN:
		main.ENTITIES.append(entity(main.CENTER[:]))

	# tick entities (removed because of gaps)
	#for _ in main.ENTITIES:
	#	_.tick()
	#	if _.dead:
	#		main.ENTITIES.remove(_)
		

# render function
def render():
	# fill
	main.SCREEN.fill(main.BACKGROUNDCOLOR)
	
	# increase main.COLORTICKS
	if main.MOVING:
		main.COLORTICKS += 1
	
	# render entities (body)
	for _ in main.ENTITIES:
		_.renderDraw(main.SURF)
	
	# blit
	main.SCREEN.blit(pygame.transform.scale(main.SURF, [int(main.WIDTH * main.SCALE), int(main.HEIGHT * main.SCALE)]), [0, 0])
	
	# head rendering (and ticking, so that gaps cannot appear)
	for _ in main.ENTITIES:
		_.tick()
		_.render(main.SCREEN)
		
		# remove dead entities
		if _.dead:
			main.ENTITIES.remove(_)
	
	# flip
	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()

# surf
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.SURF = pygame.Surface(main.SIZE)

# screen
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.SCREEN = pygame.display.set_mode(main.SCALEDSIZE)

# colors
main.BACKGROUNDCOLOR = [0, 0, 0]
main.COLOR = [255, 255, 255]

# other
main.KEYSDOWN = []
main.ENTITIES = []
main.MOVING = True

# ticks
main.TICKS = 0
main.COLORTICKS = 0

# functions
main.SURF.fill(main.COLOR)
pygame.display.set_caption("Pattern")

# start program
run()
Advertisements

One thought on “Pattern

  1. Pingback: Second Anniversary – J-Blog

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