def create_object():
    """
    Creates the original DVD Logo. Code from here will be moved into
    the new class that you are creating.
    """

    width = 200
    height = 200

    obj = GCompound()

    cube = GRect(0, 0, width, height)
    cube.set_filled(True)
    cube.set_color(random_color())
    obj.add(cube)

    dvdtext = GLabel("DVD")
    dvdtext.set_font("bold 60px 'serif'")
    obj.add(dvdtext, width / 2 - dvdtext.get_width() / 2, height / 2 - 10)

    vidtext = GLabel("video")
    vidtext.set_font("bold 50px 'serif'")
    vidtext.set_color("white")
    obj.add(vidtext, width / 2 - vidtext.get_width() / 2,
            height / 2 + vidtext.get_ascent())

    return obj
示例#2
0
 def __init__(self, text, fn=None):
     GCompound.__init__(self)
     label = GLabel(text)
     label.set_font(self.BUTTON_FONT)
     width = max(self.BUTTON_MIN_WIDTH,
                 2 * self.BUTTON_MARGIN + label.get_width())
     frame = GRect(width, self.BUTTON_DEFAULT_HEIGHT)
     frame.set_filled(True)
     frame.set_fill_color("White")
     self.add(frame)
     self.add(label)
     self.text = text
     self.label = label
     self.frame = frame
     self.fn = fn
     self._recenter()
示例#3
0
def estimate_pi(tries):
    """
    Uses random dart throws at a circular target to approximate
    the value of pi. As the number of throws gets large, pi should
    be approximately 4 times the fraction of throws that hit the
    circle.
    """
    def take_shot():
        """
        Simulates a random "throw" toward the target, and draws
        a circle of the correct color depending on if the
        throw struck the target or not.
        """
        x = random.random() * size
        y = random.random() * size
        is_hit = (x - radius)**2 + (y - radius)**2 < radius
        if is_hit:
            color = "red"
            n_hits += 1
        else:
            color = "black"
        gw.add(create_filled_circle(x, y, 1, color))

    size = 500
    radius = size / 2

    # Creates the window and adds the circular target
    gw = GWindow(size, size)
    gw.add(create_filled_circle(radius, radius, radius, "blue"))

    # Simulate tries number of throws
    num_hits = 0
    for i in range(tries):
        take_shot()

    # Compute pi
    pi = num_hits / tries * 4

    # Display the rounded value of pi centered pretty in window
    lab = GLabel(str(round(pi, 2)))
    lab.set_font("bold 100px 'serif'")
    lab.set_color("white")
    x = size / 2 - lab.get_width() / 2
    y = size / 2 - lab.get_ascent() / 2
    gw.add(lab, x, y)