Warning - This site is moving to https://getthecodingbug.anvil.app
Topics covered
​ Creating Snowflakes
Creating Snowflakes
Copy and paste this code into a new python file in your editor, save it as 'snowflakePygameGenerator.py',
then press F5 to run it.
# snowflakePygameGenerator.py
import pygame from pygame.locals import * import sys, os import time import math def xgetMirrorAngle(angle): if angle >= 0 and angle < 30: mirror = 360-angle elif angle >= 30 and angle < 60: mirror = 120-angle elif angle >= 60 and angle < 90: mirror = 120-angle elif angle >= 90 and angle < 120: mirror = 240-angle elif angle >= 120 and angle < 150: mirror = 240-angle elif angle >= 150 and angle < 180: mirror = 360-angle elif angle >= 180 and angle < 210: mirror = 360-angle elif angle >= 210 and angle < 240: mirror = 480-angle elif angle >= 240 and angle < 270: mirror = 480-angle elif angle >= 270 and angle < 300: mirror = 600-angle elif angle >= 300 and angle < 330: mirror = 600-angle elif angle >= 330: mirror = 600-angle return mirror def getMirrorAngle(angle): if angle >= 0 and angle < 30: mirror = 360-angle elif angle >= 30 and angle < 90: mirror = 120-angle elif angle >= 90 and angle < 150: mirror = 240-angle elif angle >= 150 and angle < 210: mirror = 360-angle elif angle >= 210 and angle < 270: mirror = 480-angle elif angle >= 270 and angle < 330: mirror = 600-angle elif angle >= 330: mirror = 600-angle return mirror def getXYpos(length, angle): radians = math.radians(angle) x_ratio = math.sin(radians) y_ratio = math.cos(radians) x = int(length * x_ratio) y = int(length * y_ratio) return (400+x, 400-y) def distanceFromCentre(pos): if pos >= 400: dist = pos-400 else: dist = 400-pos return dist def hypotenuse(o,a): h = math.sqrt((o**2) + (a**2)) return h def getLengthAngle(mouse_pos): o = distanceFromCentre(mouse_pos[0]) a = distanceFromCentre(mouse_pos[1]) h = hypotenuse(o,a) if h == 0: h = 0.1 # prevent division by zero angle = math.degrees(math.asin(o/h)) return h, angle pygame.init() pygame.display.set_caption('Snowflakes') mouse = pygame.mouse fpsClock = pygame.time.Clock() width = 800 height = 800 window = pygame.display.set_mode((width, height)) canvas = window.copy() # R G B BLACK = pygame.Color( 0 , 0 , 0 ) WHITE = pygame.Color(255, 255, 255) RED = (255, 0, 0) GREEN = ( 0, 255, 0) BLUE = ( 0, 0, 255) RED_GREEN = (255, 255, 0) RED_BLUE = (255, 0, 255) GREEN_BLUE = ( 0, 255, 255) clr = [RED, GREEN, BLUE, RED_GREEN, GREEN_BLUE, RED_BLUE] CENTRE = (width/2, height/2) points = [] canvas.fill(BLACK) showSnowflake = True while True: left_pressed, middle_pressed, right_pressed = mouse.get_pressed() for event in pygame.event.get(): if event.type == QUIT: pygame.quit() sys.exit() if left_pressed: mouse_pos = pygame.mouse.get_pos() length, angle = getLengthAngle(mouse_pos) mirror = getMirrorAngle(angle) movingCircleSize = 5 color = WHITE pygame.draw.circle(canvas, color, getXYpos(length, angle), movingCircleSize) offset = 60 for n in range(int(360/offset)): angle = angle+offset if angle > 359.999: angle = angle - 360 mirror = mirror+offset if mirror > 359.999: mirror = mirror - 360 if showSnowflake == False: points.append((getXYpos(length, angle), getXYpos(length, mirror))) else: pygame.draw.circle(canvas, color, getXYpos(length, angle), movingCircleSize) pygame.draw.circle(canvas, color, getXYpos(length, mirror), movingCircleSize) pressed = pygame.key.get_pressed() if pressed[pygame.K_s]: canvas.fill(BLACK) showSnowflake = True window.fill(BLACK) # draw snowflake spine for angle in [0,60,120,180,240,300]: pygame.draw.line(canvas, RED, CENTRE, getXYpos(300, angle)) if showSnowflake == True: for point in points: pygame.draw.circle(canvas, color, point[0], movingCircleSize) pygame.draw.circle(canvas, color, point[1], movingCircleSize) points = [] if right_pressed: canvas.fill(BLACK) else: window.blit(canvas, (0, 0)) pygame.display.update()
Enjoy.