B CUSP 110B – Digital Thinking Homework 7: Paddles
and Conditionals! |
|
Goal: The purpose of this exercise is to continue practice with parameters, and to begin a closer look at conditionals (if statements). We will spend a whole lot of time on conditional statements on Monday!
Here is the not so fun “pong game” we will be developing (download, unzip, and double click on the .exe file). Notice:
· The Pong ball has a random movement.
· Button-A (or K-key): resets the ball to the center of the game window, and gives the ball a new random speed.
· The Y-component of the Left ThumbStick (W, and S-Keys), and the Right ThumbStick (UP, and DOWN arrows) controls the vertical movements of the left and right paddles.
o Note: the X-Components of the Left (A, and D-Keys), and Right (LEFT, and RIGHT arrows) ThumbSticks do not change the x positions of the left and right paddles.
· The pong-ball bounces inside the game window!!
Download this source code, unzip, compile and run. Notice everything you type the “K” key, the ball resets its position to the lower left and gets a new speed that moves the ball towards upper-right.
As in all cases, the very first thing we do when receive any source code is to read and understand what we are given. Now, notice:
· Declaration of speedX and speedY
· InitializeWorld(): Initialization of speedX and Y to random speeds
· UpdateWorld():
o changing the center of the circle by speedX/Y
o Assigning lower left position, and new random speed when button-A (or K-key) is pressed.
Step 1: Change the Button-A (or K-key) reset behavior, when button-A (or K-key) is pressed,
· Circle position: instead of setting the circle position to (20f, 20f), set the circle position to (400f, 300f).
· Circle speed: instead of random numbers between 1f and 5f, set the speed to a random number between -5f and +5f.
Step 2: Controlling the left/right paddles with the left/right thumb sticks.
We understand that when we change the CenterX/CenterY of myCircle in UpdateWorld(), myCircle will “move around” accordingly in our game. Now, let’s try to apply this concept on the left and right paddles. Remember that in a pong game the player can only control the vertical (y) movement of the paddles. Here, we want to use the left and right thumbstick’s y-component to control the verticle movements of the paddles. Notice:
· Declaration of leftPaddle and rightPaddle:
· UpdateWorld(): type in this line of code:
· Now compile and run your program, try pressing the UP/DOWN keys, what happens?
Complete this step by adding in the code for controlling the left paddle.
Step 3: Now, examine how we check for the Button-A (K-key):
In particular, notice:
· The keyword “if”
· The left “(“ and right “)” that surround the “GamePad.ButtonAClick()” condition.
· The left “{“ and right “}” that surround all the statements inside the if statement.
· Together, the above says:
if (condition inside the parenthesis is true)
{
do everything inside the curly braces.
}
Now, let’s try to apply these observations/pattern in a new “if” statement. In UpdateWorld(), type in the following:
Now, compile and run your program. What happens when the circle center touches the right boundary? (Hint, keep on typing the “K” key until you see the ball moving towards right.) If we apply the concepts from the above, remembering the game window width is 800 units in length, we see that we have just typed in:
if (circle’s centerX is greater than the right boundary)
{
Change the sign of speedX
// since speedX changes the circle.CenterX, this will flip the horizontal
// direction of the circle movement!
}
Easy eh? Now, put in code to “bounce” the circle off the other boundaries:
· Left: (centerX < 0)
· Top: (centerY>600), and
· Bottom: (centerY<0).
Step 4:
Do you see your paddle moving very slowly? Do you see mine moves rather quickly? J Why is that? How can you fix the slow moving paddle problem?
Submit your ClassExample.cs file, bring a hardcopy (Remember to write your name!!) to
next class to submit.
1. Save
and submit your screen shots using your name (2 jpg or png
files, refer to Exercise-2 on how to take screen shots: select the window, and Alt-PrintScreen). You
DO NOT need to submit hard copy of this file. ß Sorry, this was a left over from
previous assignment. My mistake in forgetting to remove this line.
In this assignment you have learned 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.
In addition, you have begun experiencing with conditional statements (if statements) where you develop expressions that compare variables and numbers.