Game Development Reference
In-Depth Information
Understanding Loops
You will often need to instruct the program/game to do the same thing several
times in a row, which is called a loop and is very useful when you want to draw
some items on-screen or do something that is repetitive. It is also a lot easier to
write a loop to do this rather than to write the lines of code separately without a
loop. As an example, perhaps you were trying to make a crossword game and
had to display a 17x17 grid of pictures on-screen to represent the crossword
puzzle boxes. There are two ways you could achieve this effect. You could write a
large number of lines of code to represent each row of boxes or you could use a
loop, which would achieve the same result in a few lines of code. Hopefully from
that example you will understand the power of loops in programming. Not only
do loops simplify your code, they also keep you from having to write as much to
achieve the same result. You will also find that reducing your code makes your
program more efficient, which means in the long run it might take up less space
and run faster. More importantly though, the less code you have to write the
easier and quicker it is to debug your program.
There are different ways of creating loops. You can use the keywords do , for ,
foreach , in , and while . While is a very common keyword used for looping. It
basically tells the program, while a statement is true keep running the loop,
when it
s not true stop running the loop.
Hopefully this chapter has whetted your appetite for more information about
the C# programming language and about scripting in general. I recommend that
you take a look at the resources online at the Realm Crafter site (see www. or at the Visual C# site mentioned earlier in the chapter.
Learning C# is a big step if you have not programmed before, but will be
extremely helpful in getting your MMO up and running.
Search WWH ::

Custom Search