Created
December 3, 2011 18:38
-
-
Save osa1/1427789 to your computer and use it in GitHub Desktop.
Generate randomly shaped polygons(namely, astroids)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import math | |
import random | |
import pygame | |
class Astroid: | |
def __init__(self, points=5, radius=50, | |
radius_range=10, angle_range=20): | |
self.points = points | |
self.radius = radius | |
self.angles = [a * 360/points + random.randint(-angle_range, angle_range) | |
for a in xrange(points)] | |
self.p_distances_from_origin = \ | |
[random.randint(radius-radius_range, radius+radius_range) | |
for p in xrange(points)] | |
def point_list(self, center=(0, 0)): | |
return [(center[0]+p*math.cos(math.radians(d)), | |
center[1]+p*math.sin(math.radians(d))) | |
for (p, d) in zip(self.p_distances_from_origin, self.angles)] | |
def rotate(self, angle): | |
for i in xrange(len(self.angles)): | |
self.angles[i] += angle | |
self.angles[i] %= 360 | |
if __name__ == "__main__": | |
ticks = 0 | |
running = True | |
screen = pygame.display.set_mode((300, 300), 0, 32) | |
time = pygame.time.Clock() | |
a = Astroid(points=10, radius_range=50) | |
import pprint | |
print "angles", a.angles | |
print "p_distances", a.p_distances_from_origin | |
pprint.pprint("point_list") | |
pprint.pprint(a.point_list((100, 100))) | |
while running: | |
for event in pygame.event.get(): | |
if event.type == pygame.QUIT: | |
running = False | |
elif event.type == pygame.MOUSEBUTTONDOWN: | |
a = Astroid(points=10, radius_range=50) | |
screen.fill((0, 0, 0)) | |
pygame.draw.polygon(screen, (255, 255, 255), a.point_list((100, 100)), 1) | |
a.rotate(1) | |
pygame.display.flip() | |
ticks = time.tick(40) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment