INQ241A
Mobile Apps

Activity 5

Functions

  1. Create a new script with the function draw square(size: Number) that draws a filled in square. For example, ▹draw square(200) should produce a drawing like the following:

    filled_square.png
    The parameter size specifies the size of the square. So when the function is called with ▹draw square(200) it should draw a square that is twice as large as when it is called with ▹draw square(100). Test your code to make sure this is the case.

    Note that TouchDevelop turtle graphics can not draw filled in shapes. You can, however, change the thickness of the line that the turtle draws. So to make a square use the ♻turtle → set pen size to make the pen as wide as the square and then use the ♻turtle → forward method to move the turtel forward the size of the square. The result will be a line that is as long as it is thick, which will look like a square.

  2. Modify draw square function you created so that it has the parameters draw square(size: Number, x: Number, y: Number, color: Color. The additional parameters specify the location of the center of the square and the color of the square. For example, the following code:

    function main()
      var x = 200
      var y = 200
      ▹draw square(100, x, y, colors → red)
      ▹draw square(50, x, y, colors → blue)
    end function

    would draw two squares at the same location, (200, 200), but the smaller blue square is on top of the larger red square like the following:

    filled_squares.png
  3. Modify the main function so that it uses the draw square function you created to draw a heart like the following:

    square_heart.png
    Note that drawing all of the squares that make up the heart individually is quite a few calls to draw square. You can reduce the number of calls by just drawing one large square for all of the red squares and then drawing all of the black squares on top like the following:

    red_square.png + black_heart.png = red_square_and_black_heart.png
    Note that this will leave two errant red squares outside of the heart. Just draw white squares on top of them to erase them.

Submission

Please show your source code and run your scripts for the instructor or lab assistant.