Advertisement
Doyousketch2

Snowflake2.py

Jan 28th, 2014
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.29 KB | None | 0 0
  1. #
  2. #  Snowflake2.py
  3. #
  4. #  Copyright 2014 Eli Innis  -  DoYouSketch2  (at)  Yahoo.com
  5. #
  6. ########################################################################
  7. #  This program is free software; you can redistribute it and/or modify
  8. #  it under the terms of the GNU General Public License as published by
  9. #  the Free Software Foundation; either version 2 of the License, or
  10. #  (at your option) any later version.
  11. #
  12. #  This program is distributed in the hope that it will be useful,
  13. #  but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. #  GNU General Public License for more details.
  16. #
  17. #  You should have received a copy of the GNU General Public License
  18. #  along with this program; if not, write to the Free Software
  19. #  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  20. #  MA 02110-1301, USA.
  21. ########################################################################
  22.  
  23. from turtle import *
  24. from random import *
  25.  
  26. title("Snowflake 2")
  27.  
  28. # Screen size max X and Y values
  29. mX = 700   # Width
  30. mY = 700   # Height
  31.  
  32. # LINE color values
  33. # (defaults: R = 200, G = 220, B = 255)
  34. R = 200
  35. G = 220
  36. B = 255
  37.  
  38. # SKY color values
  39. # (defaults: sR = 200, sG = 220, sB = 255)
  40. sR = 200
  41. sG = 220
  42. sB = 255
  43.  
  44. # SnowDots color values
  45. # (defaults: sdR = 150, sdG = 160, sdB = 180)
  46. sdR = 150
  47. sdG = 160
  48. sdB = 180
  49.  
  50. # SnowDots, min max values for how many dots
  51. minD = 60
  52. maxD = 80
  53.  
  54. # SnowDots, min max values for size
  55. minS = 2
  56. maxS = 10
  57.  
  58. # Create a blank canvas to draw upon
  59. colormode(255)
  60. bgcolor(50,50,50)
  61. setundobuffer(None)
  62. setup(width=mX, height=mY)  # create screen according to maximum X & Y dimensions.
  63. setworldcoordinates(0, 0, mX, mY)  # set screen as a standard X Y grid
  64.  
  65. # Set up the various "turtles"
  66. One = Turtle()          #    3  o'clock
  67. Two = One.clone()       #    1  o'clock
  68. Three = One.clone()     #   11  o'clock
  69. Four = One.clone()      #    9  o'clock
  70. Five = One.clone()      #    7  o'clock
  71. Six = One.clone()       #    5  o'clock
  72. Seven = One.clone()     #       Center
  73. hideturtle()
  74. speed (0)
  75.  
  76. #  error check to make sure color values fit between 0 and 255
  77. def MinMax(val):
  78.     if val > 255:  val = 255
  79.     elif val < 0:  val = 0
  80.     return val
  81.  
  82. # take it to the center of the screen
  83. def GoHome(Turt):
  84.     Turt.hideturtle()
  85.     Turt.speed(0)
  86.     Turt.pensize(6)
  87.     Turt.penup()
  88.     Turt.seth(0)
  89.     Turt.goto(mX/2,mY/2)
  90.     Turt.pencolor(R,G,B)
  91.  
  92. #  create background
  93. def Sky(sR,sG,sB):
  94.     width = 13
  95.     pensize(width)
  96.     while ycor() < (mY+10):
  97.         pencolor(sR,sG,sB)
  98.         if xcor() < -18:
  99.             setx(mX+20)
  100.         elif xcor() > mX+18:
  101.             setx(-20)
  102.         sety(ycor()+width/3.6)
  103.         sR = MinMax(sR-4)
  104.         sG = MinMax(sG-2)
  105.         sB = MinMax(sB-1)
  106.  
  107. #  create random bokeh dots in the background
  108. #  as if there's out-of-focus snowfall
  109. def SnowDots(minD,maxD,minS,maxS,sdR,sdG,sdB):
  110.     How = 0
  111.     Many = randint(minD,maxD)
  112.     while How < Many:
  113.         # Try to keep snow dots around the edges of the screen
  114.         # so you don't detract from the main snowflake.
  115.         Quadrant = randint(1,4)
  116.         if Quadrant == 1:  goto(randint(0,mX/5),randint(0,mY))
  117.         elif Quadrant == 2:  goto(randint(mX/5*4,mX),randint(0,mY))
  118.         elif Quadrant == 3:  goto(randint(0,mX),randint(0,mY/5))
  119.         else:  goto(randint(0,mX),randint(mY/5*4,mY))
  120.         # Start drawing dots
  121.         pensize(randint(minS,maxS))
  122.         pencolor(sdR,sdG,sdB)
  123.         while int(pensize()) > 3:
  124.             dot()
  125.             seth(90)
  126.             # Ramp up a highlight
  127.             Bokeh = pencolor()
  128.             pencolor(tuple(map(lambda x, y: MinMax(x + y), Bokeh, (30,50,60))))
  129.             # Diminish the brush size as it gets brighter
  130.             CurrentSize = int(pensize())
  131.             pensize(CurrentSize-2)
  132.             # Smoodge it a bit to give that flash photo effect,
  133.             # as if the highlights are coming from the center of the canvas.
  134.             if int(ycor()) < (mY/3):
  135.                 forward(1)
  136.                 if int(ycor()) < (mY/6):
  137.                     forward(2)
  138.             elif int(ycor()) > (mY/3*2):
  139.                 backward(1)
  140.                 if int(ycor()) > (mY/6*5):
  141.                     backward(2)
  142.             if int(xcor()) < (mX/3):
  143.                 right(90)
  144.                 forward(1)
  145.             elif int(xcor()) > (mY/3*2):
  146.                 left(90)
  147.                 forward(1)
  148.         How += 1
  149.  
  150. def SnowFlake(R,G,B):
  151.     # Six points, plus center
  152.     Name = [One, Two, Three, Four, Five, Six, Seven]
  153.     S = 1
  154.     # "One" already points right, so we skip that for now.
  155.     # We have to set the others to face their proper directions.
  156.     while S < 6:
  157.         GoHome(Name[S])
  158.         Name[S].seth(S*60)
  159.         S += 1
  160.     S = 0
  161.     # OK, Two through Six have gone through the "GoHome" routine,
  162.     # but we haven't set One, or the center, Seven.
  163.     GoHome(One)
  164.     GoHome(Seven)
  165.     # Scoot 'em out a bit from the center,
  166.     # because there will be a filled in hexagon there.
  167.     while S < 6:
  168.             Name[S].forward(20)
  169.             Name[S].pendown()
  170.             Name[S].forward(26)
  171.             S += 1
  172.     # Set up the main branches of the snowflake.
  173.     Ray = 6
  174.     while Ray < 11:
  175.         S = 0
  176.         while S < 6:
  177.             # Left branches
  178.             L = Name[S].clone()
  179.             L.left(60)
  180.             L.width(Ray-1)
  181.             L.forward(Ray*3.5)
  182.             LL = L.clone()
  183.             LL.left(60)
  184.             LL.width(Ray-2)
  185.             LL.forward(Ray)
  186.             RL = L.clone()
  187.             RL.right(60)
  188.             RL.width(Ray-2)
  189.             RL.forward(Ray)
  190.             # Right branches
  191.             R = Name[S].clone()
  192.             R.right(60)
  193.             R.width(Ray-1)
  194.             R.forward(Ray*3.5)
  195.             RL = R.clone()
  196.             RL.left(60)
  197.             RL.width(Ray-2)
  198.             RL.forward(Ray)
  199.             RR = R.clone()
  200.             RR.right(60)
  201.             RR.width(Ray-2)
  202.             RR.forward(Ray)
  203.             # Scoot out a little each time
  204.             Name[S].width(Ray+4)
  205.             Name[S].forward(Ray*6)
  206.             S += 1
  207.         Ray += 2
  208.     # filled in Hexagons
  209.     S = 0
  210.     while S < 7:
  211.         Name[S].pencolor(255,255,255)
  212.         Hex = randint(56,57)
  213.         if S == 6:
  214.             # the central one is a bit smaller
  215.             Hex = randint(36,37)
  216.         Name[S].width(2)
  217.         Ring = 1
  218.         while Ring < Hex:
  219.             # Seg = one segment of the hexagon
  220.             Seg = 0
  221.             Name[S].seth(0)
  222.             Name[S].pendown()
  223.             Name[S].forward(2)
  224.             Name[S].left(120)
  225.             while Seg < 6:
  226.                 Name[S].forward(Ring)
  227.                 Name[S].left(60)
  228.                 Seg += 1
  229.             Name[S].right(60)
  230.             # Dim it down a bit
  231.             Fade = Name[S].pencolor()
  232.             Name[S].pencolor(tuple(map(lambda x, y: MinMax(x - y), Fade, (5,4,3))))
  233.             if Ring >= Hex-4:
  234.                 Name[S].pencolor(255,255,255)
  235.             Ring += 2
  236.         S += 1
  237.  
  238. # Now that everything is defined, run it.
  239. # Start at bottom left and draw in the sky.
  240.  
  241. penup()
  242. goto(-20,-10)
  243. pendown()
  244.  
  245. Sky(sR,sG,sB)
  246.  
  247. # Sprinkle in some flurries
  248.  
  249. penup()
  250.  
  251. SnowDots(minD,maxD,minS,maxS,sdR,sdG,sdB)
  252.  
  253. # Now draw the ice crystal
  254.  
  255. SnowFlake(R,G,B)
  256.  
  257. done()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement