Subdomain Posts
Recent Posts
C | 2 min ago
None | 2 min ago
None | 3 min ago
PHP | 5 min ago
None | 6 min ago
PHP | 6 min ago
None | 7 min ago
None | 8 min ago
XML | 8 min ago
None | 9 min ago
Sitereport
Find cool info about any domain on the internet?
visit sitereport
Free Subdomains
Want a pastebin.com sub-domain for your community?
learn more...
What is pastebin?
Pastebin is a website that hosts all your text & code on dedicated servers for easy sharing.
learn more...
Learn a little bit about the new Pastebin.com on our help page. hide message
By Dick Moores on the 10th of Aug 2008 02:14:28 PM Download | Raw | Embed | Report
  1. #!/usr/bin/env python
  2. #coding=utf-8
  3. # random_rectanglesV19_web.py
  4.  
  5. """
  6. ATTENTION: The turtle module imported here is the turtle.py that is part of Python2.6 beta2.
  7. See http://article.gmane.org/gmane.comp.python.tutor/49765 for more information.
  8. """
  9.  
  10. from turtle26 import *
  11. from random import randint, choice, randrange
  12. from math import ceil
  13. import time
  14.  
  15. COLOR_NAMES_NO_GREYS = ['yellow', 'darkseagreen', 'aquamarine4', 'aquamarine1', 'aquamarine3', 'aquamarine2', 'brown', 'indianred4', 'skyblue', 'rosybrown4', 'cornsilk', 'lightpink', 'indianred2', 'chocolate1', 'lemonchiffon3', 'lemonchiffon2', 'lemonchiffon1', 'bisque', 'violetred1', 'chocolate2', 'lemonchiffon4', 'chocolate3', 'chocolate4', 'lightgoldenrodyellow', 'lavender', 'chartreuse3', 'chartreuse2', 'chartreuse1', 'darkslateblue', 'chartreuse4', 'lightskyblue', 'blue', 'rosybrown2', 'maroon4', 'maroon3', 'maroon2', 'maroon1', 'gold3', 'gold2', 'gold1', 'gold4', 'wheat4', 'rosybrown1', 'skyblue4', 'skyblue1', 'skyblue3', 'skyblue2', 'indianred1', 'floralwhite', 'turquoise1', 'firebrick3', 'firebrick2', 'firebrick1', 'steelblue2', 'steelblue4', 'firebrick4', 'lightskyblue1', 'greenyellow', 'lightseagreen', 'sienna', 'tomato', 'indianred3', 'royalblue', 'gainsboro', 'peru', 'red3', 'red2', 'red1', 'springgreen3', 'violetred4', 'lightyellow', 'lemonchiffon', 'chocolate', 'antiquewhite', 'moccasin', 'deeppink', 'springgreen1', 'springgreen2', 'dodgerblue', 'springgreen4', 'seashell2', 'seashell3', 'magenta', 'seashell1', 'tan', 'seashell4', 'pink', 'palevioletred', 'powderblue', 'mediumblue', 'honeydew1', 'mediumpurple1', 'mediumpurple2', 'mediumpurple3', 'mediumpurple4', 'honeydew2', 'khaki4', 'honeydew3', 'paleturquoise3', 'khaki1', 'cadetblue', 'khaki3', 'salmon1', 'honeydew4', 'salmon3', 'salmon2', 'salmon4', 'linen', 'green', 'blueviolet', 'brown2', 'brown3', 'brown1', 'brown4', 'orange4', 'orange1', 'orange3', 'orange2', 'cyan', 'yellow4', 'yellow3', 'yellow2', 'yellow1', 'orange', 'lightsteelblue', 'goldenrod', 'cyan4', 'darkviolet', 'darkmagenta', 'navy', 'ghostwhite', 'plum', 'khaki2', 'darkkhaki', 'violetred', 'red', 'peachpuff', 'lightslateblue', 'lightskyblue4', 'aliceblue', 'lightskyblue2', 'lightskyblue3', 'blue1', 'blue3', 'blue2', 'blue4', 'purple3', 'lightcyan', 'darksalmon', 'mediumspringgreen', 'tomato4', 'tomato1', 'tomato3', 'tomato2', 'mistyrose', 'blanchedalmond', 'orangered', 'navajowhite', 'pink3', 'pink2', 'burlywood', 'pink4', 'mistyrose1', 'white', 'darkolivegreen4', 'lightyellow3', 'paleturquoise4', 'darkolivegreen1', 'seagreen4', 'seagreen3', 'seagreen2', 'seagreen1', 'paleturquoise2', 'limegreen', 'mistyrose3', 'palegreen1', 'palegreen3', 'palegreen2', 'palegreen4', 'mistyrose2', 'burlywood1', 'turquoise2', 'burlywood3', 'burlywood2', 'burlywood4', 'rosybrown', 'turquoise4', 'whitesmoke', 'lightblue', 'snow', 'orangered3', 'orangered2', 'orangered1', 'orangered4', 'mistyrose4', 'thistle2', 'thistle1', 'salmon', 'thistle4', 'oldlace', 'darkseagreen4', 'darkseagreen3', 'darkseagreen2', 'darkseagreen1', 'darkblue', 'lightpink1', 'gold', 'lightsalmon4', 'lightsalmon2', 'lightsalmon3', 'lightsalmon1', 'green4', 'green1', 'green3', 'green2', 'steelblue1', 'papayawhip', 'black', 'orchid4', 'orchid1', 'orchid2', 'orchid3', 'purple2', 'antiquewhite3', 'antiquewhite2', 'antiquewhite1', 'antiquewhite4', 'bisque4', 'bisque1', 'bisque2', 'bisque3', 'darkturquoise', 'turquoise3', 'plum4', 'plum3', 'plum2', 'plum1', 'lightblue3', 'thistle', 'violet', 'darkorchid4', 'darkorchid1', 'darkorchid2', 'darkorchid3', 'rosybrown3', 'honeydew', 'cornflowerblue', 'palevioletred4', 'palevioletred1', 'palevioletred2', 'palevioletred3', 'mediumpurple', 'lightblue2', 'darkcyan', 'lightblue1', 'lightblue4', 'darkred', 'lavenderblush1', 'lavenderblush3', 'lavenderblush2', 'lavenderblush4', 'mediumturquoise', 'lightyellow1', 'lightyellow4', 'lightgoldenrod', 'navajowhite4', 'hotpink', 'olivedrab', 'navajowhite1', 'navajowhite2', 'magenta2', 'magenta1', 'palegreen', 'seashell', 'aquamarine', 'tan4', 'tan3', 'tan2', 'tan1', 'paleturquoise1', 'lawngreen', 'slateblue', 'darkorange4', 'mintcream', 'darkorange1', 'darkorange3', 'darkorange2', 'saddlebrown', 'goldenrod4', 'goldenrod1', 'goldenrod2', 'goldenrod3', 'darkgoldenrod', 'lightcyan1', 'sandybrown', 'lightcyan3', 'ivory3', 'ivory2', 'ivory1', 'lightcyan2', 'mediumseagreen', 'ivory4', 'darkorange', 'violetred2', 'steelblue3', 'lightcyan4', 'sienna1', 'palegoldenrod', 'sienna3', 'violetred3', 'sienna2', 'pink1', 'lightyellow2', 'darkolivegreen3', 'darkolivegreen2', 'lavenderblush', 'azure1', 'azure3', 'azure2', 'azure4', 'firebrick', 'indianred', 'sienna4', 'darkolivegreen', 'mediumorchid4', 'lightsteelblue1', 'lightsteelblue2', 'lightsteelblue3', 'lightsteelblue4', 'darkgoldenrod4', 'mediumorchid1', 'magenta4', 'magenta3', 'darkgoldenrod1', 'darkgoldenrod2', 'darkgoldenrod3', 'chartreuse', 'mediumslateblue', 'mediumorchid3', 'springgreen', 'mediumorchid2', 'lightsalmon', 'cyan2', 'turquoise', 'cyan3', 'olivedrab4', 'royalblue4', 'cyan1', 'purple4', 'navajowhite3', 'darkgreen', 'peachpuff4', 'purple1', 'slateblue3', 'peachpuff1', 'peachpuff2', 'olivedrab1', 'olivedrab2', 'olivedrab3', 'slateblue4', 'slateblue2', 'lightgoldenrod4', 'yellowgreen', 'lightgoldenrod1', 'slateblue1', 'lightgoldenrod3', 'lightgoldenrod2', 'peachpuff3', 'orchid', 'purple', 'darkorchid', 'royalblue1', 'thistle3', 'wheat1', 'wheat3', 'wheat2', 'coral3', 'coral2', 'coral1', 'deepskyblue4', 'deepskyblue3', 'deepskyblue2', 'deepskyblue1', 'coral4', 'khaki', 'wheat', 'deepskyblue', 'coral', 'beige', 'azure', 'dodgerblue1', 'dodgerblue2', 'dodgerblue3', 'dodgerblue4', 'mediumvioletred', 'snow4', 'snow2', 'snow3', 'snow1', 'lightcoral', 'ivory', 'lightpink2', 'lightpink3', 'forestgreen', 'lightpink4', 'midnightblue', 'mediumorchid', 'cornsilk4', 'cornsilk2', 'cornsilk3', 'cornsilk1', 'royalblue3', 'royalblue2', 'maroon', 'seagreen', 'red4', 'cadetblue4', 'cadetblue3', 'cadetblue2', 'cadetblue1', 'paleturquoise', 'deeppink3', 'deeppink2', 'deeppink1', 'deeppink4', 'hotpink3', 'hotpink2', 'hotpink1', 'lightgreen', 'hotpink4', 'navyblue', 'mediumaquamarine', 'steelblue']
  16.  
  17. BACKGROUND_COLORS = ['aquamarine4', 'blue', 'blueviolet', 'cadetblue1',
  18.                 'cadetblue3', 'chartreuse2', 'chartreuse4',
  19.                 'coral', 'cyan', 'cyan3', 'darkgoldenrod1', 'darkgreen',
  20.                 'darkolivegreen1', 'darkorange2', 'darksalmon', 'darkseagreen',
  21.                 'darkseagreen2', 'darkviolet', 'deeppink', 'firebrick',
  22.                 'forestgreen', 'gold', 'green', 'hotpink', 'lightcoral',
  23.                 'lightcyan', 'lightgoldenrod1', 'lightgoldenrodyellow',
  24.                 'lightsalmon2', 'lightskyblue',
  25.                 'lightslateblue', 'lightsteelblue1', 'lightsteelblue3',
  26.                 'magenta', 'magenta1', 'magenta2', 'magenta3', 'mediumorchid1',
  27.                 'mediumvioletred', 'navy', 'orangered', 'orangered4', 'orchid1',
  28.                 'palegreen', 'paleturquoise1', 'palevioletred1', 'papayawhip',
  29.                 'pink', 'plum1', 'purple','purple4', 'red4', 'skyblue', 'skyblue2',
  30.                 'skyblue3', 'springgreen',
  31.                 'springgreen4', 'turquoise1', 'turquoise3', 'violetred', 'yellow']
  32.  
  33. def floatToNearestInt(x):
  34.     """
  35.    convert float x to the nearest integer n
  36.    e.g., 3.4 --> 3;  45.5 --> 46; -2.1 --> -2; -0.8 --> -1
  37.    """
  38.     return int(round(x))
  39.  
  40. def intCommas(n):
  41.     """
  42.    inserts commas into integers. E.g. -12345678 -> -12,345,789
  43.    """
  44.     s = str(n)
  45.     sign = ''
  46.     if s[0] == '-':
  47.         sign = '-'
  48.         s = s[1:]
  49.     slen = len(s)
  50.     a = ''
  51.     for index in range(slen):
  52.         if index > 0 and index % 3 == slen % 3:
  53.             a = a + ','
  54.         a = a + s[index]
  55.     return sign + a
  56.  
  57. def int_to_ordinal(n):
  58.     """
  59.    Takes an int and returns the string ordinal integer, with commas as appropriate
  60.    plus the appropriate suffix; e.g. 1237 -> 1,237th
  61.    """
  62.     n = str(n)
  63.     if n[-1] in "0456789":
  64.         suff = "th"
  65.     elif n[-2:] in ['11', '12', '13']:
  66.         suff = "th"
  67.     elif n[-1] == "1":
  68.         suff = "st"
  69.     elif n [-1] == "2":
  70.         suff = "nd"
  71.     else:
  72.         suff = "rd"
  73.     return intCommas(n) + suff
  74.  
  75. def hmsToText(seconds):
  76.     """
  77.    Convert seconds to hours, minutes, seconds and return result
  78.    """
  79.     seconds = floatToNearestInt(seconds)
  80.     hours, minutes = 0, 0
  81.  
  82.     if seconds >= 60 and seconds < 3600:
  83.         minutes, seconds = divmod(seconds, 60)
  84.     elif seconds >= 3600:
  85.         hours, seconds = divmod(seconds, 3600)
  86.         minutes, seconds = divmod(seconds, 60)
  87.  
  88.     hp, mp, sp = "s", "s", "s" # add "s" to make "hour", "minute", "second" plural
  89.     if hours == 1:
  90.         hp = "" # no "s" on "hour"
  91.     if minutes == 1:
  92.         mp = ""
  93.     if seconds >= 1 and seconds < 2:
  94.         sp = ""
  95.  
  96.     if hours == 0 and minutes == 0:
  97.         result = "%d second%s" % (seconds, sp)
  98.     elif hours == 0:
  99.         if seconds == 0:
  100.             result = "%d minute%s exactly" % (minutes, mp)
  101.         else:
  102.             result = "%d minute%s, %d second%s" % (minutes, mp, seconds, sp)
  103.     elif minutes == 0:
  104.         if seconds == 0:
  105.             result = "%d hour%s exactly" % (hours, hp)
  106.         else:
  107.             result = "%d hour%s, %d second%s" % (hours, hp, seconds, sp)
  108.     else:
  109.         result = "%d hour%s, %d minute%s, %d second%s" % (hours, hp, minutes, mp, seconds, sp)
  110.     return result
  111.  
  112. def getMinAndMaxIntegersFromUser(default_min, default_max):
  113.     print "Enter 2 integers > 1 for min and max, with max >= min"
  114.     print "Put a comma between the numbers. E.g., '8, 20'"
  115.     print "Press Enter to set default of  %d, %d" % (default_min, default_max)
  116.  
  117.     while True:
  118.         ans = raw_input("min, max: ")
  119.         if ans == '':
  120.             print "The default min, max have been set to %d, %d" % (default_min, default_max)
  121.             return (5, 20)
  122.         try:
  123.             min, max = ans.split(',')
  124.         except ValueError:
  125.             print "\nPlease start over and enter correct min and max."
  126.             print "Enter 2 integers > 1 for min and max, with max >= min"
  127.             print "Press Enter to set default of  5, 20\n"
  128.             continue
  129.         try:
  130.             min = int(min)
  131.         except ValueError:
  132.             print "\nPlease start over and enter correct min and max."
  133.             print "Enter 2 INTEGERS > 1 for min and max, with max >= min"
  134.             print "Press Enter to set default of  5, 20\n"
  135.             continue
  136.         try:
  137.             max = int(max)
  138.         except ValueError:
  139.             print "\nPlease start over and enter correct min and max."
  140.             print "Enter 2 INTEGERS > 1 for min and max, with max >= min"
  141.             print "Press Enter to set default of  5, 20"
  142.             continue
  143.         if min > max or min < 2:
  144.             print "\nPlease start over and enter correct min and max."
  145.             print "min must be <= max and min must be > 1"
  146.             print "Enter 2 integers > 1 for min and max, with max >= min"
  147.             print "Press Enter to set default of  5, 20"
  148.             continue
  149.         else:
  150.             break
  151.     return (min, max)
  152.  
  153. def get_pause_from_user(default_pause):
  154.     print """
  155. You can set the pause between the filling of a rectangle and the start of the
  156. next. Enter any non-negative number for the number of seconds to pause.
  157. E.g., 0, 2, or 2.4
  158. To set the default pause of %s second, just press Enter
  159. """ % str(default_pause)
  160.     ans = raw_input("Enter pause in seconds: ")
  161.     if ans == '':
  162.         print "a pause of %s seconds has been set" % str(default_pause)
  163.         return default_pause
  164.     else:
  165.         print "a pause of %.3g seconds has been set" % float(ans)
  166.         return float(ans)
  167.  
  168. def get_speed_and_delay_from_user(default_speed, default_delay):
  169.     print """
  170. Enter the speed and delay you want in the drawing of rectangles as
  171. two integers separated by a comma; e.g.,  2, 20. If you want the
  172. default of speed %d and the delay %d, just press Enter.
  173.    """ % (default_speed, default_delay)
  174.     ans = raw_input("Enter speed and delay: ")
  175.     if ans == '':
  176.         print "speed %d and delay %d have been set" % (default_speed, default_delay)
  177.         return default_speed, default_delay
  178.     else:
  179.         spd, dly = ans.split(',')
  180.         spd, dly = int(spd), int(dly)
  181.         print "a speed of %d and a delay of %d have been set" % (spd, dly)
  182.         return spd, dly
  183.  
  184. def get_max_num_cycles_from_user():
  185.     longstr = """\nEnter the number of cycles; %d cycles will show approximately %d rectangles using all %d colors"""
  186.     print longstr % (num_cycles_to_show_all_colors, num_avail_colors, num_avail_colors)
  187.     print "Press Enter to set the default of %d" % num_cycles_to_show_all_colors
  188.     ans = raw_input("Number of cycles: ")
  189.     if ans == '':
  190.         ans = num_cycles_to_show_all_colors
  191.         print "The number of cycles has been set to %d." % num_cycles_to_show_all_colors
  192.     else:
  193.         ans = int(ans)
  194.     return ans
  195.  
  196. def adjust_for_monitor_size_and_resolution():
  197.     wd = window_width()
  198.     width_adjustment = wd*1.0/999
  199.     ht = window_height()
  200.     height_adjustment = ht*1.0/702
  201.     return width_adjustment, height_adjustment
  202.  
  203. def get_next_rect_params():
  204.     color_name = get_next_rect_color()
  205.     x = randrange(15, 451, 2)
  206.     y = randrange(15, 251, 2)
  207.     width_adjustment, height_adjustment = adjust_for_monitor_size_and_resolution()
  208.     x = x * width_adjustment
  209.     y = y * height_adjustment
  210.     pen_size = get_pen_size(x, y)
  211.     flag = randint(1, 8)
  212.     return pen_size, color_name, x, y, flag
  213.  
  214. def get_pen_size(x, y):
  215.     """
  216.    Return a pen size proportional to the area of the rectangle
  217.    """
  218.     pen_size = (x * y)//11320 + 1
  219.     return pen_size
  220.  
  221. def get_next_rect_color():
  222.     global used_colors
  223.     global multiple_of_num_avail_colors
  224.     while True:
  225.         color_name = choice(COLOR_NAMES_NO_GREYS)
  226.         if len(used_colors) >= num_avail_colors:
  227.             used_colors = []
  228.             multiple_of_num_avail_colors += num_avail_colors
  229.         if color_name not in used_colors:
  230.             used_colors.append(color_name)
  231.             return color_name
  232.  
  233. def draw_rect():
  234.     speed(spd)
  235.     pensize(pen_size)
  236.     color(color_name)
  237.     print color_name
  238.     begin_fill()
  239.     ## NE, CW (northeast, clockwise)
  240.     if flag == 1:
  241.         up()
  242.         goto(x, y)
  243.         down()
  244.         # up to here tracer(False) is in effect from the last time draw_rect()
  245.         # was called (see bottom line)
  246.         tracer(True, dly)
  247.         goto(x, -y)
  248.         goto(-x, -y)
  249.         goto(-x, y)
  250.         goto(x, y)
  251.     ## NE, CCW (northeast, counterclockwise)
  252.     elif flag == 2:
  253.         up()
  254.         goto(x, y)
  255.         down()
  256.         tracer(True, dly)
  257.         goto(-x, y)
  258.         goto(-x, -y)
  259.         goto(x, -y)
  260.         goto(x, y)
  261.     ## SE, CW
  262.     elif flag == 3:
  263.         up()
  264.         goto(x, -y)
  265.         down()
  266.         tracer(True, dly)
  267.         goto(-x, -y)
  268.         goto(-x, y)
  269.         goto(x, y)
  270.         goto(x, -y)
  271.     ## SE, CCW
  272.     elif flag == 4:
  273.         up()
  274.         goto(x, -y)
  275.         down()
  276.         tracer(True, dly)
  277.         goto(x, y)
  278.         goto(-x, y)
  279.         goto(-x, -y)
  280.         goto(x, -y)
  281.     ## SW, CW
  282.     elif flag == 5:
  283.         up()
  284.         goto(-x, -y)
  285.         down()
  286.         tracer(True, dly)
  287.         goto(-x, y)
  288.         goto(x, y)
  289.         goto(x, -y)
  290.         goto(-x, -y)
  291.     ## SW, CCW
  292.     elif flag == 6:
  293.         up()
  294.         goto(-x, -y)
  295.         down()
  296.         tracer(True, dly)
  297.         goto(x, -y)
  298.         goto(x, y)
  299.         goto(-x, y)
  300.         goto(-x, -y)
  301.     ## NW, CW
  302.     elif flag == 7:
  303.         up()
  304.         goto(-x, y)
  305.         down()
  306.         tracer(True, dly)
  307.         goto(x, y)
  308.         goto(x, -y)
  309.         goto(-x, -y)
  310.         goto(-x, y)
  311.     ## NW, CCW
  312.     elif flag == 8:
  313.         up()
  314.         goto(-x, y)
  315.         down()
  316.         tracer(True, dly)
  317.         goto(-x, -y)
  318.         goto(x, -y)
  319.         goto(x, y)
  320.         goto(-x, y)
  321.     end_fill()
  322.     tracer(False)
  323.  
  324. def get_black_or_colored():
  325.     """
  326.    For the next background color, return 0 if it will
  327.    be "black"; 1 if it will be colored, i.e., a color
  328.    to be chosen from the list of unused background colors.
  329.    This function serves the purpose of preventing "black"
  330.    from being used more than once in a row, and a non-black
  331.    color from being used more than four times in a row.
  332.    """
  333.     global black_or_colored_list
  334.     if sum(black_or_colored_list[-1:]) == 0:
  335.         black_or_colored = 1
  336.     elif sum(black_or_colored_list[-4:]) == 4:
  337.         black_or_colored = 0
  338.     else:
  339.         black_or_colored = choice((0, 1))
  340.     black_or_colored_list.append(black_or_colored)
  341.     return black_or_colored
  342.  
  343. def get_next_background_color():
  344.     """
  345.    Return random hue from an updating list of unused background colors.
  346.    """
  347.     global BACKGROUND_COLORS
  348.     global unused_background_colors
  349.     while True:
  350.         if len(unused_background_colors) > 0:
  351.             hue = choice(unused_background_colors)
  352.             unused_background_colors.remove(hue)
  353.             return hue
  354.         else:
  355.             unused_background_colors = BACKGROUND_COLORS[:]
  356.  
  357. def get_background():
  358.     black_or_colored = get_black_or_colored() # 0 for black; 1 for colored
  359.     clearscreen()
  360.     if black_or_colored == 0:
  361.         hue = 'black'
  362.     else:
  363.         hue = get_next_background_color()
  364.     if num_cycles < max_num_cycles:
  365.         print
  366.         print '%s is the new background color' % hue
  367.     return hue
  368.  
  369. def print_so_far_report():
  370.     if 0 < num_used_colors_to_report < num_avail_colors:
  371.         astr = "%d different colors of %d have been used for %d rectangles so far"
  372.         print astr % (num_used_colors_to_report, num_avail_colors, num_used_colors_to_report)
  373.     elif num_avail_colors <= num_used_colors_to_report:
  374.         longstr = "all %d different colors have been used for %d rectangles so far"
  375.         print longstr % (num_avail_colors, num_used_colors_to_report)
  376.  
  377. def s_or_none(n):
  378.     """
  379.    Returns 's' to pluralize regular nouns.
  380.    """
  381.     if n == 1:
  382.         return ''
  383.     else:
  384.         return 's'
  385.  
  386. def print_program_end_report():
  387.     if 0 < total_rects < num_avail_colors:
  388.         astr = "%d colors of %d were used for %d rectangles"
  389.         print astr % (total_rects, num_avail_colors,
  390.         total_rects)
  391.     elif num_avail_colors <= total_rects:
  392.         print "all %d colors were used for %d rectangles" % (num_avail_colors,
  393.         total_rects)
  394.  
  395. def beepN(times,interval):
  396.     """Calls beep() n times at interval of x seconds."""
  397.     import time
  398.     for k in range(times-1):
  399.         beep()
  400.         time.sleep(interval)
  401.     beep()
  402.  
  403. def beep():
  404.     """A very short Beep"""
  405.     import winsound
  406.     pitch = 500
  407.     length = 30
  408.     winsound.Beep(pitch,length)
  409.  
  410. print \
  411. """
  412. This program creates sets (cycles) of concentric randomly sized and
  413. randomly colored rectangles against background colors randomly chosen from a
  414. special list of %d colors. After the initial background of white, a black
  415. background will be chosen at least once in five times, but never twice in a row.
  416. No rectangle color will repeat until all %d colors have been chosen.
  417. """ % (len(BACKGROUND_COLORS), len(COLOR_NAMES_NO_GREYS))
  418. ## all the defaults are here
  419. default_min, default_max = 5, 20
  420. default_pause = 1
  421. default_speed = 4
  422. default_delay = 15
  423. print """
  424. You can accept all defaults by pressing Enter at the prompt
  425. Enter 'n' to decline to accept all defaults
  426. """
  427. ans = raw_input("Your choice: ")
  428. if ans == '':
  429.     flag = True
  430. else:
  431.     flag = False
  432.  
  433. if not flag:
  434.     print \
  435.     """You can configure the number of rectangles per cycle by setting the minimum
  436.    and maximum numbers. This will be the range from which the actual number will
  437.    be chosen by a random process for each cycle. E.g., enter  10, 15 for a range of
  438.    10 to 15."""
  439.  
  440.     print "For the default of %d, %d just press Enter." % (default_min, default_max)
  441.     begin_range, end_range = getMinAndMaxIntegersFromUser(default_min, default_max)
  442.     pause = get_pause_from_user(default_pause)
  443. else:
  444.     begin_range, end_range = default_min, default_max
  445.     pause = default_pause
  446.  
  447. avg = (end_range + begin_range)*1.0/2
  448. num_avail_colors = len(COLOR_NAMES_NO_GREYS)
  449. num_cycles_to_show_all_colors = int(ceil(num_avail_colors/avg))
  450.  
  451. if not flag:
  452.     max_num_cycles = get_max_num_cycles_from_user()
  453. else:
  454.     max_num_cycles = num_cycles_to_show_all_colors
  455.  
  456. print """
  457. The program will stop after %d cycles.
  458. To stop it sooner, click in the console window to
  459. make it the active window; then press Ctrl+Q.
  460. """ % max_num_cycles
  461.  
  462.  
  463.  
  464. if not flag:
  465.     spd, dly = get_speed_and_delay_from_user(default_speed, default_delay)
  466. else:
  467.     spd, dly = default_speed, default_delay
  468. ## spd is used in line below; dly is used in tracer() in function draw_rect()
  469. speed(spd)
  470. print "\nThe program has started\n"
  471. timeStart = time.time()
  472. mode("standard")
  473. setup(width=.735, height=.915, startx=-1, starty=0)
  474. unused_background_colors = BACKGROUND_COLORS[:]
  475. num_cycles = 0
  476. used_colors = []
  477. total_rects = 0
  478. multiple_of_num_avail_colors = 0
  479. #flag = 0
  480. black_or_colored_list = [10, 10, 10, 0] ## so that first sum of list cannot be 0 or 4
  481.  
  482. title_string = "Random Rectangles with %d Colors" % num_avail_colors
  483. title(title_string)
  484.  
  485. for x in range(1, max_num_cycles+1):
  486.     if x == 1:
  487.         bgcolor("white")
  488.         print "white is the background color"
  489.     final_rect_num = randint(begin_range, end_range)
  490.     count = 0
  491.     num_used_colors = len(used_colors)
  492.     num_used_colors_to_report = num_used_colors + multiple_of_num_avail_colors
  493.     print_so_far_report()
  494.     print "%d rectangles are coming up in this %s cycle" % (final_rect_num,
  495.                                             int_to_ordinal(num_cycles+1))
  496.  
  497.     for x in range(0, final_rect_num):
  498.         pen_size, color_name, x, y, flag = get_next_rect_params()
  499.         hideturtle()
  500.         draw_rect()
  501.         total_rects += 1
  502.         count += 1
  503.         if count == final_rect_num:
  504.             num_cycles += 1
  505.             print "That's the end of the %s cycle of %d cycles" % (int_to_ordinal(num_cycles), max_num_cycles)
  506.             time.sleep(1.5)
  507.             if num_cycles < max_num_cycles:
  508.                 hue = get_background()
  509.                 bgcolor(hue)
  510.             else:
  511.                 beepN(3, 0.2)
  512.                 print "The program will close in 10 seconds"
  513.         else:
  514.             time.sleep(pause)
  515. ## report at end of program
  516. print
  517.  
  518. print_program_end_report()
  519. timeEnd = time.time()
  520. print "Time was %s" % (hmsToText(timeEnd - timeStart))
  521.  
  522. time.sleep(8.5)
Submit a correction or amendment below. [ previous version ] | [ difference ] | Make A New Post
To highlight particular lines, prefix each line with @h@
Syntax highlighting:
Post expiration:
Post exposure:
Name / Title:
Email: