Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #
- # Snowflake2.py
- #
- # Copyright 2014 Eli Innis - DoYouSketch2 (at) Yahoo.com
- #
- ########################################################################
- # This program is free software; you can redistribute it and/or modify
- # it under the terms of the GNU General Public License as published by
- # the Free Software Foundation; either version 2 of the License, or
- # (at your option) any later version.
- #
- # This program is distributed in the hope that it will be useful,
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- # GNU General Public License for more details.
- #
- # You should have received a copy of the GNU General Public License
- # along with this program; if not, write to the Free Software
- # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
- # MA 02110-1301, USA.
- ########################################################################
- from turtle import *
- from random import *
- title("Snowflake 2")
- # Screen size max X and Y values
- mX = 700 # Width
- mY = 700 # Height
- # LINE color values
- # (defaults: R = 200, G = 220, B = 255)
- R = 200
- G = 220
- B = 255
- # SKY color values
- # (defaults: sR = 200, sG = 220, sB = 255)
- sR = 200
- sG = 220
- sB = 255
- # SnowDots color values
- # (defaults: sdR = 150, sdG = 160, sdB = 180)
- sdR = 150
- sdG = 160
- sdB = 180
- # SnowDots, min max values for how many dots
- minD = 60
- maxD = 80
- # SnowDots, min max values for size
- minS = 2
- maxS = 10
- # Create a blank canvas to draw upon
- colormode(255)
- bgcolor(50,50,50)
- setundobuffer(None)
- setup(width=mX, height=mY) # create screen according to maximum X & Y dimensions.
- setworldcoordinates(0, 0, mX, mY) # set screen as a standard X Y grid
- # Set up the various "turtles"
- One = Turtle() # 3 o'clock
- Two = One.clone() # 1 o'clock
- Three = One.clone() # 11 o'clock
- Four = One.clone() # 9 o'clock
- Five = One.clone() # 7 o'clock
- Six = One.clone() # 5 o'clock
- Seven = One.clone() # Center
- hideturtle()
- speed (0)
- # error check to make sure color values fit between 0 and 255
- def MinMax(val):
- if val > 255: val = 255
- elif val < 0: val = 0
- return val
- # take it to the center of the screen
- def GoHome(Turt):
- Turt.hideturtle()
- Turt.speed(0)
- Turt.pensize(6)
- Turt.penup()
- Turt.seth(0)
- Turt.goto(mX/2,mY/2)
- Turt.pencolor(R,G,B)
- # create background
- def Sky(sR,sG,sB):
- width = 13
- pensize(width)
- while ycor() < (mY+10):
- pencolor(sR,sG,sB)
- if xcor() < -18:
- setx(mX+20)
- elif xcor() > mX+18:
- setx(-20)
- sety(ycor()+width/3.6)
- sR = MinMax(sR-4)
- sG = MinMax(sG-2)
- sB = MinMax(sB-1)
- # create random bokeh dots in the background
- # as if there's out-of-focus snowfall
- def SnowDots(minD,maxD,minS,maxS,sdR,sdG,sdB):
- How = 0
- Many = randint(minD,maxD)
- while How < Many:
- # Try to keep snow dots around the edges of the screen
- # so you don't detract from the main snowflake.
- Quadrant = randint(1,4)
- if Quadrant == 1: goto(randint(0,mX/5),randint(0,mY))
- elif Quadrant == 2: goto(randint(mX/5*4,mX),randint(0,mY))
- elif Quadrant == 3: goto(randint(0,mX),randint(0,mY/5))
- else: goto(randint(0,mX),randint(mY/5*4,mY))
- # Start drawing dots
- pensize(randint(minS,maxS))
- pencolor(sdR,sdG,sdB)
- while int(pensize()) > 3:
- dot()
- seth(90)
- # Ramp up a highlight
- Bokeh = pencolor()
- pencolor(tuple(map(lambda x, y: MinMax(x + y), Bokeh, (30,50,60))))
- # Diminish the brush size as it gets brighter
- CurrentSize = int(pensize())
- pensize(CurrentSize-2)
- # Smoodge it a bit to give that flash photo effect,
- # as if the highlights are coming from the center of the canvas.
- if int(ycor()) < (mY/3):
- forward(1)
- if int(ycor()) < (mY/6):
- forward(2)
- elif int(ycor()) > (mY/3*2):
- backward(1)
- if int(ycor()) > (mY/6*5):
- backward(2)
- if int(xcor()) < (mX/3):
- right(90)
- forward(1)
- elif int(xcor()) > (mY/3*2):
- left(90)
- forward(1)
- How += 1
- def SnowFlake(R,G,B):
- # Six points, plus center
- Name = [One, Two, Three, Four, Five, Six, Seven]
- S = 1
- # "One" already points right, so we skip that for now.
- # We have to set the others to face their proper directions.
- while S < 6:
- GoHome(Name[S])
- Name[S].seth(S*60)
- S += 1
- S = 0
- # OK, Two through Six have gone through the "GoHome" routine,
- # but we haven't set One, or the center, Seven.
- GoHome(One)
- GoHome(Seven)
- # Scoot 'em out a bit from the center,
- # because there will be a filled in hexagon there.
- while S < 6:
- Name[S].forward(20)
- Name[S].pendown()
- Name[S].forward(26)
- S += 1
- # Set up the main branches of the snowflake.
- Ray = 6
- while Ray < 11:
- S = 0
- while S < 6:
- # Left branches
- L = Name[S].clone()
- L.left(60)
- L.width(Ray-1)
- L.forward(Ray*3.5)
- LL = L.clone()
- LL.left(60)
- LL.width(Ray-2)
- LL.forward(Ray)
- RL = L.clone()
- RL.right(60)
- RL.width(Ray-2)
- RL.forward(Ray)
- # Right branches
- R = Name[S].clone()
- R.right(60)
- R.width(Ray-1)
- R.forward(Ray*3.5)
- RL = R.clone()
- RL.left(60)
- RL.width(Ray-2)
- RL.forward(Ray)
- RR = R.clone()
- RR.right(60)
- RR.width(Ray-2)
- RR.forward(Ray)
- # Scoot out a little each time
- Name[S].width(Ray+4)
- Name[S].forward(Ray*6)
- S += 1
- Ray += 2
- # filled in Hexagons
- S = 0
- while S < 7:
- Name[S].pencolor(255,255,255)
- Hex = randint(56,57)
- if S == 6:
- # the central one is a bit smaller
- Hex = randint(36,37)
- Name[S].width(2)
- Ring = 1
- while Ring < Hex:
- # Seg = one segment of the hexagon
- Seg = 0
- Name[S].seth(0)
- Name[S].pendown()
- Name[S].forward(2)
- Name[S].left(120)
- while Seg < 6:
- Name[S].forward(Ring)
- Name[S].left(60)
- Seg += 1
- Name[S].right(60)
- # Dim it down a bit
- Fade = Name[S].pencolor()
- Name[S].pencolor(tuple(map(lambda x, y: MinMax(x - y), Fade, (5,4,3))))
- if Ring >= Hex-4:
- Name[S].pencolor(255,255,255)
- Ring += 2
- S += 1
- # Now that everything is defined, run it.
- # Start at bottom left and draw in the sky.
- penup()
- goto(-20,-10)
- pendown()
- Sky(sR,sG,sB)
- # Sprinkle in some flurries
- penup()
- SnowDots(minD,maxD,minS,maxS,sdR,sdG,sdB)
- # Now draw the ice crystal
- SnowFlake(R,G,B)
- done()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement