Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame, math, random
- from Tester import Test
- class Shape: #can't be a line.
- def __init__(self, points, circle): #if a circle, second point is radius
- self.points = points
- self.circle = circle
- if len(points) > 2:
- centre = [ 0, 0 ]
- for i in range(len(points)):
- p = points[i]
- centre[0] = centre[0] + p[0]
- centre[1] = centre[1] + p[1]
- centre[0] = centre[0]/len(points)
- centre[1] = centre[1]/len(points)
- self.centre = centre
- else:
- self.centre = [points[0][0], points[0][1]]
- def rotate(self, angle):
- if self.circle == False:
- angle = math.radians(angle)
- c = self.centre
- nPoints = []
- for i in range(len(self.points)):
- p = self.points[i]
- tp = [p[0]-c[0], p[1]-c[1]]
- np = [ (math.cos(angle)*tp[0]) - (math.sin(angle)*tp[1]) + c[0],
- (math.sin(angle)*tp[0]) + (math.cos(angle)*tp[1]) + c[1] ]
- nPoints.append(np)
- oPoints = self.points
- self.points = nPoints
- return [oPoints, nPoints]
- def getRect(self):
- points = self.points
- minX = points[0][0]
- minY = points[0][1]
- maxX = points[0][0]
- maxY = points[0][1]
- if len(self.points) > 2:
- for i in range(len(points)):
- p = self.points[i]
- if p[0] < minX:
- minX = p[0]
- if p[1] < minY:
- minY = p[1]
- if p[0] > maxX:
- maxX = p[0]
- if p[1] > maxY:
- paxY = p[1]
- else:
- p = self.points[0]
- r = self.points[1]
- minX = p[0]-r
- minY = p[1]-r
- maxX = p[0]+r
- maxY = p[1]+r
- return [ [minX, minY], [maxX, maxY], [maxX-minX, maxY-minY] ]
- def draw(self, screen, colour, fill):
- if self.circle == False:
- pygame.draw.polygon(screen, colour, self.points, fill)
- else:
- if len(self.points) == 4:
- test = 1
- # NOT WORKING: pygame.draw.ellipse(screen, colour, Rect(*self.points), fill)
- elif len(self.points) == 2:
- pygame.draw.circle(screen, colour, self.points[0], self.points[1], fill)
- else:
- raise Exception("Invalid shape!")
- def getTexturedShape(screen, shape, texture):
- srect = shape.getRect()
- surf = Surface((srect[2][0], srect[2][1]), pygame.SRCALPHA, 32)
- surf.convert_alpha()
- surf.fill([0, 0, 0, 0])
- shape.draw(surf, [255, 255, 255, 100], 0)
- for x in range(srect[2][0]):
- for y in range(srect[2][1]):
- col = surf.get_at((x, y))
- if col[0] == 255 and col[1] == 255 and col[2] == 255:
- surf.set_at((x, y), texture.getColour(x, y))
- return surf
- def testFile():
- test = Test()
- for a in range(180):
- ticks = pygame.time.get_ticks()
- i = 0
- while pygame.time.get_ticks()-ticks < 1000:
- shape = Shape( [ [100, 100], [130, 130], [110, 160], [90, 150], [80, 120] ], False )
- shape.rotate(a)
- test.screen.fill([0,0,0])
- shape.draw(test.screen, [255, 0, 0], 0)
- pygame.display.flip()
- i = i + 1
- print(i/100, "FPS for ", a)
- testFile()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement