Paste Code
Paste Blends
Paste Images
# Pygame spiral drawing function
# Copyright David Barker 2010


import pygame, sys, math


def DrawSpiral(display, pos, radius, rotation = math.pi, numSpokes = 1, clockwise = True, startAngle = 0.0):
"""DrawSpiral(display, pos, radius, rotation = math.pi, numSpokes = 1, clockwise = True, startAngle = 0): return None
Draws a spiral at the given position.
Arguments:
-display: the pygame.Surface object to draw to
-pos: a tuple for the position of the centre of the spiral
-radius: the radius of the spiral
-rotation: the angle through which a single spoke will have turned by the time it reaches its end
-numSpokes: the number of spokes to draw
-clockwise: a boolean variable specifying which direction the spiral should turn in
-startAngle: the starting angle of the first spoke
"""
resolution = radius / 2.0
radiusIncrement = radius / resolution
rotationIncrement = rotation / resolution
spokeRotationIncrement = (math.pi * 2.0) / float(numSpokes)
spokeRotation = startAngle

if clockwise:
direction = -1
else:
direction = 1

for i in range(0, numSpokes):
spoke = []
spoke.append(pos)

curAngle = spokeRotation
curRadius = 0.0

while curRadius <= radius:
newx = curRadius * math.sin(curAngle)
newy = curRadius * math.cos(curAngle)
spoke.append((pos[0] + newx, pos[1] + (direction * newy)))

curRadius += radiusIncrement
curAngle += rotationIncrement

pygame.draw.aalines(display, (255, 255, 255), False, spoke)

spokeRotation += spokeRotationIncrement


if __name__ == '__main__': # Begin demo code
screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption("Spirals!")

clock = pygame.time.Clock()

curRad = 20.0
curRot = math.pi
startAngle = 0.0

while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()

screen.fill((0, 0, 0))
DrawSpiral(screen, pos = pygame.mouse.get_pos(), radius = curRad, rotation = curRot, numSpokes = 5, clockwise = True, startAngle = startAngle)

curRad += 2.0
curRot += 0.1
startAngle -= 0.05

clock.tick(30)
pygame.display.update()
  1. # Pygame spiral drawing function
  2. # Copyright David Barker 2010
  3.  
  4.  
  5. import pygame, sys, math
  6.  
  7.  
  8. def DrawSpiral(display, pos, radius, rotation = math.pi, numSpokes = 1, clockwise = True, startAngle = 0.0):
  9.     """DrawSpiral(display, pos, radius, rotation = math.pi, numSpokes = 1, clockwise = True, startAngle = 0): return None
  10.    Draws a spiral at the given position.
  11.    Arguments:
  12.        -display: the pygame.Surface object to draw to
  13.        -pos: a tuple for the position of the centre of the spiral
  14.        -radius: the radius of the spiral
  15.        -rotation: the angle through which a single spoke will have turned by the time it reaches its end
  16.        -numSpokes: the number of spokes to draw
  17.        -clockwise: a boolean variable specifying which direction the spiral should turn in
  18.        -startAngle: the starting angle of the first spoke
  19.    """
  20.     resolution = radius / 2.0
  21.     radiusIncrement = radius / resolution
  22.     rotationIncrement = rotation / resolution
  23.     spokeRotationIncrement = (math.pi * 2.0) / float(numSpokes)
  24.     spokeRotation = startAngle
  25.  
  26.     if clockwise:
  27.         direction = -1
  28.     else:
  29.         direction = 1
  30.  
  31.     for i in range(0, numSpokes):
  32.         spoke = []
  33.         spoke.append(pos)
  34.  
  35.         curAngle = spokeRotation
  36.         curRadius = 0.0
  37.  
  38.         while curRadius <= radius:
  39.             newx = curRadius * math.sin(curAngle)
  40.             newy = curRadius * math.cos(curAngle)
  41.             spoke.append((pos[0] + newx, pos[1] + (direction * newy)))
  42.  
  43.             curRadius += radiusIncrement
  44.             curAngle += rotationIncrement
  45.  
  46.         pygame.draw.aalines(display, (255, 255, 255), False, spoke)
  47.        
  48.         spokeRotation += spokeRotationIncrement
  49.  
  50.  
  51. if __name__ == '__main__':  # Begin demo code
  52.     screen = pygame.display.set_mode((800, 600))
  53.     pygame.display.set_caption("Spirals!")
  54.  
  55.     clock = pygame.time.Clock()
  56.  
  57.     curRad = 20.0
  58.     curRot = math.pi
  59.     startAngle = 0.0
  60.  
  61.     while True:
  62.         for event in pygame.event.get():
  63.             if event.type == pygame.QUIT:
  64.                 sys.exit()
  65.  
  66.         screen.fill((0, 0, 0))
  67.         DrawSpiral(screen, pos = pygame.mouse.get_pos(), radius = curRad, rotation = curRot, numSpokes = 5, clockwise = True, startAngle = startAngle)
  68.  
  69.         curRad += 2.0
  70.         curRot += 0.1
  71.         startAngle -= 0.05
  72.        
  73.         clock.tick(30)
  74.         pygame.display.update()
  75.  
go to heaven