Advertisement
billysback

PyGame shape render thing

Jan 16th, 2014
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.56 KB | None | 0 0
  1. import pygame, math, random
  2. from Tester import Test
  3.  
  4. class Shape: #can't be a line.
  5.     def __init__(self, points, circle): #if a circle, second point is radius
  6.         self.points = points
  7.         self.circle = circle
  8.         if len(points) > 2:
  9.             centre = [ 0, 0 ]
  10.             for i in range(len(points)):
  11.                 p = points[i]
  12.                 centre[0] = centre[0] + p[0]
  13.                 centre[1] = centre[1] + p[1]
  14.             centre[0] = centre[0]/len(points)
  15.             centre[1] = centre[1]/len(points)
  16.             self.centre = centre
  17.         else:
  18.             self.centre = [points[0][0], points[0][1]]
  19.  
  20.     def rotate(self, angle):
  21.         if self.circle == False:
  22.             angle = math.radians(angle)
  23.             c = self.centre
  24.             nPoints = []
  25.             for i in range(len(self.points)):
  26.                 p = self.points[i]
  27.                 tp = [p[0]-c[0], p[1]-c[1]]
  28.                 np = [ (math.cos(angle)*tp[0]) - (math.sin(angle)*tp[1]) + c[0],
  29.                        (math.sin(angle)*tp[0]) + (math.cos(angle)*tp[1]) + c[1] ]
  30.                 nPoints.append(np)
  31.             oPoints = self.points
  32.             self.points = nPoints
  33.             return [oPoints, nPoints]
  34.  
  35.     def getRect(self):
  36.         points = self.points
  37.         minX = points[0][0]
  38.         minY = points[0][1]
  39.         maxX = points[0][0]
  40.         maxY = points[0][1]
  41.         if len(self.points) > 2:
  42.             for i in range(len(points)):
  43.                 p = self.points[i]
  44.                 if p[0] < minX:
  45.                     minX = p[0]
  46.                 if p[1] < minY:
  47.                     minY = p[1]
  48.                 if p[0] > maxX:
  49.                     maxX = p[0]
  50.                 if p[1] > maxY:
  51.                     paxY = p[1]
  52.         else:
  53.             p = self.points[0]
  54.             r = self.points[1]
  55.             minX = p[0]-r
  56.             minY = p[1]-r
  57.             maxX = p[0]+r
  58.             maxY = p[1]+r
  59.         return [ [minX, minY], [maxX, maxY], [maxX-minX, maxY-minY] ]
  60.            
  61.  
  62.     def draw(self, screen, colour, fill):
  63.         if self.circle == False:
  64.             pygame.draw.polygon(screen, colour, self.points, fill)
  65.         else:
  66.             if len(self.points) == 4:
  67.                 test = 1
  68.                 # NOT WORKING: pygame.draw.ellipse(screen, colour, Rect(*self.points), fill)
  69.             elif len(self.points) == 2:
  70.                 pygame.draw.circle(screen, colour, self.points[0], self.points[1], fill)
  71.             else:
  72.                 raise Exception("Invalid shape!")
  73.  
  74.        
  75. def getTexturedShape(screen, shape, texture):
  76.     srect = shape.getRect()
  77.     surf = Surface((srect[2][0], srect[2][1]), pygame.SRCALPHA, 32)
  78.     surf.convert_alpha()
  79.     surf.fill([0, 0, 0, 0])
  80.     shape.draw(surf, [255, 255, 255, 100], 0)
  81.     for x in range(srect[2][0]):
  82.         for y in range(srect[2][1]):
  83.             col = surf.get_at((x, y))
  84.             if col[0] == 255 and col[1] == 255 and col[2] == 255:
  85.                 surf.set_at((x, y), texture.getColour(x, y))
  86.     return surf
  87.                
  88.    
  89.    
  90. def testFile():
  91.     test = Test()
  92.     for a in range(180):
  93.         ticks = pygame.time.get_ticks()
  94.         i = 0
  95.         while pygame.time.get_ticks()-ticks < 1000:
  96.             shape = Shape( [ [100, 100], [130, 130], [110, 160], [90, 150], [80, 120] ], False )
  97.             shape.rotate(a)
  98.             test.screen.fill([0,0,0])
  99.             shape.draw(test.screen, [255, 0, 0], 0)
  100.             pygame.display.flip()
  101.             i = i + 1
  102.         print(i/100, "FPS for ", a)
  103.    
  104.        
  105.  
  106. testFile()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement