Pre-requisite: it is assumed
that you have read through
the prior tutorials, and are familiar with the concepts covered in those
tutorials.
Goals:
Throughout this chapter, we will work towards a simple game, with each
tutorial adding a little bit more to what we've already done. In this
tutorial, we're going to
Refactor our code to use the logical OR operator
1. Obtain the example code
Here is the zip file to the source
files and compiled executable of this example.
Download and unzip the zip file and you will see an ExampleProgram folder. Open the ExampleProgram
folder, the EXE folder contains the compiled program and you can double click on the .sln
file to work with the source code.
When the game starts, you'll see a screen that
looks similar to this:
The game behaves in a manner that is identical to the what
was described for the previous tutorial. In this tutorial, we will simply
refactor some code so that it uses the logical OR operator.
2. Refactoring BounceOffPaddles To Use The Logical OR
BounceOffPaddles:
///
<summary>
///
Test collision between the soccer ball and the two paddles,
///
if collide flip ball's velocity X-component, and keep track
///
of number of bounces.
///
</summary>
private
void
BounceOffPaddles()
{
if (m_RightPaddle.Collided(m_TheSoccer)
|| m_LeftPaddle.Collided(m_TheSoccer))
{
m_TheSoccer.VelocityX = -m_TheSoccer.VelocityX;
PlayACue(
"Bounce");
if
(m_NumBounces > BOUNCES_NEEDED_TO_WIN && m_BallsMissed >=
MAX_BALLS_MISSED_TO_WIN)
m_BallsMissed--;
else
m_NumBounces =
m_NumBounces + 1;
}
}
If you look back at the BounceOffPaddles function in previous tutorials,
you'll notice that no matter which paddle the ball bounces off of, the
program does the exact same thing. Instead of
duplicating that code twice, let's refactor that code, and combine the two
separate cases into one:
if
(m_RightPaddle.Collided(m_TheSoccer) || m_LeftPaddle.Collided(m_TheSoccer))
In other words, if the right paddle has collided with the
soccer ball, OR the left paddle has collided with the soccer ball, then do
everything inside the curly-braces (which you've seen already)
FURTHER EXERCISES::
Start from a blank starter project (1000.201, if you need it), and re-do
the code from memory as much as possible. On your first try, do what
you can, and keep the above code open so that when you get stuck, you can
quickly look up what you forgot (and that after you finish a line, so that
you can compare your line to the 'correct' line). On the next try, do
the same thing, but try to use the finished code less. Repeat this
until you can type everything, without refering the tutorial's code.
Repeat this exercise daily for several days, so that you really get the
hang of this. As you go on, periodically review this by re-doing this
exercise.
Logical OR
For this exercise, you should use the same project that was explained in the
above tutorial.
Looking at the code for the BounceOffBlocks method, is it possible to replace
the three separate if statements with a single if statement and several
logical ORs, like so:
if
(m_ABlock.Collided(m_TheSoccer) || m_BBlock.Collided(m_TheSoccer) ||
m_CBlock.Collided(m_TheSoccer) )
{ // more code goes here
Why, or why not?
Logical OR vs. Logical AND
For this exercise, you should use the same project that was explained in the
above tutorial.
In UpdateWorld, why do we need to
use the logical AND, rather than OR, to detect if the player has won?
What would the game play like if we used OR instead?
Logical OR
For this exercise, you should use the same project that was explained in the
above tutorial.
Change the program so that the game will be reset if the player presses the A
button, or if the player presses the B button.