Homework 17: Robot Army!
Concepts
Focus on a few main concepts for this assignment: appreciating libraries, encapsulating "units of work" with functions, and using loops to call functions many times (with different arguments).
Appreciating Libraries
Have you ever stopped to think about your shoes? Yes, your shoes. Someone, somewhere, took the time to assemble and produce your "kicks," yet it's likely that you rarely think of them while you walk to class or your friend's house.
As software engineers, we stand upon the shoulders of giants. A key concept in programming that is almost always overlooked is appreciating libraries. If you were writing a program that calculated trajectories of edible objects flung from a huge slingshot, think about how you would have to implement functions that calculate sine, cosine, secant, tangent and so on. Similarly, think about the work involved in reading and writing files, or decoding multimedia like photos, images and videos.
Someone, somewhere has gone through the trouble of implementing libraries and has shared them with you such that you can leverage their work to solve greater and more interesting engineering problems. The next time you go for a walk in your favorite kicks, think about the person that made your shoes; the next time you use a library function, think about that code's creator(s) as well.
Instructions
Now that you've learned how to define functions, use functions and have had a chance to experiment with OpenFrameworks to draw graphics, it's time to test your mettle: can you leverage loops and functions together to create a robot army? Robot armies are handy when you have a certain message you'd like to assert and enforce, such as "No more homework or else!"
Your goal for this assignment is to draw a screen that looks like this:
While you could certainly draw this manually, typing things like ofCircle()
a bazillion times, that would be a tragic waste of computing power and your robot army would be sad, sad, sad. Instead, stop and ask yourself:
What if there was a function called drawRobot
that could draw just one robot?
Could I then use some loops to call drawRobot
, passing it a different location to draw the robot each time it is called?
Hints & Recommended Steps
Note that this assignment will require some experimentation to get right. To help save you time, we suggest following the steps described below. If you are not typing in the snippets of code presented below, you are "doing things wrong."
Step 1: Draw One Robot
First, realize that your robot for this assignment should be simple, and look like this (colors are up to you):
Using Visual Studio, take a look at the file called testApp.cpp
. Notice that below testApp::setup
we have provided an empty function for you, called drawRobot
. Notice how it expects two parameters called baseX
and baseY
. For your first step in this assignment, you should:
- Add one line to the
draw
function, which is a call todrawRobot
. For example,drawRobot(100, 100);
- Implement your
drawRobot
function using functions from the OpenFrameworks library, such asofSetColor
,ofCircle
andofRect
.
However, when you call functions like ofCircle
and ofRect
, be sure to pass it arguments incorporating baseX
and baseY
. For example, you might start with:
ofSetColor(255, 153, 153);
ofCircle(baseX, baseY, 30);
}
Continue drawing your simple robot using appropriate graphics functions and passing them arguments in terms of baseX
and baseY
. You will need to experiment with this! For example, try the following:
Hmmm, that doesn't look quite right, does it? What if you tried this:
Now what do you see? Keep experimenting with using baseX
and baseY
as part of your function arguments until you can draw one happy (or content) robot.
Step 2: Draw One Row of Robots
You should notice that we have a repetitive task to accomplish: drawing tons of robots. What better mechanism to handle this than a loop? Experiment with the following code:
ofCircle(x, 30, 15);
}
What do you see? Holy smokes, a row of glamorous circles! Notice what is happening. A loop is running, in which x
changes from 0 to 750 in steps of 50. For each iteration of the loop, we're calling ofCircle
, passing it the value of x
.
Now, assuming you've got your drawRobot
function complete, try replacing the call of ofCircle
to drawRobot(x, 30)
and see what you get.
Step 3: Draw Many Rows of Robots
Now that you can draw a row of robots with a loop, do you have an idea of how to draw many rows of robots? "No, no idea," you say? What you need is a mind-bender of a construct: two nested for
loops. Try this out:
for (int y = 0; y < 800; y += 50) {
ofCircle(x, y, 15);
}
}
What do you see? Holy smokes, row after row of glamorous circles! Notice what is happening. A loop is running, in which x
changes from 0 to 750 in steps of 50. For each iteration of the loop, another loop executes, in which y
continually changes. So for every value of x
we also have a bunch of different values of y
. Furthermore, we're calling ofCircle
and passing it the different values of x
's and y
's.
Now try replacing the call of ofCircle
to drawRobot(x, y)
and see what you get.
Requirements and Rubric
This work is worth 55 points.
Requirement | Points | Notes |
---|---|---|
Place your name in the comment header in main.cpp | 3 | |
Correct submission of src directory as a .zip file. | 2 | |
drawRobot correctly draws a "robot" |
10 | |
drawRobot correctly uses baseX and baseY |
10 | |
draw calls drawRobot |
10 | |
draw implements nested for loops |
20 |
Concepts Exercised: libraries, functions, loops, graphics, fun, abstraction
© 2011 Yong Joseph Bakos.