CS 115 Lab 8, Part A: Draw national flags

[Back to lab instructions]


Circles: Japan and Bangladesh

  1. In IDLE, create a new Python source code file named lab08a.py:
    """
    Program: CS 115 Lab 8a
    Author: Your name
    Description: This program displays national flags.
    """
    
    from graphics import *
    
    
    def draw_japan_flag(flag_width):
        ''' Draws the national flag of Japan in a graphics window.
    
        Parameter: the width of the window
        Returns: nothing
        '''
        flag_height = 2 / 3 * flag_width
        circle_diameter = 3 / 5 * flag_height
    
        # Open a new graphics window with the title 'Japanese flag',
        # the width passed by the caller, and the calculated height
        win = GraphWin('Japanese flag', flag_width, flag_height)
    
        # Set the window background to white
        win.setBackground('white')
    
        # Set up the red circle.
        flag_center = Point(flag_width / 2, flag_height / 2)
        circle_radius = circle_diameter / 2
    
        # Create a circle that is centered in the middle of the flag
        # and has the specified radius
        circ = Circle(flag_center, circle_radius)
    
        # Turn that circle red
        circ.setFill('red')     # the inside of the circle
        circ.setOutline('red')  # the line around the circle
    
        # Actually draw the circle
        circ.draw(win)
    
        # Close?
        input('Press ENTER to close the flag window.')
        win.close()
    
    
    # --- Our main function ---
    def main():
        draw_japan_flag(600)  # Draw a Japanese flag with a width of 600 pixels
    
    
    main()
  2. Run your program. You should see a graphics window pop up with the Japanese flag:

    Japan national flag
  3. Read through the code for the draw_japan_flag() function. Try to understand all of the comments.
  4. In main, change the 600 to other values (200, 800, etc.), run the program, and see what changes.
  5. Answer Question 1 in your writeup.
  6. Over the next few steps, you will write a new function called draw_bangladesh_flag. This function will draw the flag of Bangladesh:

    Bangladesh national flag
  7. Since the Bangladeshi flag is similar to the Japanese flag, you can use your old code as a basis for your new code. Just below your docstring and import statement, make a new copy of your ENTIRE draw_japan_flag function. Rename this copy to draw_bangladesh_flag. Here is what the structure of your code should look like:
    """
    This is just the STRUCTURE of your code. Don't copy this into your program!
    """
    import statements
    
    
    def draw_japan_flag(flag_width):
        code for Japanese flag
    
    
    def draw_bangladesh_flag(flag_width):
        code for Japanese flag (for now)
    
    
    def main():
        draw_japan_flag(600)
    
    
    main()
  8. Here are the specifications for the Bangladesh flag, according to Wikipedia:
  9. Go through the draw_bangladesh_flag function and change it to meet the above specification.
  10. Inside your def main():, comment out the call to draw_japan_flag, and add the following line:
    draw_bangladesh_flag(600)
  11. Run your program. Verify that it draws the Bangladeshi flag. Change the 600 to other values, rerun your program, and be sure that the flag resizes correctly.

Rectangles: France and Russia

  1. Next, we'll draw a flag that has stripes instead of a circle. We'll start with the national flag of France:

    France national flag
  2. To draw the stripes, we will draw three rectangles. To draw a rectangle, we have to specify the coordinates of two opposite corners.
  3. Insert the following code at the top of your program, just below the import statement. The def lines should be against the left margin, just like with the other two flag functions.
    def draw_stripe(window, top_left, bottom_right, color):
        '''
        Draws a rectangle in the window
        Parameters:
        - window: the window to draw in
        - top_left: the coordinates of the top left corner (as a Point)
        - bottom_right: the coordinates of the bottom right corner (as a Point)
        - color: the color to make the rectangle (as a string)
    
        Returns: nothing
        '''
        stripe = Rectangle(top_left, bottom_right)
        stripe.setFill(color)
        stripe.setOutline(color)
        stripe.draw(window)
    
    
    def draw_france_flag(flag_width):
        '''
        Draws a French flag in the graphics window.
        Parameter: the width of the window
        Returns: nothing
        '''
    
        flag_height = 2 / 3 * flag_width
        stripe_colors = ['DarkBlue', 'white', 'red']
        stripe_width = flag_width / len(stripe_colors)
    
        # Open a new graphics window with the title 'French flag', the
        # width provided by the user, and the calculated height
        window = GraphWin('French flag', flag_width, flag_height)
    
        # Draw the blue stripe
        # The top left of the stripe is the top left of the window
        blue_stripe_top_left = Point(0 * stripe_width, 0)
    
        # The bottom right of the stripe is 1/3 of the way across
        # the flag, and all the way to the bottom.
        blue_stripe_bottom_right = Point(stripe_width, flag_height)
        draw_stripe(window, blue_stripe_top_left, blue_stripe_bottom_right, stripe_colors[0])
    
        ######### Write similar code for the white and red stripes.
    
        # Close?
        input('Press ENTER to close the flag window.')
        window.close()
  4. In main, comment out the draw_bangladesh_flag(600) line and add the line
    draw_france_flag(600)
  5. Run your program and verify that you see a dark blue stripe.
  6. Answer Question 2 in your writeup. You are STRONGLY encouraged to get out a pencil and paper to answer this question.
  7. Based on the code for the dark blue stripe, write code to draw the white stripe (even though the background may already be white) and the red stripe.
  8. Answer Question 3 in your writeup.
  9. Based on your answers to Question 3, rewrite your draw_france_flag code to draw the stripes in a loop. The loop should look something like this:
    for i in range(...something...):
        stripe_top_left = Point(...something...)
        stripe_bottom_right = Point(...something...)
        draw_stripe(...4 things...)
  10. Next, you will draw the Russian flag. To do so, make a new copy of the draw_france_flag() function, and rename it to draw_russia_flag(). You will adapt this program to draw the national flag of Russia:

    Russia national flag
  11. Here are the specifications for the Russian flag:
  12. Answer Question 4 in your writeup.
  13. Revise the draw_russia_flag() function to draw the Russian flag. You should NOT need to change the definition of draw_stripe().
  14. In main, test your code by calling the draw_russia_flag() function and passing it different values of the window width.

Other Polygons: Sudan

  1. Make a copy of your draw_russia_flag() function, and name the copy draw_sudan_flag().
  2. Modify your main() function to call draw_sudan_flag.
  3. Over the next few steps, you will modify this function to actually draw the national flag of Sudan:

    Sudanese national flag

  4. Go through the definition of draw_sudan_flag and replace the mentions of Russia with Sudan.
  5. Modify your code so that the height of the Sudanese flag is 1/2 of the width.
  6. Use 'red', 'white', and 'black' for the stripe colors.
  7. The triangle is an isosceles triangle (2 equal sides) and extends 1/3 of the way across the flag. Answer Question 5 in your writeup.
  8. You can use the Polygon function to draw the triangle. To use the Polygon function, just pass it Point objects for each vertex of the triangle. The graphics library will connect the vertices in the order listed, plus one more connection between the last point and the first point. For example,
    triangle = Polygon(Point(50, 50), Point(100, 100), Point(25, 100))
    draws a triangle with vertices at (50, 50), (100, 100), and (25, 100).
  9. Write code to draw the Sudanese flag's triangle using Polygon. Use 'DarkGreen' for the color.
  10. Continue to Part B.