B CUSP 110B – Digital Thinking

Exercise 5: Variables and Soccer Motion

 

 

Variables and Soccer Motion

In the previous lectures we have discussed how to provide arguments to parameterize the creation of circles, such that the created circles can appear different.



In Exercise 4, we practiced with creating and then changing the parameters of the two paddle rectangles of a simple pong setup:

Goals: In this exercise, we will investigate how to define variables as parameters for describing the motion of the soccer ball. In particular, we will experience with:

·         Declaring and initializing variables representing the motion of the soccer ball.

·         Randomizing the soccer’s motion based on user input

·         Randomizing the soccer’s initial position based on user input

Please work with the person next to you, start with this source code.

Step 1: Examine the variable speedX

Declared:
 

Initialized to 0.5f in InitializedWorld():

Updates the myCircle.CenterX in UpdateWorld()

Notice when you compile and run the given source code, the soccer ball moves towards the right at a constant speed. Discuss with your partner and explain to yourselves what is causing the horizontal movement?

 

Step 2: Discuss how you can declare/initialize a variable to change the vertical speed of myCircle. Hint: by changing myCircle.CenterY.

·         Now, implement your solution for moving myCircle in both the X and Y directions.

·         Can you move myCircle in X and Y in different speed? (e.g., faster in X and slower in Y)?

·         Can you move myCircle in the negative X direction?

 

Step 3: Remember the right thumbStick:

Recall that the right thumbstick is mapped to the arrow keys and gives a value of -1f to 1f depending on which of the arrows are typed. Also remember the “+=” operator accumulates value of a given variable, where
            myVariable += GamePad.ThumbSticks.Right.X;
will change myVariable’s value according to the left/right arrow keys.

Now, discuss with your partner on how you can change the speed of myCircle with the right ThumbStick. Now implement your solution.

 

Step 4: If there is time (optional): remember GamePad.ButtonA is mapped to the “K” key, and we know how to test if the “K” key is pressed:

also remember the RandomFloat(min, max) function that returns a random number between min and max. Now, discuss how you can randomly change the speed of myCircle between -2 and +2 in both the X and Y directions whenever the player presses the “K” key. Implement your solution.

To Turn In
Print out and submit your ClassExample.cs (remember to write your names). Also submit a version to the class submission site.

 

Wrap Up

In this exercise you have parameterized the speed of myCircle. You have experienced with changing the arguments to myCircle’s speed parameters by the right thumbstick (Step-3), and by allowing the player to randomly assigning values of the arguments (Step-4).