📄 level.cs
字号:
return false; // Nothing to do if the game is over
currentPlayer.SetDirection(pos ? PlayerDirection.Down : PlayerDirection.Up);
if (CanPlayerMoveVertical(pos))
{
// Update the block here
if (UpdatePlayerAndBlock(false, playerIndex + ((pos) ? -SquareSize : SquareSize)))
{
// Update the player index
playerIndex += (pos) ? -SquareSize : SquareSize;
// Update the number of moves
totalMoves++;
return true;
}
}
return false;
}
/// <summary>
/// Update the player's position horizontally
/// </summary>
/// <param name="right">Is the player moving right</param>
/// <returns>true if the move was made; false otherwise</returns>
public bool MovePlayerX(bool right)
{
if (isGameOver)
return false; // Nothing to do if the game is over
currentPlayer.SetDirection(right ? PlayerDirection.Right : PlayerDirection.Left);
if (CanPlayerMoveHorizontal(right))
{
// Update the block here
if (UpdatePlayerAndBlock(false, playerIndex + ((right) ? 1 : -1)))
{
// Update the player index
playerIndex += (right) ? 1 : -1;
// Update the number of moves
totalMoves++;
return true;
}
}
return false;
}
#endregion
#region Updates and Game Checks
/// <summary>
/// Update any time specific variables here. Will be called once per frame
/// </summary>
public void Update(float time)
{
// Update the currently running time
elapsedTime += time;
// Calculate the time remaining
if (maxTime > elapsedTime)
{
timeLeft = new TimeSpan(0,0, (int)(maxTime - elapsedTime));
}
// Has the player won the game?
if (GameWon(elapsedTime))
{
// Set the variables and return
isGameOver = true;
isGameWon = true;
}
else if ((elapsedTime >= maxTime) || (totalMoves > maxMoves))
{
// Set the variables and quit
isGameOver = true;
isGameWon = false;
}
// Update the blocks if the game is over
if ((isGameOver) && (!isGameWon))
{
// If this is the first time the game is over,
// randomly assign a velocity to each block
if (isFirstGameOver)
{
isFirstGameOver = false;
Random r = new Random();
int index = 0;
foreach(Block b in blocks)
{
if ((b != null) && (playerIndex != index))
{
// Assign a random velocity
float x, y, z = 0.0f;
x = (float)r.NextDouble() * Block.MaximumVelocity;
y = (float)r.NextDouble() * (Block.MaximumVelocity * 2);
z = (float)r.NextDouble() * Block.MaximumVelocity;
if (r.Next(50) > 25) { x *= -1; }
if (r.Next(50) > 25) { y *= -1; }
if (r.Next(50) > 25) { z *= -1; }
b.SetBlockVelocity(new Vector3(x, y, z));
}
index++;
}
}
foreach(Block b in blocks)
{
if (b != null)
{
b.UpdateBlock(time);
}
}
}
}
/// <summary>
/// Use this method to determine if the game has been won yet.
/// </summary>
private bool GameWon(float totalTime)
{
bool won = true;
numberBlocks = 0;
numberCorrectBlocks = 0;
// Just scroll through each block and see if it is the correct color
foreach(Block b in blocks)
{
// Is there a block here?
if (b != null)
{
numberBlocks++;
b.SetBlockTime(totalTime);
// Is it the right color?
if (b.BlockColor != finalColor)
{
// Nope, you haven't won yet.
b.SetBlockPulse(true);
won = false;
}
else
{
b.SetBlockPulse(false);
numberCorrectBlocks++;
}
}
}
return won;
}
/// <summary>
/// A method to determine if a particular level exists
/// </summary>
/// <param name="level">The level number</param>
/// <returns>true if the level exists; false otherwise</returns>
public static bool DoesLevelExist(int level)
{
return File.Exists(GameEngine.MediaPath +
string.Format("Level{0}.lvl", level));
}
#endregion
#region Properties
/// <summary>
/// Total number of blocks
/// </summary>
public int NumberBlocks
{
get { return numberBlocks; }
}
/// <summary>
/// Total number of correct blocks
/// </summary>
public int NumberCorrectBlocks
{
get { return numberCorrectBlocks; }
}
/// <summary>
/// Get the time remaining in the level
/// </summary>
public string TimeRemaining
{
get { return timeLeft.ToString(); }
}
/// <summary>
/// The total number of moves taken
/// </summary>
public int TotalMoves
{
get { return totalMoves; }
}
/// <summary>
/// The total number of moves allowed
/// </summary>
public int MaximumMoves
{
get { return maxMoves; }
}
/// <summary>
/// Determines if the game is over for any reason
/// </summary>
public bool IsGameOver
{
get { return isGameOver; }
}
/// <summary>
/// Determines if the game has been won
/// </summary>
public bool IsGameWon
{
get { return isGameWon; }
}
#endregion
#region Rendering
/// <summary>
/// Render the entire level
/// </summary>
/// <param name="device">The rendering device</param>
public void Draw(Device device)
{
// Render every block
foreach(Block b in blocks)
{
// It's possible the block may be null
if (b != null)
{
b.Draw(device);
}
}
}
#endregion
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -