Game Development Reference
In-Depth Information
Don't believe me? Check out the code in bold that shows the section of the kjCqaoo>qppkj?he_g
event handler's eb+ahoa statement that does this:
Think about the logic behind what the eb+ahoa statement is saying. If players still have enough guesses
remaining, the game will continue. If the game is still continuing, and players have guessed the right
number, they must have won the game. The c]iaSkj variable is then set to pnqa, and the directives in
the aj`C]ia method are run. When the aj`C]ia method runs, its conditional statement notices that
c]iaSkj is pnqa and displays the message telling the player they've won.
Modular programming with methods
Can you see how the _da_gC]iaKran and aj`C]ia methods were used to help modularize the code? In
truth, you could have written this program without them by adding all the conditions they check for
in one extremely long eb+ahoa statement. But if you'd done so, you'd have to write some of the same
code over twice, and it would all start to become very difficult to read and debug.
Modularizing specific tasks inside self-contained methods allows you to modify or debug those bits of
code in isolation without having to change (and possibly damage) other parts of the program that are
working. It also means that whenever you want to perform a certain task, you don't need to duplicate
any of the code you've already written; you just have to call the method.
Using methods to modularize your code might take you a bit of practice, and you might find it a bit
of a brain-twister until you've seen a few more examples and experimented with using them in your
own projects. Have a look at how the game is working so far and see if you can figure out how the
interrelationships between methods and method calls are working. Figure 5-19 is a map of how it all
fits together.
Search Nedrilad ::

Custom Search