📄 myblockmain.cs
字号:
// About about = new About();
// about.ShowDialog(this);
//
// // Destroys the About form.
// about.Dispose();
// about = null;
}
/// <summary>
/// The click of the "Help Topics..." menu. Shows the help file.
/// </summary>
/// <param name="sender">The parent control who triggered the event.</param>
/// <param name="e">Default event arguments.</param>
private void mnuHelpTopics_Click(object sender, System.EventArgs e)
{
// Shows the help file.
Help.ShowHelp(this, "AnotherBlock.chm");
}
/// <summary>
/// The click of the "How to Play..." menu. Shows the help file, in the "How to Play" topic.
/// </summary>
/// <param name="sender">The parent control who triggered the event.</param>
/// <param name="e">Default event arguments.</param>
private void mnuHowToPlay_Click(object sender, System.EventArgs e)
{
// Shows the help file, in the "How to Play" topic.
Help.ShowHelp(this, "AnotherBlock.chm", HelpNavigator.Topic, "How%20to%20Play/How_to_Play.htm");
}
/// <summary>
/// The click of the "Sounds" menu. Enables or disables the sound effects.
/// </summary>
/// <param name="sender">The parent control who triggered the event.</param>
/// <param name="e">Default event arguments.</param>
private void mnuSounds_Click(object sender, System.EventArgs e)
{
// Inverts the checked state of the Sounds menu.
mnuSounds.Checked = !mnuSounds.Checked;
// Saves the options to the registry.
SaveConfigs();
}
/// <summary>
/// The click of the "Music" menu. Enables or disables the background music.
/// </summary>
/// <param name="sender">The parent control who triggered the event.</param>
/// <param name="e">Default event arguments.</param>
private void mnuMusic_Click(object sender, System.EventArgs e)
{
// Inverts the checked state of the Music menu.
mnuMusic.Checked = !mnuMusic.Checked;
// Saves the options to the registry.
SaveConfigs();
// Checks if there is a current game.
if (game != null)
{
// There is a current game.
// Checks if the current game is not over.
if (game.GameState != GameState.Over)
{
// Current game is not over.
// Checks if the "Music" menu is checked.
if (mnuMusic.Checked)
{
// "Music" menu is checked.
// Starts playing the music.
soundMusic.Play();
}
else
{
// "Music" menu is not checked.
// Stops playing the music.
soundMusic.Stop();
}
}
}
}
/// <summary>
/// The lost of the focus by the main application. Pauses the current game, if there is one.
/// </summary>
/// <param name="sender">The parent control who triggered the event.</param>
/// <param name="e">Default event arguments.</param>
private void AnotherBlockMain_LostFocus(object sender, System.EventArgs e)
{
// Checks if there is a current game.
if (game != null)
{
// There is a current game.
// Checks if the current game state is "Running".
if (game.GameState == GameState.Running)
{
// Current game state is "Running".
// Pauses the game, by setting the current state to "Paused".
game.GameState = GameState.Paused;
this.Text += " - " + resources.GetString("Paused");
}
}
}
#region High Score and Registry Treatment
/// <summary>
/// Private method that checks for the registry entries that the game needs, and creates them if necessary.
/// </summary>
private void CheckForRegistryEntries()
{
int numHighScores;
// Opens the HKEY_LOCAL_MACHINE key in the registry.
RegistryKey hkeyLocalMachine = Registry.LocalMachine;
// Tries to open the application subkey (SOFTWARE\Tempo Software e Servi鏾s Ltda.\Another Block) in the HKEY_LOCAL_MACHINE key.
RegistryKey anotherBlockKey = hkeyLocalMachine.OpenSubKey(@"SOFTWARE\Tempo Software e Servi鏾s Ltda.\Another Block", true);
// Checks if the application subkey was really opened.
if (anotherBlockKey != null)
{
// The application subkey was opened.
// Tries to get the number of kept high scores.
try
{
numHighScores = Convert.ToInt32(anotherBlockKey.GetValue("NumHighScores").ToString());
}
catch
{
// Couldn't get the number of kept high scores.
// Creates the value NumHighScores, and sets the number of kept high scores to 10.
anotherBlockKey.SetValue("NumHighScores", "10");
numHighScores = 10;
}
// Tries to get the rotation configuration.
try
{
string rotateConfig = anotherBlockKey.GetValue("Rotate").ToString();
}
catch
{
// Couldn't get the rotation configuration.
// Creates the value Rotate, and sets it to clockwise.
anotherBlockKey.SetValue("Rotate", "clockwise");
}
// Tries to get the Sounds configuration.
try
{
string soundsConfig = anotherBlockKey.GetValue("Sounds").ToString();
}
catch
{
// Couldn't get the Sounds configuration.
// Creates the value Sounds, and sets it to 1 (enabled).
anotherBlockKey.SetValue("Sounds", "1");
}
// Tries to get the Music configuration.
try
{
string musicConfig = anotherBlockKey.GetValue("Music").ToString();
}
catch
{
// Couldn't get the Music configuration.
// Creates the value Music, and sets it to 1 (enabled).
anotherBlockKey.SetValue("Music", "1");
}
// Tries to get all the high scores.
for(int i = 1; i <= numHighScores; i++)
{
// Tries to get the high score name for this position.
try
{
string highScoreName = anotherBlockKey.GetValue("HighScoreName" + i.ToString()).ToString();
}
catch
{
// Couldn't get the high score name for this position.
// Creates the value HighScoreName for this position, and sets its initial value to none.
anotherBlockKey.SetValue("HighScoreName" + i.ToString(), "");
// Creates the value HighScore for this position, and sets its initial value to zero.
anotherBlockKey.SetValue("HighScore" + i.ToString(), "0");
}
}
}
else
{
// The application subkey was not opened, so it doesn't exist.
// Creates the application subkey (SOFTWARE\Tempo Software e Servi鏾s Ltda.\Another Block) in the HKEY_LOCAL_MACHINE key.
hkeyLocalMachine.CreateSubKey(@"SOFTWARE\Tempo Software e Servi鏾s Ltda.\Another Block");
// Checks for the registry entries again.
CheckForRegistryEntries();
}
// Tries to close the subkey and the key.
try
{
// Closes the application subkey.
anotherBlockKey.Close();
// Closes the HKEY_LOCAL_MACHINE key.
hkeyLocalMachine.Close();
}
catch
{
// Couldn't close the keys. Do nothing.
}
}
/// <summary>
/// Private method that gets a high score entry from a position, and returns it.
/// </summary>
/// <param name="position">The position of the high score entry to get.</param>
/// <returns>A high score entry, with name and score.</returns>
private HighScoreEntry GetHighScoreEntry(int position)
{
// Opens the HKEY_LOCAL_MACHINE key in the registry.
RegistryKey hkeyLocalMachine = Registry.LocalMachine;
// Opens the application subkey (SOFTWARE\Tempo Software e Servi鏾s Ltda.\Another Block) in the HKEY_LOCAL_MACHINE key.
RegistryKey anotherBlockKey = hkeyLocalMachine.OpenSubKey(@"SOFTWARE\Tempo Software e Servi鏾s Ltda.\Another Block", false);
// Gets the number of kept high scores.
int numHighScores = Convert.ToInt32(anotherBlockKey.GetValue("NumHighScores"));
// Checks if the position of the high score to get is equal or smaller than the total number of kept high scores.
if (position <= numHighScores)
{
// Position of the high score to get is equal or smaller than the total number of kept high scores.
// Creates a new high score entry.
HighScoreEntry currentEntry = new HighScoreEntry();
// Gets the values in the registry, to fill the just created high score entry.
currentEntry.Name = anotherBlockKey.GetValue("HighScoreName" + position.ToString()).ToString();
currentEntry.Score = Convert.ToInt32(anotherBlockKey.GetValue("HighScore" + position.ToString()));
// Returns the just filled high score entry.
return currentEntry;
}
else
{
// Position of the high score to get is not equal or smaller than the total number of kept high scores.
// Throw an argument exception with the error.
throw new ArgumentException("The argument position is larger than the number of high scores kept in the registry.");
}
}
/// <summary>
/// Private method that records the current high score (if there is one), to the registry.
/// </summary>
/// <param name="highScoreEntry">The high score entry with name and score to be recorded along with the high score.</param>
private void RecordHighScore(HighScoreEntry highScoreEntry)
{
// Checks if the current score sets a high score.
if (CheckForHighScore())
{
// Opens the HKEY_LOCAL_MACHINE key in the registry.
RegistryKey hkeyLocalMachine = Registry.LocalMachine;
// Opens the application subkey (SOFTWARE\Tempo Software e Servi鏾s Ltda.\Another Block) in the HKEY_LOCAL_MACHINE key.
RegistryKey anotherBlockKey = hkeyLocalMachine.OpenSubKey(@"SOFTWARE\Tempo Software e Servi鏾s Ltda.\Another Block", true);
// Gets the number of kept high scores.
int numHighScores = Convert.ToInt32(anotherBlockKey.GetValue("NumHighScores"));
// Holds the current score.
string currentName = highScoreEntry.Name;
int currentScore = highScoreEntry.Score;
// Includes the current score in the high score list, in the registry, by running through all the high score list.
for(int i = 1; i <= numHighScores; i++)
{
// Gets this position's score and name.
int currentHighScore = Convert.ToInt32(anotherBlockKey.GetValue("HighScore" + i.ToString()));
string currentHighScoreName = anotherBlockKey.GetValue("HighScoreName" + i.ToString()).ToString();
// Checks if the current held score is higher than the current score in the high score list.
if (currentScore > currentHighScore)
{
// Current held score is higher than the current score in the high score list.
// Record the current score and name to the high score list in the registry.
anotherBlockKey.SetValue("HighScore" + i.ToString(), currentScore.ToString());
anotherBlockKey.SetValue("HighScoreName" + i.ToString(), currentName);
// Gets the old score and name in the high score list, and puts them in the current held score and name.
currentScore = currentHighScore;
currentName = currentHighScoreName;
}
}
// Closes the application subkey.
anotherBlockKey.C
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -