CMSC110 (Introduction to Computing)
Assignment #2
Due by 10:10 am on September 19, 2011
Task: Design an interactive sketch that does the
following:
- It should have a background generated using randomness and
iteration (for or while loops).
- For example, you might draw a nighttime sky filled with
randomly colored and sized stars over a green field with
random blades of grass, or a tessellated background with
random colors.
- Each time the user presses a key, the program should erase
whatever is displayed and redraw the random background.
The background should be different with each subsequent
keypress.
- As the user clicks on the sketch, it should draw an object at
that location. Something about the physical shape of this
object must change based on its location.
- For example, the size of a house could increase as the user
clicks closer to the center of the sketch, or a smiley face's
eyes could enlarge and its mouth grin depending on where it
was placed.
- After several clicks, your sketch should have multiple
copies of the object at different locations on the screen,
each varying slightly based on the mouse position.
- You could even draw multiple different types of objects,
depending on the mouse location.
Somewhere within your sketch, you must use (1) variables, (2)
conditional if or if-else statements, and (3) for or while loop
iteration.
Other hints:
- Make sure that it is at least 500 pixels by 500 pixels.
- Your objects can be simple. For example, clouds could be
made of multiple ellipses, flowers could be made with nested
colored ellipses and a green rectangle.
- Your code should use variables as appropriate.
- Include proper sketch source code header and adequate
comments. (See Course Coding
Standards).
- We recommend the following steps to develop your
program. After each step, you should run and test your
program thoroughly, ensuring that it works.
- Begin by randomly generating the background. Develop
this slowly, building up complexity as you go. Test it
to make sure it works after every few lines of code.
- Add in the keyPressed() method that clears the
background. Test that it works.
- Add in the mousePressed() method to draw a simple shape
(e.g., a single ellipse) at the mouse location upon a
click. Test that you can draw multiple objects and
randomly regenerate the background.
- Change the simple shape to the more complex looking object
you want to draw. Test your code.
- Modify your complex object to adjust some aspect of its
appearance (i.e. shape or size) based on the mouse
location. Test your completed program.
- Do not try to write the entire program at once. Trying to do
everything at once and expecting it to work is the pathway to
frustration. Instead, build your program up slowly,
testing as you go, you'll ensure that you always have a working
program (and if it doesn't work, the error was in the small bit
of code you just wrote).
Use the following structure for your program.
// Header comments
// Declare variables
void setup() {
// Set up the drawing.
// Call the drawBackground() method
}
void draw() { /* remains empty */ }
void mousePressed() {
// Use the mouseX, mouseY position to change the physical shape of your object
// Draw the object
}
void keyPressed() {
// Call the drawBackground() method
}
// clear the entire image and redraw the background
void drawBackground() {
// Draw a background that contains random elements and repetition using a for loop
}
Requirements:
- Generated background using randomness and for/while loop
iteration
- Pressing a key clears the sketch and redraws a new background
- On mousePressed, different types of objects are drawn on the
sketch at the mouse location
- The physical shape of the object (size, shape, etc.) changes
based on the mouse location
- Conditional if or if-else
tests are used somewhere in the assignment
- Variables used appropriately in the remainder of the
assignment
- Includes proper header and adequate comments
Carefully read the Assignment
Submission Policy for how to submit your assignment.