B CUSP 110B – Digital Thinking

Homework 8: Pong Collision + Scores!

 

 

 

Goal: The purpose of this exercise is to continue practice with parameters, and working with conditionals (if statements). We will spend a whole lot of time on conditional statements on next Monday!

 

Here is an almost fun “pong game” we will be developing (download, unzip, and double click on the .exe file). Notice:

·         The pong-ball bounces inside the game window!!

·         The pong-ball actually bounces off the left/right paddles

·         We are keeping scores!!

·         Same controls from HW7:

o   Button-A (or K-key): resets the ball to the center of the game window, and gives the ball a new random speed.

o   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.

 

Investigate Given Source

Download this source code, unzip, compile and run. Notice that the right-paddle’s score is being kept correctly, while the left paddle is bouncing the ball away correctly. Now, let’s figure out why this is the case.

 

Once again, the very first thing we do when receive any source code is to read and understand what we are given. Now, notice:

·         Declaration of rightScore and leftScore

·         InitializeWorld(): Initialization of the two scores to 0:

·         UpdateWorld():

o   Changing the rightScore by 1 when the ball reaches the right side of the window. Can you find the code that implements this?

 

o   At the end of the function, detects if leftPaddle collides the pongBall, if so, change the speedX direction:

 

 

Step 1: Record number of times left side of the window is touched by the pongBall.

 

In UpdateWorld(), find the code that is changing the value for rightScore (by 1) when the ball touches the right side of the window. Examine and understand this code. Now, how can you implement similar code when the ball touches the left side of the window?

 

 

Step 2: Collide the rightPaddle with the pongBall.

 

Examine how the leftPaddle collides the the pongBall, now, implement the collision of the rightPaddle.

 

To Turn In

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

Wrap Up

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.