Paste: PygameLine Drawing

Author: Daniel Gabriele
Mode: python
Date: Mon, 8 Jun 2009 20:51:05
Plain Text |
#!/usr/bin/env python
# thetalines.py

import pygame

# Set up
pygame.init()
colors = pygame.color.THECOLORS
#
#
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~!
class AngledLine (pygame.sprite.Sprite):
	
	def __init__ (self, scrsize):
		self.bcolor = colors['white']
		self.lcolor = colors['gray']
		self._init_image(scrsize)
	
	def _init_image (self, scrsize):
		self.image = pygame.Surface((scrsize[0]*1.5, scrsize[1]))
		self.rect = self.image.get_rect()
		self.rect.center = tuple([dim/2 for dim in scrsize])
		self.image.fill(self.bcolor)
		self.image.set_colorkey(self.bcolor)
		
	def _get_points (self, dy): 
		a = (self.rect.midleft[0], self.rect.midleft[1] - dy)
		b = (self.rect.midright[0], self.rect.midright[1] + dy)
		c = (self.rect.midleft[0] - 1, self.rect.midleft[1] - dy - 1)
		d = (self.rect.midright[0] - 1, self.rect.midright[1] - 1 + dy)
		return [a, b, c, d]
	
	def update (self, dy):	
		self.image.fill(self.bcolor)
		
		points = self._get_points(dy)
		pygame.draw.aalines(self.image, self.lcolor, 0, points, 1)


#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~!
class LineAssembly (object):
	
	def __init__ (self, scrsize):
		self.topline = AngledLine(scrsize)
		self.botline = AngledLine(scrsize)
		self.dy = 95.0
		
	def update (self):
		self.topline.update(self.dy)
		self.botline.update(-self.dy)
	
	def draw (self, surface):
		shiftedx = -100 #- surface.get_size()[0] / 2
		normaly = self.topline.rect.topleft[1]
		surface.blit(self.topline.image, (shiftedx, normaly))
		surface.blit(self.botline.image, (shiftedx, normaly))

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~!
class UnitTest (object):
	
	
	def __init__ (self):
		
		# display elements
		self.scrsize = (800, 600)
		self.screen = pygame.display.set_mode(self.scrsize)
		self.scrrect = self.screen.get_rect()
		self.clock = pygame.time.Clock()
		
		# visual objects
		self.bg = pygame.Surface(self.scrsize)
		self.lines = LineAssembly(self.scrsize)
		self.eye = pygame.image.load("eyeball.jpg").convert()
		self._configure_visuals()
		
		# runtime flags
		self.running = False
		self.fps = 36
	
	
	def _configure_visuals (self):
		
		# background:
		self.bg.fill(colors['white'])
		self.screen.blit(self.bg, (0, 0))
		self.bg.set_alpha(80)
		
		# eye ball image:
		self.eye.set_alpha(170)
		self.eye.set_colorkey((255, 255, 255))
		
			
	def go (self):
		self.running = True
		
		while self.running:
			self.clock.tick(self.fps)
			
			for e in pygame.event.get():
				kpress = (e.type == pygame.KEYDOWN)
				
				if e.type == pygame.MOUSEMOTION and e.buttons == (1, 0, 0):
					self.lines.dy -= e.rel[0]
				
				if hasattr(e, 'key'):
					if e.key == pygame.K_q:
						self.running = False

			
			self.screen.blit(self.bg, (0, 0))
			self.lines.update()
			self.lines.draw(self.screen)
			self.screen.blit(self.eye, (170, self.scrrect.centery - 85))
					
			pygame.display.flip()
			
			
		
		
		
# Unit Test /////////////!
if __name__ == '__main__':
	test = UnitTest()
	test.go()
		

New Annotation

Summary:
Author:
Mode:
Body: