Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Microsoft CSharp Programming For The Absolute Beginner (2002) [eng].pdf
Скачиваний:
39
Добавлен:
16.08.2013
Размер:
15.71 Mб
Скачать

Chapter 9: File Handling: The Adventure Kit

You are beginning to see how important data is to a programmer. Often, even when your program is not running, you want the data in your program to be permanently stored so that you can retrieve it later. Modern languages such as C# support many tools for storing and retrieving information on disk drives. In this chapter you will learn two of the most basic and important schemes: text file management and object serialization. Text files are used to store basic data, and object serialization enables you to save and load entire objects. You will also learn how to use dialog boxes to streamline certain kinds of user interaction and how to add menus to your programs. In this chapter you will learn how to

Store and load a plain text file to a disk.

Use file streams to manage your interactions with disk files.

Create multilevel menu systems on your forms.

Use the File, Font, and Color dialog boxes to interact with the user.

Store entire classes with object serialization.

Write programs that use multiple forms.

Introducing the Adventure Kit

The Adventure Kit game program you will produce in this chapter is fun in a couple ways. For one, it allows you to play simple adventure games. However, the real appeal of the program is that it encourages the user to create original adventure games. One key to making a program interesting is to make it extensible. You can use the Adventure Kit to develop many types of adventure games.

Viewing the Main Screen

The Adventure Kit game uses several screens to control the action. The main screen is extremely simple. It allows the user to load, play, or edit (or create) a game or quit the program. This simplistic main program is featured in Figure 9.1. I deliberately made the main screen simple because the other parts of the program can be complex and I wanted to control the level of complexity. For example, a complete beginner to the program would probably want to play a game or two before calling up the editor.

236

Figure 9.1: The main screen provides very basic access to the other parts of the program.

Loading an Adventure

The Adventure Kit game is designed to allow multiple adventures. Each adventure is stored as a file on the drive system. The adventure files can be shared between users. To play an adventure, you must load it into memory first.

When the user clicks the Load button, a standard file dialog appears (see Figure 9.2). The File dialog box uses very standard Windows metaphors to handle selecting a file from the drive system. It looks like a complex form to build, but don’t panic. A trick is coming, in the section titled (strangely enough) “Using the File Dialog Controls.”

Figure 9.2: The file dialog looks very familiar to experienced users of Windows.

237

Playing an Adventure

Of course, the obvious thing to do with the Adventure Kit game is to play an adventure. If the user clicks the Play button, a second screen appears, with a game preloaded into it (see Figure 9.4). The user interface of the game program is designed to be as clean and obvious as possible so that the user can concentrate on the game rather than on the mechanics of moving around.

I wrote a simple adventure named Enigma. In this adventure, you are a spy sent to capture the famous enigma code machine from a Nazi submarine. Figure 9.3 through Figure 9.5 show a few scenes from this adventure. When the user is finished with the game, he can close the game form with a command from the menu. Alternatively, the player can edit the current game, also with a menu command. The menu system is nice because most users already know how to use menus.

Figure 9.3: Here’s the starting situation. Try clicking Sub Deck. (The Enigma device was carried on submarines.)

238

Figure 9.4: The screen changes to reflect the new situation. It’s not looking good, but there are plenty of places to jump.

239

Соседние файлы в предмете Программирование на C++