Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Beginning CSharp Game Programming (2005) [eng]

.pdf
Скачиваний:
162
Добавлен:
16.08.2013
Размер:
4.97 Mб
Скачать

280 Chapter 10 Putting Together a Game

Networking

Networking is another huge topic in game programming. When’s the last time you played a big game that didn’t support multiplayer online game play? Probably a long time ago, right?

Advanced Storage

The storage for GSS3K is amazingly simple: three arrays of objects. This is about as simple as it gets because it doesn’t get any easier. In larger games, you have to figure out how to store all kinds of data—entire worlds worth of data, sometimes! How are you going to do that? Well, that’s where data structure theory comes into play. There’s more to data structures than just arrays, and you need to research what kind of structure is best suited to storing your virtual worlds.

You may even look into having a database to store your data, but that’s usually only done with huge, massively-multiplayer online games.

Summary

Well, now that you’ve assembled a whole game, what are you going to do? Go to Disney World? You should be thinking about building onto your first game, or building something better. By now, you should know enough to get started on your own projects.

What You Learned

The main concepts that you should have picked up from this chapter are:

How to think about your design.

How to visualize your universe.

How to start small, and not bite off more than you can chew.

How to think about how your actors are going to interact with each other.

How to figure out what kind of data you need before you write any code.

How Generic Space Shooter 3000’s data and game rules are arranged.

How to build a better framework to suit your game programming needs.

How to encapsulate game states into classes to make your programs more modular.

How to use a stack to store game states.

How to build your own arcade space shooter game!

Summary 281

Review Questions

These review questions test your knowledge on the important concepts exposed to you in this chapter. The answers can be found in Appendix A.

10.1.Why is it important to think about your design before you start writing any code?

10.2.What parts of your game should you think about while designing it?

10.3.Why is it a bad idea to start a huge game project on your own, especially if it’s your first game?

10.4.How can using game states make your programs more modular?

10.5.Why is it a good idea to use a stack to store game states?

10.6.You should always be thinking about ways to improve your game, and should plan accordingly. Why?

On Your Own

There’s quite a bit you can do on your own with the knowledge that you have now. Some features of GSS3K were cut out due to time and space restraints, so the game isn’t quite complete in its current form. Among those features were:

A scrolling star field

Force feedback effects

Enemy waves generated from files on disk

Enemy and weapon data stored on disk in a flexible format

Pick some of these features and add them to the game. Or, make up your own features and add them.

This page intentionally left blank

Conclusion

You now know everything there is to know about game programming in C#! Okay, no, I’m just kidding. I really hate to be the one to break it to you, but you’ve barely just scratched the surface. But you’re off to a great start! Game programming is one of the most complex areas of study in the entire world, and you should be proud that you’re part of this elite community.

You will always be learning. No one can possibly know everything there is to know about game programming, ever. It’s simply not possible. But this is a good thing. I find that most people get into game programming in the first place because they are on a never-ending quest to learn. If that describes you, then boy, have you found the right niche!

Chapters 5 and 10 gave you some ideas about where to continue your knowledge in the areas of C# and Game Programming in general. Luckily for you, there are tons of books that can help you out in this quest. In particular, you’re probably going to want to continue reading on about DirectX and Direct3D. Beginning DirectX 9, by Wendy Jones, and

Beginning Direct3D Game Programming (2nd Edition), by Wolfgang Engel, will be particularly useful to you.

And now, pupils, I’m afraid it is time for me to go, and it’s time for you to spread your wings and explore the vast world of game programming.

If you have any questions or comments about the book, feel free to drop me a line at CSBook@ronpenton.net, or visit my public forum discussing this book at http://ronpenton. net/forum.

Thank you for reading this book—it has been a pleasure teaching you game programming in C#. Fare thee well!

283

This page intentionally left blank

PART III

Appendixes

Appendix A

Answers to Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .287

Appendix B

Setting Up DirectX and .NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .303

This page intentionally left blank

Appendix A

Answers to

Review Questions

Each chapter in this book includes review questions about some of the important concepts covered in the chapter. Here are the answers to those questions.

Chapter 1: The History of C#

1.1. Why does a virtual machine slow down programs?

A virtual machine needs to translate virtual machine code into real machine code, which takes time and adds a significant amount of overhead to your program.

1.2. How does JIT compilation speed up VM execution?

JIT (Just In Time) compilation speeds up the execution of virtual machines by converting the VM code into real machine code when the program is first loaded, so that the conversion doesn’t occur while the program is running.

1.3 What languages does Microsoft officially support for .NET?

Microsoft officially supports four languages on .NET:

Managed C++

C#

Visual Basic.NET

J#

1.4.Can other languages support .NET as well?

Yes. Since the .NET platform runs virtual machine code in the form of MSIL (Microsoft

 

Intermediate Language), any program in any language can be converted into MSIL code

 

and run on the .NET framework.

287

288 Appendix A Answers to Review Questions

Chapter 2: The Basics

2.1. Every C# program requires at least one main static class. (True/False)

False. Every program must contain at least one static function, not class. This static function must be called Main and will be the entry point of the program.

2.2. Booleans are only one bit in size. (True/False)

False. Booleans are one byte in size. Storing bits is inefficient in speed, as a computer cannot address memory that small.

2.3. Unsigned integers can hold numbers up to around 4 billion. (True/False)

True. Unsigned integers can hold values from 0 to 4,294,967,295, which is one less than 232.

2.4. Floating-point numbers hold exact representations of numbers. (True/False)

False. Floating-point numbers hold approximations of decimal numbers.

2.5. Why can’t you use variables before they have been assigned a value?

In older languages, whenever you created a new variable and didn’t assign anything to it, the variable would contain whatever data was previously in that address, which could be anything. This would end up causing many hard-to-detect errors because the variable would hold seemingly valid data. In C#, the compiler detects if you haven’t assigned anything to the variable and gives you a compiler error if you try using it.

2.6. Why do constants make your programs easier to read?

Constant values, such as pi and e, are very recognizable, and if you used 3.14159 in your program, most people would probably recognize it. But if you wanted all spaceships in your game to move at 200 pixels per second, then putting the value 200 in your program might not be so obvious. A constant called Shipspeed, however, makes your programs much more readable.

If you need to change how fast spaceships travel later on, it’s much easier to change the value of the constant in one place than to search your code for the value 200 and change it multiple times—this can lead to big bugs.

2.7.Is the following code valid? int x = 10;

float y = 20; x = y;

No. You cannot convert a float to an int like this (implicitly). To fix this code, you would replace the last line with:

x = (int)y;

Chapter 2: The Basics

289

2.8.What is the value of x after this code is done? int x = 10;

if( x == 10 ) x = 20;

The value is 20.

2.9.Assume that c is 0. What are the values of the variables after this code is done, and why? int w = 0, x = 0, y = 0, z = 0;

switch( c )

{

case 0:

w = 10; case 1:

x = 10; case 2:

y = 10; break;

case 3:

z = 10; break;

}

w is 10, x is 10, y is 10, z is 0. The case statements 0, 1, and 2 are all executed because they do not break out of the switch statement. Case 3 isn’t executed because case 2 breaks out.

2.10. Now assume c is 2 and the code from question 2.9 is run again. What are the values of the variables w, x, y, and z?

w is 0, x is 0, y is 10, z is 0.

2.11. Does the computer compare the value of x and 10 in this example? int x = 10, y = 20;

if( y == 20 && x == 10 ) x = 20;

Yes. As this is an and comparison, the only way the second comparison will be executed is if the first comparison is false, which it is not in this case.

2.12. Does the computer compare the value of x and 10 in this example? int x = 10, y = 20;

if( y == 20 || x == 10 ) x = 20;

No. As the first comparison passed, and this is an or comparison, there’s no need to check the second part.