Pygame Bug

Tinkering around with circles and colors, I created a program which creates interesting images, both with pygame.draw.circle() (left) and pygame.gfxdraw.circle() (right). (Both images at 400×400 pixels)

pygame.draw.circle() pygame.gfxdraw.circle()
To get a smoother and nicer result, I tried using the pygame.gfxdraw.aacircle() function, which draws antialiased circles.

pygame.gfxdraw.aacircle()
As expected, the antialiased function gets rid of the black spots between the colored circles.

Wanting a higher resolution, I used the same code on a 1000×1000 pixel wide surface.

img4 (1000x1000;aagfx)
Again, it is a smooth and neat result. But as I got higher and higher resolutions, I got this interesting result. (At 3000×3000 pixels)

img7 (3000x3000;aagfx)
To make sure, the problem is on pygame.gfxdraw.aacircle()’s side, I created images of equal size with both pygame.draw.circle() (left) and pygame.gfxdraw.circle() (right). (Both images again at 3000×3000 pixels)

To ensure, the problem is not on my Operating System’s side, I used the exact same code on Mac OSX, Debian Linux and Windows 7 and 8.1 and got the same result each time.

img9 (3000x3000;draw) pygame.gfxdraw.circle()
At last, I used all three functions at 4000×4000 pixels. (pygame.draw.circle() on the left, pygame.gfxdraw.cirlce() on the right and pygame.gfxdraw.aacircle() on the bottom)

pygame.draw.circle() pygame.gfxdraw.circle() pygame.gfxdraw.aacircle()


# Python 2.7.7 Code
# Pygame 1.9.1 (for Python 2.7.7)
# Jonathan Frech 19th of August,    2015
#         edited 20th of August,    2015
#         edited 21st of August,    2015
#         edited  5th of September, 2015
#         edited 26th of September, 2015

# set screen's dimensions
WIDTH, HEIGHT = 400, 400

# set draw type
DRAW = ["aagfx", "gfx", "draw"][0]

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

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

""" FUNCTIONS """
# 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
# quits the program
def quit():
	sys.exit()

""" TICK; RENDER """
# tick function
def tick():
	# handle events
	for event in pygame.event.get():
		# quit
		if event.type == pygame.QUIT:
			quit()
		
		# save screenshot
		if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
			try:
				# create path
				path = os.getcwd() + "/img"
				if not os.path.exists(os.getcwd() + "/img"):
					os.mkdir(path)
				n = len(os.listdir(path))
				
				# save current surface
				pygame.image.save(main.SURF, os.getcwd() + "/img/img" + str(n) + " (" + str(main.WIDTH) + "x" + str(main.HEIGHT) + ";" + main.DRAW + ").png")
			except:
				pass

# render function
def render():
	# draw multiple circles
	for _ in range(0, 50):
		# increase circle's radius
		# (will not stop, even if the radius is bigger than the screen)
		main.N += .1
		
		# calculate color
		c = [
			colorValid( main.N * 1 ),
			colorValid( main.N * 1.1 ),
			colorValid( main.N * 1.2 )
		]
		
		if main.DRAW == "aagfx":
			# draw antialiased gfx circle
			pygame.gfxdraw.aacircle(main.SURF, main.WIDTH / 2, main.HEIGHT / 2, int(main.N), c)
		elif main.DRAW == "gfx":
			# draw gfx circle
			pygame.gfxdraw.circle(main.SURF, main.WIDTH / 2, main.HEIGHT / 2, int(main.N), c)
		elif main.DRAW == "draw":
			# draw circle
			if int(main.N) >= 1:
				pygame.draw.circle(main.SURF, c, [main.WIDTH / 2, main.HEIGHT / 2], int(main.N), 1)
	
	# blit and flip
	main.SCREEN.blit(main.SURF, [0, 0])
	pygame.display.flip()

""" INIT """
# initialize program
def init():
	# variables
	main.WIDTH, main.HEIGHT = WIDTH, HEIGHT
	main.DRAW = DRAW
	
	main.SIZE = [main.WIDTH, main.HEIGHT]
	main.SCREEN = pygame.display.set_mode(main.SIZE)
	main.SURF = pygame.Surface(main.SIZE)
	
	main.CAPTION = "Circle Drawer (" + str(main.WIDTH) + "x" + str(main.HEIGHT) + ")"
	main.TICKS = 0
	main.KEYSDOWN = []
	
	main.N = 0
	
	# functions
	pygame.display.set_caption(main.CAPTION)

""" RUN """
# run function (uses tick() and render())
def run():
	# variables
	ticksPerSecond = 60
	lastTime = time.time() * 1000000000
	nsPerTick =  1000000000.0 / float(ticksPerSecond)
	
	ticks = 0
	frames = 0
	
	lastTimer = time.time() * 1000
	delta = 0.0
	
	# main loop
	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 (disabled)
			#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