Star

I had an idea to make a simulation which generates stars. Using my ‘getCirclePos()’ function, two radii – an inner and an outer one – and a number of points I realized the idea in this program.

How it works

$\frac{360^\circ}{\text{number of points}} = \text{circle sector}$
$\frac{360^\circ}{\text{number of points} \cdot 2} = \text{shifted circle sector}$

Using those to sectors – and alternating between the inner (shifted) and outer radius – the program connects the points and out comes a star shape.

How to use

• ‘d’ toggles debug mode (gray circles)
• ‘f’ toggles points being drawn (blue points at the corners)

# Python 2.7.7 Code
# Pygame 1.9.1 (for Python 2.7.7)
# Jonathan Frech 24th of April, 2015

# gets the scaled mouse position
def getMousePos():
p = pygame.mouse.get_pos()
return [
p[0] / main.SCALE, p[1] / main.SCALE
]

# gets the distance between two points
def getDistance(p1, p2):
return int(
math.sqrt((p2[0] - p1[0])**2 + (p2[1] - p1[1])**2)
)

# validates color integer
# extra feature: _min and _max implementation
def colorValid(_color, _min = 0, _max = 255):
newColor = math.fabs(_color)
n = _max - _min
if newColor > n:
if int(newColor / n) % 2 == 0:
newColor = newColor % n
else:
newColor = n - (newColor % n)

return int(newColor) + _min

# gets the position on a circle
# circle center                           : '_pos'
# angle from center to point on the circle: '_angle'
return [
]

# swaps a boolean
def boolSwap(_bool):
if _bool:
return False
else:
return True

class star():
def __init__(self, _center = None):
self.center = _center
if self.center == None:
self.center = main.CENTER
self.points = 5
self.color = [255, 255, 255]

#self.pointsDraw = False
self.pointsDraw = True
self.pointsColor = [0, 0, 255]
#self.debug = True
self.debug = False

self.debugColor = [50, 50, 50]

def render(self, _surface):
if self.debug:

last = None
for _ in range(0, self.points + 1):
p2 = getCirclePos(self.center, self.rad2, self.addAngle + 360. / self.points * _ + (360. / (self.points * 2)))
if self.pointsDraw:
pygame.draw.circle(_surface, self.pointsColor, p1, 5, 0)
pygame.draw.circle(_surface, self.pointsColor, p2, 5, 0)
if last != None:
pygame.draw.line(_surface, self.color, last, p1)
pygame.draw.line(_surface, self.color, p1, p2)
last = p2

def setRotation(self, _angle):

def rotate(self, _angle):

def rotatePos(self, _center, _distance, _angle):
self.center = getCirclePos(_center, _distance, _angle)

# tick function
def tick():
# handle events
for event in pygame.event.get():
# quit
if event.type == pygame.QUIT:
sys.exit()

# keyup
if event.type == pygame.KEYUP:
# handle 'main.KEYSDOWN'
if event.key in main.KEYSDOWN:
main.KEYSDOWN.remove(event.key)

# keydown
if event.type == pygame.KEYDOWN:
# handle 'main.KEYSDOWN'
if event.key not in main.KEYSDOWN:
main.KEYSDOWN.append(event.key)

# turn on and off debug mode
if event.key == pygame.K_d:
main.STAR1.debug = boolSwap(main.STAR1.debug)
main.STAR2.debug = boolSwap(main.STAR2.debug)
main.STAR3.debug = boolSwap(main.STAR3.debug)

# turn on and off point draw mode
if event.key == pygame.K_f:
main.STAR1.pointsDraw = boolSwap(main.STAR1.pointsDraw)
main.STAR2.pointsDraw = boolSwap(main.STAR2.pointsDraw)
main.STAR3.pointsDraw = boolSwap(main.STAR3.pointsDraw)

# tick stars
main.STAR1.setRotation(main.TICKS/1)

main.STAR2.setRotation(main.TICKS/2)
# interesting pattern

main.STAR3.setRotation(main.TICKS*2)
main.STAR3.rotatePos([150, main.HEIGHT - 150], 200, main.TICKS)

# render function
def render():
# fill
main.SCREEN.fill(main.BACKGROUNDCOLOR)
if main.FILL:
main.SURF.fill(main.COLOR)

# render stars
main.STAR1.render(main.SURF)
main.STAR2.render(main.SURF)
main.STAR3.render(main.SURF)

# blit and flip
main.SCREEN.blit(pygame.transform.scale(main.SURF, [int(main.WIDTH * main.SCALE), int(main.HEIGHT * main.SCALE)]), [0, 0])
pygame.display.flip()

# run function (uses tick() and render())
def run():
ticksPerSecond = 60
lastTime = time.time() * 1000000000
nsPerTick =  1000000000.0 / float(ticksPerSecond)

ticks = 0
frames = 0

lastTimer = time.time() * 1000
delta = 0.0

while True:
now = time.time() * 1000000000
delta += float(now - lastTime) / float(nsPerTick)
lastTime = now
shouldRender = False

while delta >= 1:
ticks += 1
main.TICKS += 1
tick()
delta -= 1
shouldRender = True

if shouldRender:
frames += 1
render()

if time.time() * 1000 - lastTimer >= 1000:
lastTimer += 1000

# debug
#print("Frames: " + str(frames) + ", ticks: " + str(ticks))

frames = 0
ticks = 0

# dummy class for global variables
class dummy():
pass

# importing needed modules
import pygame, sys, time, math, os, random

# main dummy
main = dummy()
main.TICKS = 0

main.RES = 1
main.WIDTH = 1080 / main.RES
main.HEIGHT = 720 / main.RES
main.SIZE = [main.WIDTH, main.HEIGHT]
main.CENTER = [main.WIDTH / 2, main.HEIGHT / 2]
main.CENTERFLOAT = [main.WIDTH / 2.0, main.HEIGHT / 2.0]

main.SCALE = 1 * main.RES
main.SCALEDSIZE = [int(main.WIDTH * main.SCALE), int(main.HEIGHT * main.SCALE)]
main.SCALEDSIZEFLOAT = [main.WIDTH * main.SCALE, main.HEIGHT * main.SCALE]

main.SURF = pygame.Surface(main.SIZE)

main.SCREEN = pygame.display.set_mode(main.SCALEDSIZE)
pygame.display.set_caption("Star")

main.BACKGROUNDCOLOR = [0, 0, 0]
main.COLOR = [0, 0, 0]
main.FILL = True

main.KEYSDOWN = []

main.STAR1 = star([main.WIDTH / 7*3, main.HEIGHT / 5])
main.STAR2 = star([main.WIDTH / 3*2, main.HEIGHT / 3*2])
main.STAR3 = star()

# start program
run()