B CUSP 110C – Digital Thinking

Homework 9a: Practice with Functions (again) And Some Loops

 

 

 

Goal: To get practice writing functions, using parameters, and start experiencing with loops.

 

Instead of working with owl, this time we will work with balls trapped inside a box. Find the file Homework9a.TrappedBall.txt with this assignment. This given code draws a red ball trying to breakout from a white box. Notice that the box is a 50 x 50 figure.

 

Part 1: Add Parameter. Change the trappedBall( )  function so that it accepts one more input parameter, namely a variable of type float, the size for the red ball. A legitimate size input could be, say, 10.0 which would change the radius of the ellipse() function that draws the ball.

 

 

Part 2: Add Function. Write a new function with the following specification:

Name: oneRandomFloat

Parameters: none

Returned type: float

Returned value:  random(2, 10)

 

The returned value will create a random floating point number between 2 and 10. Recall that the Processing function random( ) returns an unpredictable value every time it is called. In this case, we call the random( ) function with two arguments, telling the function to return a random number between 2 and 10! The oneRandomFloat() function is only one line long.

 

Part 3: Random ball size. Instead of calling trappedBall( ) with size 10, call trappedBall( ) and draw the red ball with oneRandomFloat() as its size. Now draw two trappedBalls, one at (25, 50), and the second one at (375, 350). Notice the two trappedBalls have different size red balls inside them. Here is an example of what your program may look like.

 

Part 4: Random ball color. Instead of always drawing the ball in the white box in red color, re-implement the pick() function from Homework 9 and use the pick() function to randomize the ball color. Here is an example of what your program may look like.

 

Part 5: A row of trappedBalls.

Make a function to draw a row of 8 trappedBalls each with random ball size and ball color. The trappedBalls should be in a row, and they should be tight together. Call your function aRowOfTrappedBalls( ). It should have one parameter:

y an integer, which is the y position of the left most trappedBall.

You must use a for loop to draw the 8 trappedBalls. Here is an example of what your program may look like.

 

Part 6: all balls are trapped. Write a for loop (in the draw() function) to call aRowOfTrappedBalls( ) to fill the entire drawing window with trappedBalls. Once again, you must use a for loop to complete this part of the assignment. Here is an example of what your program may look like.

 

Part 7: Challenge (Optional, 3pt extra credit): Modify the trappedBall() function to draw n numbers of trapped balls in the white box instead of just one. Where

            float n = random(2, 6);  // n is a random number between 2 to 6

Here is an example of what your program may look like.

 

 

Remember to comment your program.

 

Wrap Up: You have practiced writing functions, calling functions, working with parameters, and writing for loops. These skills will be used throughout your computational thinking.

 

Turn in: Save your source code .pde file, e-Submit to class dropbox and bring hardcopy to submit at the beginning of class.