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

something besides its starting value of "". In that case, the program creates an instance of the Game class, shows the form, and opens the game stored in the current dungeon. If the value of gameFile is still null, the method reminds the user to load a game before trying to play it.

Editing the Game

The process for opening the editor is simpler than for opening the game because it’s reasonable for the user to open up the editor without a game in memory, especially if the user wants to create a new game from scratch.

private void btnEdit_Click(object sender, System.EventArgs e) {

theEditor = new Editor(); theEditor.Show();

if (gameFile != ""){ theEditor.OpenGame(theDungeon);

} // end if

}// end btnEdit

The method simply creates a new instance of the Editor class, displays the form, and opens up the game if it is already in memory.

Summary

Your programs are beginning to take on more challenging and interesting dimensions. You have added some very useful tools to your bag of tricks in this chapter. You learned how to store and load plain text files. You learned how to use file streams, stream readers, and stream writers to manipulate files. You added menu structures—complete with hotkeys, shortcuts, and multiple levels—to your programs. You learned how the standard dialogs can query the user for fonts, colors, and file names. You practiced binary object serialization to store entire objects to files. You put all these things together in an interesting multiform application.

Challenges

Add a Save feature to the game editor. Save usually works just like Save As if the file has not yet been saved. However, if the program recognizes a file name for the current file, it automatically saves that file without calling up a dialog box.

Add menus to the Visual Critter program from Chapter 6, “Creating a Windows Program.”

Modify the Mini Adventure program (the game where you added words to a text file) from Chapter 1 “Basic Input and Output: A Mini Adventure,” so that it can read a text file from a disk. Allow the user to put in special tags (such as <noun>). When the program encounters a tag, have it ask for a noun and place it in a variable. Perhaps add an editor so that the player can save and load multiple stories.

Add object serialization to the Snowball Fight program from Chapter 5, “Constructors, Inheritance, and Polymorphism.” Allow the user to modify various characteristics of a robot−controlled fighter. Then store and load these objects to the drive system.

Extend the Serialization Demo program in this chapter to be a small database. Extend the Contact class so that it has the properties you want, and build a class like Dungeon to store a list of contacts.

276

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