CMU 15-112: Fundamentals of Programming and Computer Science
Week 2: Graphics


  • To start:
    1. Download 3. graphicsPractice.py to the folder named 'Week2'.
    2. Don't forget to copy cmu_graphics library to the same folder. to the folder named 'Week2'.
    3. Edit the downloaded file using your preferred editor

  • Code Tracing
    What will this code print? Figure it out by hand, then run the code to confirm. Then slightly edit the code and try again.


    Fill in the blanks

    Free Response (Problem-Solving)

    1. drawFlags
      Google the flags of the following countries and use CMU graphics to draw at least two of them.
      • Belgium
      • England
      • Switzerland
      • Palestine
      • Japan

    2. drawClock
      Draw a clock with colours of your choice that shows the current time on your system.

    3. drawAbacusRow
    4. Write the function drawAbacusRow(app, rowStr), which draws a set of 10 horizontally aligned circles that represent a row of sliding beads in an Abacus. The length of the row is equal to 12 times the diameter, which means there are always two empty slots in the row. Here’s one example:

      The function receives as input a string rowStr that specifies the beads’ color and positions. The string starts with the color name and ends with 12 characters. Each character of the last twelve refers to one slot and specifies whether there’s a bead or not in the specific slot in the row: a 0 indicates that there is a bead, and a- indicates that there is no bead in the corresponding slot. For instance, calling drawAbacusRow(app, 'red000000--0000')produces the graphics above. You can assume that app.width > app.height. The row should span the entire width of the window. Your code should be able to handle window resizes. A line depicting the rod should be seen in the empty slots. You can assume that cmu_graphics is already imported and there a function redrawAll(app) that will call your function. Here’s another example calling drawAbacusRow(app, 'lightBlue00-00000000-')


    5. drawRGBTarget
    6. Write the function drawRGBTarget(canvas, width, height, n), which draws a target with n concentric circles with alternating colors (red, green, blue) and the label RGB as shown below:

      In this example, the number of concentric circles is 10. You can assume that the canvas has equal width and height. The biggest circle should span the entire canvas and always be red. The font should be bold, and its size should equal the radius of the smallest circle. Your code should be able to handle window resizes. As usual, this question will have partial credit. Here’s another example with n = 6:



    7. drawNiceFlower
    8. Write the function drawNiceFlower(app, n), which draws a nice flower using ('pink')-colored ovals and a ('lightCyan')-colored circle centered in the flower. The function takes as an input an integer n representing the number of ovals that are used to draw the flower. Consider the following two examples:

      You can assume that app.width==app.height. The ovals and the circle should be centered on the canvas. The oval’s height should be 2/3 of the canvas height and its width should be 1/3 of the canvas width. The circle should fit inside the last oval. You can assume that cmu_graphics is already imported and there is a function redrawAll(app) that will call your function.
      Hint: Remember that shapes can be rotated using an argument to the function that draws the shape.

    9. drawTicTacToe
    10. Write the function drawTicTacToe(app, s), which draws a 3x3 TicTacToe board game with Xs and Os. The function takes as an input a string s that specifies the content of the nine board cells. Each character of s refers to one cell and specifies whether it has X ('x'), O ('o'), or Nothing ('_'). The first character in s corresponds to the first cell which is the top-left one and the last character corresponds to the last cell which is the right-bottom one. Consider the following two examples:

      YYou can assume that app.width==app.height. The nine cells should be drawn to span the entire canvas. Your code should be able to handle window resize. To simplify things for you, feel free to draw the Xs and Os as text instead of drawing them manually. You can assume that cmu_graphics is already imported and there is a function redrawAll(app) that will call your function.