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
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)