📄 myblockmain.cs
字号:
}
else
{
// Game is not over.
// Updates the score in the interface.
lblScore.Text = game.Score.ToString();
// Checks if the lines have changed in the game.
if (lblLines.Text != game.Lines.ToString())
{
// Lines have changed in the game.
// Checks if the sounds menu is checked.
if (mnuSounds.Checked)
{
// Sounds menu is checked.
// Plays the sound for a line.
soundLine.Play();
}
// Updates the lines in the interface.
lblLines.Text = game.Lines.ToString();
}
// Checks if the level has changed in the game.
if (lblLevel.Text != game.Level.ToString())
{
// Level has changed in the game.
// Checks if the sounds menu is checked.
if (mnuSounds.Checked)
{
// Sounds menu is checked.
// Plays the sound for a level.
soundLevel.Play();
}
// Sets the new timer interval, for the new level.
tmrTimer.Interval = (int)((Math.Pow((double)basicInterval / (double)(1000), (double)(game.Level)) + 0.05) * 1000);
// Updates the level in the interface.
lblLevel.Text = game.Level.ToString();
}
// Restarts the timer.
tmrTimer.Start();
}
// Forces the paint event to occurr in both picture boxes (the playing field and the next block).
picPlayingField.Invalidate();
picNextBlock.Invalidate();
}
}
/// <summary>
/// The key down event in the main interface. Checks for the pressed keys, and executes
/// the respective commands.
/// </summary>
/// <param name="sender">The parent control who triggered the event.</param>
/// <param name="e">The key down event arguments.</param>
private void AnotherBlockMain_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
// Checks if there is a current game.
if (game != null)
{
// There is a current game.
// Checks if the game is running.
if (game.GameState == GameState.Running)
{
// Game is running.
// Checks for the key code pressed.
switch(e.KeyCode)
{
case Keys.Up:
// Up arrow pressed.
// Rotate the current block, clockwise or counterclockwise, according to options.
game.RotateCurrentBlock(mnuRotateClockwise.Checked);
break;
case Keys.Down:
// Down arrow pressed.
// Checks if the block can be moved down.
if (!dontMoveDown)
{
// Block can be moved down.
// Move the current block down and checks if there was a hit.
if (game.MoveCurrentBlockDown())
{
// There was a hit.
// Checks if the sounds menu is checked.
if (mnuSounds.Checked)
{
// Sounds menu is checked.
// Plays the hit sound.
soundHit.Play();
}
// Makes the dontMoveDown attribute true, so that the user can't force the next block to move down.
dontMoveDown = true;
// Makes the timer interval the default interval, like level 1.
tmrTimer.Interval = basicInterval;
}
}
break;
case Keys.Right:
// Right arrow pressed.
// Move the current block to the right.
game.MoveCurrentBlockSide(false);
break;
case Keys.Left:
// Left arrow pressed.
// Move the current block to the left.
game.MoveCurrentBlockSide(true);
break;
case Keys.Space:
// Space bar pressed.
// Checks if the sounds menu is checked.
if (mnuSounds.Checked)
{
// Sounds menu is checked.
// Plays the hit sound.
soundHit.Play();
}
// Move the current block all the way down.
game.MoveCurrentBlockCompletelyDown();
break;
}
}
}
// Forces the paint event to occurr in both picture boxes (the playing field and the next block).
picPlayingField.Invalidate();
picNextBlock.Invalidate();
}
/// <summary>
/// The click of the "Pause" menu. Pauses or unpauses the game.
/// </summary>
/// <param name="sender">The parent control who triggered the event.</param>
/// <param name="e">Default event arguments.</param>
private void mnuPause_Click(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 "Paused".
if (game.GameState == GameState.Paused)
{
// Current game state is "Paused".
// Unpauses the game, by setting the current state to "Running".
game.GameState = GameState.Running;
this.Text = Application.ProductName;
}
else
{
// Current game state is not "Paused".
// Pauses the game, by setting the current state to "Paused".
game.GameState = GameState.Paused;
this.Text += " - " + resources.GetString("Paused");
}
// Forces the paint event to occurr in both picture boxes (the playing field and the next block).
picPlayingField.Invalidate();
picNextBlock.Invalidate();
}
}
/// <summary>
/// The override of the Paint event in the next block box, where the next block will be drawn.
/// </summary>
/// <param name="sender">The parent control who triggered the event.</param>
/// <param name="e">The paint event arguments.</param>
private void picNextBlock_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
// Checks if there is a current game.
if (game != null)
{
// There is a current game.
// Checks if the game is running.
if (game.GameState == GameState.Running)
{
// Game is running.
// Draws the next block in the graphics object of the picture box.
game.DrawNextBlock(e.Graphics);
}
}
}
/// <summary>
/// The click of the "Rotate clockwise" menu. Sets the option to rotate the block clockwise.
/// </summary>
/// <param name="sender">The parent control who triggered the event.</param>
/// <param name="e">Default event arguments.</param>
private void mnuRotateClockwise_Click(object sender, System.EventArgs e)
{
// Checks the "Rotate clockwise" menu.
mnuRotateClockwise.Checked = true;
// Unchecks the "Rotate counterclockwise" menu.
mnuRotateCounterclockwise.Checked = false;
// Saves the options to the registry.
SaveConfigs();
}
/// <summary>
/// The click of the "Rotate counterclockwise" menu. Sets the option to rotate the block counterclockwise.
/// </summary>
/// <param name="sender">The parent control who triggered the event.</param>
/// <param name="e">Default event arguments.</param>
private void mnuRotateCounterclockwise_Click(object sender, System.EventArgs e)
{
// Unchecks the "Rotate clockwise" menu.
mnuRotateClockwise.Checked = false;
// Checks the "Rotate counterclockwise" menu.
mnuRotateCounterclockwise.Checked = true;
// Saves the options to the registry.
SaveConfigs();
}
/// <summary>
/// The click of the "Start At..." menu. Shows the Start At window, so that the user can choose a level to start.
/// </summary>
/// <param name="sender">The parent control who triggered the event.</param>
/// <param name="e">Default event arguments.</param>
private void mnuStartAt_Click(object sender, System.EventArgs e)
{
// Creates a new StartAt dialog.
StartAt startAt = new StartAt();
// Shows the StartAt dialog.
startAt.ShowDialog(this);
// After the StartAt dialog is closed, checks if there is a chosen level.
if (startAt.Level > 0)
{
// There is a chosen level.
// Creates a new game in the chosen level.
game = new Game(startAt.Level);
// Resets the score, lines and level labels in the interface, with the initial values.
lblScore.Text = game.Score.ToString();
lblLines.Text = game.Lines.ToString();
lblLevel.Text = game.Level.ToString();
// Enables the "Pause" menu.
mnuPause.Enabled = true;
// Sets the initial timer interval.
tmrTimer.Interval = (int)((Math.Pow((double)basicInterval / (double)(1000), (double)(game.Level)) + 0.05) * 1000);
// Starts the timer.
tmrTimer.Start();
// Forces the paint event to occurr in both picture boxes (the playing field and the next block).
picPlayingField.Invalidate();
picNextBlock.Invalidate();
}
// Destroys the StartAt dialog.
startAt.Dispose();
startAt = null;
}
/// <summary>
/// The click of the "High Scores..." menu. Shows the High Score dialog, with all the high scores.
/// </summary>
/// <param name="sender">The parent control who triggered the event.</param>
/// <param name="e">Default event arguments.</param>
private void mnuHighScores_Click(object sender, System.EventArgs e)
{
// Creates a HighScore form.
HighScore highScore = new HighScore();
// Creates a high score entry.
HighScoreEntry highScoreEntry;
// Runs through the first 10 high score entries.
for(int i = 1; i <= 10; i++)
{
// Gets the current high score entry.
highScoreEntry = GetHighScoreEntry(i);
// Includes the current high score entry in the HighScore form.
highScore.IncludeHighScore(highScoreEntry);
}
// Shows the HighScore form window.
highScore.ShowDialog(this);
// Destroys the HighScore form.
highScore.Dispose();
highScore = null;
}
/// <summary>
/// The click of the "About..." menu. Shows the About dialog.
/// </summary>
/// <param name="sender">The parent control who triggered the event.</param>
/// <param name="e">Default event arguments.</param>
private void mnuAbout_Click(object sender, System.EventArgs e)
{
// Creates an About form, and show its window.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -