B CUSP 110B – Digital Thinking

Homework 10: Functions

 

 

 

Goal: The purpose of this exercise is for you to experiment with defining and making function calls.

 

Here is a Star Pong game that we will be developing (download, unzip, and double click on the .exe file). Notice:

·         The behavior is identical to the bullseye pong game we have gone through in class.

·         Your job is to replace the bullseye with a Star!

 

Investigate Given Source

You will begin with the source code from the lecture on functions: download, unzip, compile and run. Now, run the program to investigate its behavior. Remember what we have learned when examine a given source code:

·         Find/read/understand the declaration of all variables

·         Find/read/understand the initialization of the variables: InitializeWorld()

·         Find/read/understand how the variables are updated: UpdateWorld()

 

 

Step 1: Creating the “Star:”

Notice,

·         The middle “soccer” is “on top” of 4 surrounding smaller circles

·         Soccer radius is 20

·         Surrounding smaller circles (use any color you like) has radii of 10, the centers of the smaller circles touches the circumference of the soccer, so

o   topSmallCircle.CenterX = soccer.CenterX+20

o   topSmallCircle.CenterY = soccer.CenterY;

 

Now, define a new function:

 

And call your function from InitializeWorld() to create a “Star.”

 

 

Step 2: Move the Star by speedX, and speedY. Implement and call the function

To move the entire Star by speedX and SpeedY at every update.

 

 

 

Step 3: Collide the Star with the paddles. Implement and call the function

Such that you can call this function with the left/right paddles and determine collision status. Notice, the actually collision is between the paddle and the surrounding circles on the star and not the center soccer ball. Now call this function to “bounce” the star off the left and right paddles.

 

 

Step 4: Reset the Star to an arbitrary location. Implement the function:

 

To reset the star to location (xPos, yPos), and speedX/Y to some arbitrary random numbers of your choice. You will reset the Star when: ß notice: these are different from the given source code!!

·         Button-A (K-Key) is clicked, reset the start to the center of the screen (600,300).

·         The soccer’s center falls outside the left/right boundaries, reset the start to: (150, 500)

 

 

To Turn In

Submit your ClassExample.cs file, bring a hardcopy (Remember to write your name!!) to class on next Wednesday to submit.

Wrap Up

In this assignment you have practice (yet again) that the steps to examine a given source code by:

·         Running the program to understand the behavior

·         Examine variable declarations

·         Examine the initialization of the variables

·         Understand how the variables are changed and relating the changes to what you see when running the programs.

·         New: this time with functions, what you have done is to read the UpdateWorld(), understand abstractly the behavior of the entire program, and then follow each function and understand what each function implements.

 

In addition, you have:

·         Practiced how to define functions based on given specifications

·         Invoked your function based on functional needs

·         Understand functions serve both as a way to organize you code (hide details), and as an abstract parameterization for reuse.

 

 

No need to panic!!

If you did not manage to complete HW10 by Wednesday, please learn the lesson that you need to start EARLY!! Notice you have missed both of my office hours (Monday _and_ Tuesday)! Well, in this case, no need to panic, please ask for help EARLY.

 

If you have spent tones of time to complete your assignment, well, don’t feel bitter … here is an opportunity for extra credit. Please refer to my solution (download, unzip, compile, run): to see: click the B-Button (L-Key) to change the star back into bullseye, and A-Button (K-Key) to return to star. This is what you do:

·         Define a function: ChangeToBullseye(), to

o   change the radius of all circles in star to corresponding ones for a bullseye

o   change the Center of the circles to those of a bullseye

o   make sure the soccer-texture circle has the smallest radius (why? Because it is on top!!)

·         Modify “ResetStarTo()” funciton such that the radius of the “small circles” are reset properly