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

sensible. If the user runs out of gas, I disable the timer temporarily to stop the game flow and then display a message to the user so that he or she knows why the game stopped. I then call the killShip() method to take a ship out of the inventory, and I call the initGame() method to reset the speed and position of the lander and platform.

After dealing with the fuel situation, my attention turns to the actual key presses. Because I’m concerned with arrow keys here, I use the KeyDown() method and concentrate on e.KeyData. Depending on which key was pressed, I copy the appropriate image from the ImageList and set dx and dy to achieve the appropriate motion later when the timer ticks. Notice that I added a default condition to handle keystrokes other than arrow keys. If I were a nice guy, I would have used the default condition to add back the fuel value. Then, if the user accidentally hits a key, it would not cost precious fuel. However, as a game programmer, you can be mean if you want (cue maniacal laughter).

The showStats() Method

The showStats() method is called every time the timer ticks. Its job is to update the labels that display statistics, such as the score and the ships remaining to the user. This code is quite simple:

public void showStats(){ //display the statistics lblDx.Text = "dx: " + dx; lblDy.Text = "dy: " + dy; lblFuel.Text = "fuel: " + fuel;

lblShips.Text = "ships: " + ships; lblScore.Text = "score: " + score;

} // end showStats

A few assignment statements are all that is required. However, if you don’t provide adequate information to the user, your game will not be successful. Also, because updating the score happens often, it’s nice to have the code stored in a procedure.

The killShip() Method

The killShip() method is meant to be called whenever the user has lost a ship because of crashing or running out of fuel:

public void killShip(){

//kill off a ship, check for end of game DialogResult answer;

ships−−;

if (ships <= 0){ //game is over

answer = MessageBox.Show("Play Again?", "Game Over",MessageBoxButtons.YesNo);

if (answer == DialogResult.Yes){ ships = 3;

fuel = 100; initGame();

}else { Application.Exit();

}// end if

}// end 'no ships' if

}// end killShip

200

The act of killing off the player’s ship takes only one line of code. The real meat of the killShip() method is the part that checks whether the game is over. If the player is out of ships, the method asks the user whether he or she wants to play again, using a yes/no message box. If the player does want to play again, the number of ships is reset, and the initGame() method resets the speed and position of the lander and platform. If the user does not want to play again, the program exits completely with the call to Application.Exit().

The initGame() Method

The initGame() method is a real workhorse. It has a simple job but is called from several places in the program. The purpose of initGame() is to randomly set the location of the lander and platform and randomly choose the speed of the lander:

public void initGame(){

// re−initializes the game Random roller = new Random(); int platX, platY;

//find random dx, dy values for lander

dx = Convert.ToDouble(roller.Next(5) − 2); dy = Convert.ToDouble(roller.Next(5) − 2);

//place lander randomly on form

x = Convert.ToDouble(roller.Next(this.Width)); y = Convert.ToDouble(roller.Next(this.Height));

//place platform randomly on form (but make sure it appears)

platX = roller.Next(this.Width − picPlatform.Width); platY = roller.Next(this.Height− picPlatform.Height); picPlatform.Location = new Point(platX,platY);

//turn on timer timer1.Enabled = true;

} // end initGame

With all the randomization that happens in the initGame() method, you won’t be surprised to find an instance of the Random class defined in the method.

Choosing New dx and dy Values

I wanted dx and dy to be somewhere between 2 and –2, which is not directly possible with the Random class. However, I asked for an integer value between 0 and 4 (remember, the 5 refers to the number of possible responses, not the largest possible response), and I subtracted 2 from this number. This will give results evenly spaced between 2 and –2. However, the results are integers, and dx and dy are double variables, so I used the trusty convert object to get doubles where I wanted them.

Placing the Lander on the Form

Finding a legal place for the lander is relatively easy. The new X location should be somewhere between 0 and the width of the form, and the new Y should be between 0 and the height of the form. Again, it was necessary to convert to doubles because I chose to implement x and y as double values.

201

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