Assignment 4

Due Friday February 8, 2013 at noon.

Submit a single zip file to cuLearn with all of your answers (1 file for the written part and 1 file for each Processing sketch).


Part 1: Write your answer in plain English. Put all of your answers in one single text file (.txt) or a single PDF file (.pdf).

Note: Please submit your written work in a single .txt or .pdf file. Your work will not be graded if it is not submitted in one of these two formats.


  1. Consider the following code fragment:

    int x = 3;
    int y = 4;
    int z = 2*x+y;
    while( y < 9 ){
       y = y + 1;
       z = z + y;
    }
    text(z, 10, 10);
    
    What value is displayed on the screen? (That is, what is the value of z at the end?)
  2. We have now seen the function mouseClicked() in class. Using this, along with the mouseX and mouseY variables, describe how you would make a "button" in the output window that toggles the background colour of the window between two colours. (When you press the mouse button AND the mouse is over the button you should toggle the background colour.) Your description should not be processing code, but it should be detailed enough that some could easily take your description and turn it into code.

    The button can simply be a rectangle or ellipse on the screen.

  3. Given an RGB colour, how can you decide if the colour is "blue"?


Part 2: Write a Processing sketch as an answer for each of the following questions or tasks, unless otherwise stated.

Note: For each question, add the processing folder with your sketch to your ZIP file, not just your sketch (.pde file). Label each folder/sketch as Question4, Question5, etc. Do not put a space between "Question" and the number.


Note: For all of your sketches, the output window should have width and height at least 200 (but do not make them extremely large).

Note: For this and the remaining assignments, it is expected that you WILL use setup() and draw() unless specifically asked not to.

  1. Write a program that draws 100 ellipses to the screen; each at a random position and with a random colour. Use the draw() loop for your repetition/looping. We want to the see the ellipses appear on the screen one at a time rather than have them all drawn at the same time.

  2. Write a program that draws three ellipses on the screen at random positions and with random colours (this is the initialization). Each of the ellipses should move in a random direction (horizontally, vertically, diagonally, etc) until it hits the one of the walls of the output window. Once an ellipse hits a wall it should just remain there. Each ellipse should move in a different direction and a different (random) speed.

    For full marks at least one of the ellipses must move in a direction that is not purely horizontal or vertical.

    The step lengths for the motion (which is essentially the speed of the motion) should not be too large. You want to see the ellipses moving not simply jump to the wall in one step (unless their initial position happens to be right next to a wall).

  3. Write a basic drawing program. As you move the mouse on the screen, its path should be drawn. It doesn't have to be fancy. You can simply draw a line from the current mouse position to the previous mouse position. (Note: mouseX, mouseY, pmouseX, and pmouseY will probably be useful.)

  4. Improve your basic drawing program so that you only draw when the mouse button is pressed down.

  5. Draw a 2x2 grid on the screen so that the screen is split into 4 equally sized sections. The lines that you draw for the grid should be reasonably thick so that it is very clear.

    When the mouse is in a given section of the grid, that section should have a different colour than the other three sections. When you move the mouse around, it should "turn on" the colour of a section when it is in the section and then "turn off" the colour when it leaves the section.

  6. Make a guessing game program. Draw two squares on the screen. One square should a big "A" written on it and the other should have a big "B" written on it. Your program will randomly choose one of the squares to be the winning square (and hence the other will be the losing square). When you click the mouse button over a given square, a message should appear on the screen telling you if you chose the winning or losing square.

  7. Modify your previous program so that the game keeps playing. A message should be displayed on the screen giving your number of wins (and also total number of games played). Each time you click the mouse on one of the two boxes you play the game once. The winning square should be random for each game.