示例#1
0
def test_color_changes():
    """ Tests the   color_changes   function. """
    print('--------------------------------------------------')
    print('Testing the   color_changes   function:')
    print('See the two graphics windows that pop up.')
    print('--------------------------------------------------')

    # ------------------------------------------------------------------
    # Test 1: Flashes red, white, blue -- 5 times.
    # ------------------------------------------------------------------
    window = rg.RoseWindow(400,
                           180,
                           'Red, white and blue!',
                           canvas_color='dark gray')

    circle = rg.Circle(rg.Point(150, 100), 40)
    circle.attach_to(window.initial_canvas)

    number_of_cycles = 5
    window.continue_on_mouse_click('Click anywhere in here to start')

    for _ in range(number_of_cycles):
        color_changes(window, circle, ['red', 'white', 'blue'])

    window.close_on_mouse_click()

    # ------------------------------------------------------------------
    # Test 2: Flashes through a bunch of colors,
    # forwards in rectangle, then backwards in an ellipse.
    # ------------------------------------------------------------------
    window = rg.RoseWindow(400, 250, 'Colors!', canvas_color='yellow')

    rectangle = rg.Rectangle(rg.Point(125, 100), 100, 40)
    ellipse = rg.Ellipse(rg.Point(300, 100), 70, 160)

    rectangle.attach_to(window.initial_canvas)
    ellipse.attach_to(window.initial_canvas)

    colors = [
        'red', 'white', 'blue', 'chartreuse', 'chocolate', 'DodgerBlue',
        'LightPink', 'maroon', 'orchid', 'plum', 'SteelBlue', 'violet'
    ]

    window.continue_on_mouse_click('Click anywhere in here to start')
    color_changes(window, rectangle, colors)

    # The  reverse  method reverses its list IN PLACE
    # (i.e., it "mutates" its list -- more on that in future sessions).
    colors.reverse()

    window.continue_on_mouse_click()

    color_changes(window, ellipse, colors)
    window.close_on_mouse_click()
示例#2
0
class ShapesTest(unittest.TestCase):
    """
    Runs tests for the Shapes specified in the class variable
      shapes
    below.
    """
    shapes = (rg.Rectangle(rg.Point(50, 100), rg.Point(70, 140)),
              rg.Ellipse(rg.Point(50, 100),
                         rg.Point(70, 140)), rg.Circle(rg.Point(50, 100), 30),
              rg.Square(rg.Point(50, 100), 30), rg.Point(50, 100),
              rg.Line(rg.Point(50, 100), rg.Point(70, 140)))
    speed = 10

    def test(self):
        testcase = ShapeTest()
        testcase.speed = ShapesTest.speed
        for shape in ShapesTest.shapes:
            testcase.start_shape = shape
            testcase.test()
示例#3
0
def main():
    """
    Uses ROSEGRAPHICS to demonstrate:
      -- CONSTRUCTING objects,
      -- applying METHODS to them, and
      -- accessing their DATA via INSTANCE VARIABLES
    """
    example1()
    example2()
    example3()
    window = rg.RoseWindow()
    point3 = rg.Point(200, 300)
    point3.attach_to(window)

    point4 = rg.Point(200,200)
    circle = rg.Circle(point4, 50)
    circle.attach_to(window)

    rg.Square(point3,10)
    rg.Ellipse(point3,point4)

    window.render()
    window.close_on_mouse_click()
示例#4
0
def run_test_fill_from_colors():
    """ Tests the   fill_from_colors   function. """
    print('--------------------------------------------------')
    print('Testing the   fill_from_colors   function:')
    print('See the two graphics windows that pop up.')
    print('--------------------------------------------------')

    # ------------------------------------------------------------------
    # Test 1: Flashes red, white, blue -- 5 times.
    # ------------------------------------------------------------------
    title = 'Red, white and blue, repeated 5 times!'
    window = rg.RoseWindow(400, 180, title, canvas_color='dark gray')

    circle = rg.Circle(rg.Point(150, 100), 40)
    circle.attach_to(window.initial_canvas)

    number_of_cycles = 5
    window.continue_on_mouse_click('Click anywhere in here to start')

    for _ in range(number_of_cycles):
        fill_from_colors(window, circle, ['red', 'white', 'blue'])

    window.close_on_mouse_click()

    # ------------------------------------------------------------------
    # Test 2: Flashes through a bunch of colors, looping through the
    # list forwards in a rectangle, then backwards in an ellipse.
    # ------------------------------------------------------------------
    colors = [
        'red', 'white', 'blue', 'chartreuse', 'chocolate', 'DodgerBlue',
        'LightPink', 'maroon', 'yellow', 'green', 'SteelBlue', 'black'
    ]

    title = 'Loop through 12 colors, forwards then backwards!'
    window = rg.RoseWindow(450, 250, title, canvas_color='yellow')

    rect_width = 100
    rect_height = 40
    rect_center = rg.Point(125, 100)
    rectangle = rg.Rectangle(
        rg.Point(rect_center.x - (rect_width / 2),
                 rect_center.y - (rect_height / 2)),
        rg.Point(rect_center.x + (rect_width / 2),
                 rect_center.y + (rect_height / 2)))

    oval_width = 70
    oval_height = 160
    oval_center = rg.Point(300, 100)
    ellipse = rg.Ellipse(
        rg.Point(oval_center.x - (oval_width / 2),
                 oval_center.y - (oval_height / 2)),
        rg.Point(oval_center.x + (oval_width / 2),
                 oval_center.y + (oval_height / 2)))

    rectangle.attach_to(window)
    ellipse.attach_to(window)
    window.render()
    window.continue_on_mouse_click('Click anywhere in here to start')

    # This function call iterates through the colors,
    # filling the rectangle with those colors:
    fill_from_colors(window, rectangle, colors)

    # The  reverse  method reverses its list IN PLACE
    # (i.e., it "mutates" its list -- more on that in future sessions).
    colors.reverse()

    window.continue_on_mouse_click()

    # This function call iterates through the colors,
    # filling the ellipse (oval) with those colors:
    fill_from_colors(window, ellipse, colors)

    window.close_on_mouse_click()
示例#5
0
def draw_ellipses(width, height, n):
    """
    See   ellipses.pdf   in this project for pictures that may
    help you better understand the following specification:

    Constructs a rg.RoseWindow and draws 3 SETS of rg.Ellipse objects
    on it, where each SET contains   n   rg.Ellipse objects
    (for the given parameter n).

    The parameters specify:
      -- the width and height, respectively, of each rg.Ellipse
           (all dimensions are in pixels)
      -- the number of rg.Ellipse objects to draw in EACH
           of the three sets.

    The first set of rg.Ellipse objects is a COLUMN on the LEFT side
    of the window, with each rg.Ellipse barely touching the previous one.
    These rg.Ellipse objects should be filled 'blue'.

    The second set of rg.Ellipse objects is a COLUMN on the RIGHT side
    of the window, with each rg.Ellipse barely touching the previous one.
    These rg.Ellipse objects should be filled 'green'.

    The third set of rg.Ellipse objects is a DIAGONAL from the
    UPPER-RIGHT side of the window toward the LOWER-LEFT side
    of the window, with each rg.Ellipse INTERSECTING the previous one,
    with nice "even" intersections.  (The exact amount of overlap is up
    to you.  One possibility is to move each oval by half of its width
    and half of its height. That's what I did in the attached pictures.)
    These rg.Ellipse objects should be filled 'red'.

    It is OK if the third set overlaps the first and/or second set
    somewhat.  The third set has to go 'down and to the left', but any
    reasonable choice for how much down and how much to the left is OK.

    Pauses after drawing, waiting for the user to click the mouse
    in the window, then closes the window.
    """
    # TODO: 2b. Implement and test this function.

    width_g = width * n
    height_g = height * n
    windows = rg.RoseWindow(width_g, height_g)

    for k in range(1, n + 1):

        center1x = width * 0.5
        center1y = height_g - (height * (2 * k - 1) / 2)
        point1 = rg.Point(center1x, center1y)
        elli = rg.Ellipse(point1, width, height)
        elli.fill_color = 'violet'
        elli.attach_to(windows.initial_canvas)

        center2x = width_g - (width * 0.5)
        center2y = height_g - (height * (2 * k - 1) / 2)
        point2 = rg.Point(center2x, center2y)
        elli = rg.Ellipse(point2, width, height)
        elli.fill_color = 'pink'
        elli.attach_to(windows.initial_canvas)

        center3x = width_g - (width * (1 + 0.5 * k))
        center3y = height * 0.5 * k
        point3 = rg.Point(center3x, center3y)
        elli = rg.Ellipse(point3, width, height)
        elli.fill_color = 'light blue'
        elli.attach_to(windows.initial_canvas)

    windows.render()
    windows.close_on_mouse_click()
#window=rg.RoseWindow(500,500)
#     c. What is the default height of a RoseWindow?
#        (Use the HOVER trick to determine the answer to this question.)
#            300
#
#     d. Write a line of code that construct a RoseWindow object
#        whose height is 100:  (Use the HOVER trick to figure it out)
#           window_name = rg.RoseWindow(400,100)
#
#     e. Use the DOT trick to answer the following:
#
#          -- Write the names of two types of graphics objects that
#             you can construct OTHER than Circle and Point:
windo=rg.RoseWindow(800,600)
for k in range(150):
    oval = rg.Ellipse(rg.Point(250-k,250+k), rg.Point(450-k,350-k))
    oval.fill_color = 'green'
    oval.attach_to(windo)
    polly=rg.Line(rg.Point(12+(k),200-k),rg.Point(200-(k*1),12+(k*2)))
    polly.fill_color='red'
    polly.attach_to(windo)
    windo.render(0.01)
    oval.detach_from(windo)
    polly.detach_from(windo)
#
#          -- Write the names of three METHODs that Circle objects have:
#                WRITE_YOUR_ANSWER_HERE,_REPLACING_THIS
#
#          -- Write the names of three INSTANCE VARIABLEs that Circle
#             objects have:
#                WRITE_YOUR_ANSWER_HERE,_REPLACING_THIS