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

} // end Open_game

The openGame() method simply copies the passed dungeon parameter to theDungeon, initializes the room number to 0, copies the dungeon name to the appropriate text box, and shows the room.

Exiting, Playing, and Creating a New Game

The other menu event handlers are (as usual) standard fare. When the user chooses to exit the editor, I use this.Dispose() to eliminate the form from memory.

If the user wants to create a new game, the setupRooms() method does most of the work, but I also reset roomNum to 0 and showed room 0. Finally, I reset the txtGameName text box to empty.

The mnuPlay_Click() method directly calls the game screen so that the user can immediately play whatever adventure he has been working on without having to return to the main form first. It opens a new instance of the Game class, shows the form, and opens up the current dungeon with a call to theGame.OpenGame(). Finally, the method disposes the current (Editor) class to get it out of the way.

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

this.Dispose(); } // end mnuExit

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

setupRooms(); roomNum = 0; showRoom();

txtGameName.Text = ""; } // end mnuNewGame

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

Game theGame = new Game(); theGame.Show(); theGame.OpenGame(theDungeon); this.Dispose();

} // end mnuPlay

Writing the MainForm Class

The MainForm class is the user’s initial entry and exit point to the program. It serves mainly as a control center, routing the user between the other forms. Although it appears to the user to be the main part of the program, it is actually the simplest part of the project, and the last part I designed. Each button calls up the appropriate form or closes the program altogether.

Creating the Main Form's Visual Design

The primary purpose of the main form is to tie the rest of the program together. Ironically, the form is most effective if the user spends very little time on it at all. For this reason, all the main actions belong to prominent command buttons that dominate the form. To keep the program appearance unified, I assigned the same scroll background to each button and to a picture box on the form. The form also has a label indicating which game, if any, is currently loaded in memory.

274

Building the MainForm Class Instance Variables

The MainForm class uses instance variables to control each form, the adventure data, and a file name for the current game:

//classes for game and editor screens private Editor theEditor = new Editor(); private Game theGame = new Game();

private Dungeon theDungeon = new Dungeon(); private string gameFile = "";

Loading the Dungeon

The MainForm class is capable of loading a dungeon class before calling the other classes. This ensures that the Game class is never called without a valid dungeon in memory. Also, this makes debugging your adventures easier because you can simultaneously call a game window and an editor window to see how your game plays while you examine it in the editor.

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

//read the data from a binary file FileStream s;

BinaryFormatter bf = new BinaryFormatter();

if (fileOpener.ShowDialog() != DialogResult.Cancel){ gameFile = fileOpener.FileName;

s = new FileStream(fileOpener.FileName, FileMode.Open); theDungeon = (Dungeon) bf.Deserialize(s);

s.Close();

lblCurrent.Text = "Game: " + theDungeon.Name;

} // end if

}// end btnLoad

The binary serialization technique, which has been such a workhorse in this program, is called into service one more time. As usual, the value of the file is copied to a Dungeon instance after being deserialized.

This theDungeon variable will be used to call the OpenGame() methods of the other two forms.

Playing the Game

If the user wants to play a game, the program first checks whether a game has been loaded. If not, it asks the player to do so. If a game has been loaded, starting up the game window is a simple process:

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

if (gameFile != ""){ theGame = new Game(); theGame.Show();

theGame.OpenGame(theDungeon);

}else {

MessageBox.Show("Please load a game first");

}// end if

}// end btnPlay

If a game has been successfully loaded into the program, the value of the gameFile variable will be

275

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