Colors VI

This sixth version of my ‘Colors’ program has only a few improvements. At first it uses a different method to change the individual pixels. Instead of using ‘pygame.PixelArray()’ it calls on the surface ‘.set_at()’. Also you get an indicator if it has finished drawing (to be seen in the caption). At last it always – not needing a user input – saves the drawing if it is finished.
To get more information on my ‘Colors’ program, see ‘Colors V’.

Image 1 Image 2 Image 3


# Python 2.7.7 Code
# Pygame 1.9.1 (for Python 2.7.7)
# Jonathan Frech 23rd of March, 2015
#         edited  2nd of April, 2015
#         edited 20th of May  , 2015

Here goes the ‘getColor()’ function.

Image 1:

# creates pixel
def getColor(x, y):
	try:
		color = (
			colorValid( y*1 ),
			colorValid( y*2 ),
			colorValid( y*3 )
		)
		if random.randint(0, 1) == 0:
			color = (0, 0, 0)
	except:
		color = (0, 0, 0)
	
	return color

Image 2:

# creates pixel
def getColor(x, y):
	try:
		color = (
			colorValid( y*x ),
			colorValid( y ),
			colorValid( x )
		)
		if random.randint(0, 1) == 0:
			color = (0, 0, 0)
	except:
		color = (0, 0, 0)
	
	return color

Image 3:

# creates pixel
def getColor(x, y):
	try:
		color = (
			colorValid( y/x ),
			colorValid( x/y ),
			colorValid( 0 )
		)
	except:
		color = (0, 0, 0)
	
	return color

Further 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

# set the caption
def setCaption():
	if main.DRAW:
		pygame.display.set_caption("Colors VI (...)")
	else:
		pygame.display.set_caption("Colors VI")

# save the drawn image
def save():
	if not os.path.exists(os.getcwd() + "/screenshots/"):
		os.mkdir(os.getcwd() + "/screenshots")
	n = 1
	while os.path.exists(os.getcwd() + "/screenshots/" + str(n) + ".png"):
		n += 1
	pygame.image.save(main.SCREEN, os.getcwd() + "/screenshots/" + str(n) + ".png")

# tick function
def tick():
	
	# handle events
	for event in pygame.event.get():
		if event.type == pygame.QUIT:
				sys.exit()
		
		if event.type == pygame.KEYDOWN:
			
			# save current display
			if event.key == keys.screenshot:
				save()
			
			# reload image
			if event.key == keys.reload:
				main.SURF.fill([0, 0, 0])
				main.DRAWTICKS = 0
				main.DRAW = True
				setCaption()
	
	# draw
	if main.DRAW:
		# 'main.DRAWTICKS' defines the pixel column
		main.DRAWTICKS += 1
		if main.DRAWTICKS > main.WIDTH:
			main.DRAW = False
			setCaption()
			save()
		
		# change the the surface (line by line for performance)
		for Y in range(0, main.HEIGHT):
			X = (main.DRAWTICKS - 1) % main.WIDTH
		
			# scaling
			x = 255. / main.WIDTH  * X
			y = 255. / main.HEIGHT * Y
			
			# set pixel
			pos = [X, Y]
			color = getColor(x, y)
			main.SURF.set_at(pos, color)

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

main.WIDTH  = int( 1080 / main.RESOLUTION )
main.HEIGHT = int( 720  / 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.SCREEN = pygame.display.set_mode(main.SCALEDSIZE)

main.DRAWTICKS = 0
main.DRAW = True
setCaption()

# key dummy
keys = dummy()
keys.screenshot = pygame.K_s
keys.reload = pygame.K_SPACE

# 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