// '' <summary> // '' Read the user's name for their highsSwinGame. // '' </summary> // '' <param name="value">the player's sSwinGame.</param> // '' <remarks> // '' This verifies if the score is a highsSwinGame. // '' </remarks> public static void ReadHighScore(int value) { const int ENTRY_TOP = 550; if ((_Scores.Count == 0)) { HighScoreController.LoadScores(); } // is it a high score if ((value > _Scores[(_Scores.Count - 1)].Value)) { Score s = new Score(); s.Value = value; GameController.AddNewState(GameState.ViewingHighScores); int x; x = (SCORES_LEFT + SwinGame.TextWidth(GameResources.GameFont("Courier"), "Name: ")); SwinGame.StartReadingText(Color.Black, NAME_WIDTH, GameResources.GameFont("Courier"), x, ENTRY_TOP); // Read the text from the user while (SwinGame.ReadingText()) { SwinGame.ProcessEvents(); UtilityFunctions.DrawBackground(); HighScoreController.DrawHighScores(); SwinGame.DrawText("Name: ", Color.Black, GameResources.GameFont("Courier"), SCORES_LEFT, ENTRY_TOP); SwinGame.RefreshScreen(); } s.Name = SwinGame.TextReadAsASCII(); if ((s.Name.Length < 3)) { s.Name = (s.Name + new string(((char)(' ')), (3 - s.Name.Length))); } _Scores.RemoveAt((_Scores.Count - 1)); _Scores.Add(s); _Scores.Sort(); SaveScores(); GameController.EndCurrentState(); } }
// '' <summary> // '' Draws the current state of the game to the screen. // '' </summary> // '' <remarks> // '' What is drawn depends upon the state of the game. // '' </remarks> public static void DrawScreen() { UtilityFunctions.DrawBackground(); switch (CurrentState) { case GameState.ViewingMainMenu: MenuController.DrawMainMenu(); break; case GameState.ViewingGameMenu: MenuController.DrawGameMenu(); break; case GameState.AlteringSettings: MenuController.DrawSettings(); break; case GameState.Deploying: DeploymentController.DrawDeployment(); break; case GameState.Discovering: DiscoveryController.DrawDiscovery(); break; case GameState.EndingGame: EndingGameController.DrawEndOfGame(); break; case GameState.ViewingHighScores: HighScoreController.DrawHighScores(); break; case GameState.QuitConfirm: MenuController.DrawQuit(); break; } UtilityFunctions.DrawAnimations(); SwinGame.RefreshScreen(); }
// '' <summary> // '' Handles the user SwinGame. // '' </summary> // '' <remarks> // '' Reads key and mouse input and converts these into // '' actions for the game to perform. The actions // '' performed depend upon the state of the game. // '' </remarks> public static void HandleUserInput() { // Read incoming input events SwinGame.ProcessEvents(); switch (CurrentState) { case GameState.ViewingMainMenu: MenuController.HandleMainMenuInput(); break; case GameState.ViewingGameMenu: MenuController.HandleGameMenuInput(); break; case GameState.AlteringSettings: MenuController.HandleSetupMenuInput(); break; case GameState.Deploying: DeploymentController.HandleDeploymentInput(); break; case GameState.Discovering: DiscoveryController.HandleDiscoveryInput(); break; case GameState.EndingGame: EndingGameController.HandleEndOfGameInput(); break; case GameState.ViewingHighScores: HighScoreController.HandleHighScoreInput(); break; case GameState.QuitConfirm: MenuController.HandleQuitMenuInput(); break; } UtilityFunctions.UpdateAnimations(); }
// Reads the users name for their high score public static void ReadHighScore(int value) { const int ENTRY_TOP = 500; if (_Scores.Count == 0) { LoadScores(); } if (value > _Scores[_Scores.Count - 1].Value) { Score s = new Score(); s.Value = value; GameController.AddNewState(GameState.ViewingHighScores); int x; x = SCORES_LEFT + SwinGame.TextWidth(SwinGame.FontNamed("Courier"), "Name: "); SwinGame.StartReadingText(Color.White, NAME_WIDTH, GameResources.GameFont("Courier"), x, ENTRY_TOP); while (SwinGame.ReadingText()) { SwinGame.ProcessEvents(); UtilityFunctions.DrawBackground(); DrawHighScores(); SwinGame.DrawText("Name: ", Color.White, GameResources.GameFont("Courier"), SCORES_LEFT, ENTRY_TOP); SwinGame.RefreshScreen(); } s.Name = SwinGame.TextReadAsASCII(); if (s.Name.Length < 3) { s.Name = s.Name + new string(Convert.ToChar(" "), 3 - s.Name.Length); } _Scores.RemoveAt(_Scores.Count - 1); _Scores.Add(s); _Scores.Sort(); SaveScores(); GameController.EndCurrentState(); } }
// '' <summary> // '' Draw the end of the game screen, shows the win/lose state // '' </summary> public static void DrawEndOfGame() { Rectangle toDraw = new Rectangle(); string whatShouldIPrint; UtilityFunctions.DrawField(GameController.ComputerPlayer.PlayerGrid, GameController.ComputerPlayer, true); UtilityFunctions.DrawSmallField(GameController.HumanPlayer.PlayerGrid, GameController.HumanPlayer); toDraw.X = 0; toDraw.Y = 250; toDraw.Width = SwinGame.ScreenWidth(); toDraw.Height = SwinGame.ScreenHeight(); if (GameController.HumanPlayer.IsDestroyed) { whatShouldIPrint = "YOU LOSE!"; } else { whatShouldIPrint = "-- WINNER --"; } SwinGame.DrawText(whatShouldIPrint, Color.White, Color.Transparent, GameResources.GameFont("ArialLarge"), FontAlignment.AlignCenter, toDraw); }
// Draws the game field and interface during the attack phase. public static void DrawDiscovery() { const int SCORES_LEFT = 172; const int SHOTS_TOP = 157; const int HITS_TOP = 206; const int SPLASH_TOP = 256; if ((SwinGame.KeyDown(KeyCode.LeftShiftKey) | SwinGame.KeyDown(KeyCode.RightShiftKey)) & SwinGame.KeyDown(KeyCode.CKey)) { UtilityFunctions.DrawField(GameController.HumanPlayer.EnemyGrid, GameController.ComputerPlayer, true); } else { UtilityFunctions.DrawField(GameController.HumanPlayer.EnemyGrid, GameController.ComputerPlayer, false); } UtilityFunctions.DrawSmallField(GameController.HumanPlayer.PlayerGrid, GameController.HumanPlayer); UtilityFunctions.DrawMessage(); SwinGame.DrawText(GameController.HumanPlayer.Shots.ToString(), Color.White, GameResources.GameFont("Menu"), SCORES_LEFT, SHOTS_TOP); SwinGame.DrawText(GameController.HumanPlayer.Hits.ToString(), Color.White, GameResources.GameFont("Menu"), SCORES_LEFT, HITS_TOP); SwinGame.DrawText(GameController.HumanPlayer.Missed.ToString(), Color.White, GameResources.GameFont("Menu"), SCORES_LEFT, SPLASH_TOP); }
/// <summary> /// ''' Draws the deployment screen showing the field and the ships /// ''' that the player can deploy. /// ''' </summary> public static void DrawDeployment() { UtilityFunctions.DrawField(GameController.HumanPlayer.PlayerGrid, GameController.HumanPlayer, true); // Draw the Left/Right and Up/Down buttons if (_currentDirection == Direction.LeftRight) { SwinGame.DrawBitmap(GameResources.GameImage("LeftRightButton"), LEFT_RIGHT_BUTTON_LEFT, TOP_BUTTONS_TOP); } else { SwinGame.DrawBitmap(GameResources.GameImage("UpDownButton"), LEFT_RIGHT_BUTTON_LEFT, TOP_BUTTONS_TOP); } // DrawShips foreach (ShipName sn in Enum.GetValues(typeof(ShipName))) { int i; i = (int)sn - 1; if (i >= 0) { if (sn == _selectedShip) { SwinGame.DrawBitmap(GameResources.GameImage("SelectedShip"), SHIPS_LEFT, SHIPS_TOP + i * SHIPS_HEIGHT); } } } if (GameController.HumanPlayer.ReadyToDeploy) { SwinGame.DrawBitmap(GameResources.GameImage("PlayButton"), PLAY_BUTTON_LEFT, TOP_BUTTONS_TOP); } SwinGame.DrawBitmap(GameResources.GameImage("RandomButton"), RANDOM_BUTTON_LEFT, TOP_BUTTONS_TOP); UtilityFunctions.DrawMessage(); }
/// <summary> /// Draws the current state of the game to the screen. /// </summary> /// <remarks> /// What is drawn depends upon the state of the game. /// </remarks> public static void DrawScreen() { UtilityFunctions.DrawBackground(); if (CurrentState == GameState.ViewingMainMenu) { MenuController.DrawMainMenu(); } else if (CurrentState == GameState.ViewingGameMenu) { MenuController.DrawGameMenu(); } else if (CurrentState == GameState.AlteringSettings) { MenuController.DrawSettings(); } else if (CurrentState == GameState.Deploying) { DeploymentController.DrawDeployment(); } else if (CurrentState == GameState.Discovering) { DiscoveryController.DrawDiscovery(); } else if (CurrentState == GameState.EndingGame) { EndingGameController.DrawEndOfGame(); } else if (CurrentState == GameState.ViewingHighScores) { HighScoreController.DrawHighScores(); } UtilityFunctions.DrawAnimations(); SwinGame.RefreshScreen(); }
/// <summary> /// Handles the user SwinGame. /// </summary> /// <remarks> /// Reads key and mouse input and converts these into /// actions for the game to perform. The actions /// performed depend upon the state of the game. /// </remarks> public static void HandleUserInput() { //Read incoming input events SwinGame.ProcessEvents(); if (CurrentState == GameState.ViewingMainMenu) { MenuController.HandleMainMenuInput(); } else if (CurrentState == GameState.ViewingGameMenu) { MenuController.HandleGameMenuInput(); } else if (CurrentState == GameState.AlteringSettings) { MenuController.HandleSetupMenuInput(); } else if (CurrentState == GameState.Deploying) { DeploymentController.HandleDeploymentInput(); } else if (CurrentState == GameState.Discovering) { DiscoveryController.HandleDiscoveryInput(); } else if (CurrentState == GameState.EndingGame) { EndingGameController.HandleEndOfGameInput(); } else if (CurrentState == GameState.ViewingHighScores) { HighScoreController.HandleHighScoreInput(); } UtilityFunctions.UpdateAnimations(); }
// '' <summary> // '' Draws a large field using the grid and the indicated player's ships. // '' </summary> // '' <param name="grid">the grid to draw</param> // '' <param name="thePlayer">the players ships to show</param> // '' <param name="showShips">indicates if the ships should be shown</param> public static void DrawField(ISeaGrid grid, Player thePlayer, bool showShips) { UtilityFunctions.DrawCustomField(grid, thePlayer, false, showShips, FIELD_LEFT, FIELD_TOP, FIELD_WIDTH, FIELD_HEIGHT, CELL_WIDTH, CELL_HEIGHT, CELL_GAP); }
public static void AddSplash(int row, int col) { UtilityFunctions.AddAnimation(row, col, "Splash"); }
/// <summary> /// Handles user input for the Deployment phase of the game. /// </summary> /// <remarks> /// Involves selecting the ships, deloying ships, changing the direction /// of the ships to add, randomising deployment, end then ending /// deployment /// </remarks> public static void HandleDeploymentInput() { if (SwinGame.KeyTyped(KeyCode.vk_ESCAPE)) { GameController.AddNewState(GameState.ViewingGameMenu); } else if (SwinGame.MouseClicked(MouseButton.LeftButton) & UtilityFunctions.IsMouseInRectangle(MENU_LEFT, MENU_TOP, MENU_WIDTH, MENU_WIDTH)) { GameController.AddNewState(GameState.ViewingGameMenu); } if (SwinGame.KeyTyped(KeyCode.vk_UP)) { _currentDirection = Direction.Up; } if (SwinGame.KeyTyped(KeyCode.vk_DOWN)) { _currentDirection = Direction.Down; } if (SwinGame.KeyTyped(KeyCode.vk_LEFT)) { _currentDirection = Direction.Left; } if (SwinGame.KeyTyped(KeyCode.vk_RIGHT)) { _currentDirection = Direction.Right; } if (SwinGame.KeyTyped(KeyCode.vk_r)) { GameController.HumanPlayer.RandomizeDeployment(); } if (SwinGame.MouseClicked(MouseButton.LeftButton)) { ShipName selected = default(ShipName); selected = GetShipMouseIsOver(); if (selected != ShipName.None) { _selectedShip = selected; } if (GameController.HumanPlayer.ReadyToDeploy && UtilityFunctions.IsMouseInRectangle(PLAY_BUTTON_LEFT, TOP_BUTTONS_TOP, PLAY_BUTTON_WIDTH, TOP_BUTTONS_HEIGHT)) { GameController.EndDeployment(); } else if (UtilityFunctions.IsMouseInRectangle(UP_BUTTON_LEFT, TOP_BUTTONS_TOP, DIR_BUTTONS_WIDTH, TOP_BUTTONS_HEIGHT)) { _currentDirection = Direction.Up; DoUpClick(); } else if (UtilityFunctions.IsMouseInRectangle(DOWN_BUTTON_LEFT, TOP_BUTTONS_TOP, DIR_BUTTONS_WIDTH, TOP_BUTTONS_HEIGHT)) { _currentDirection = Direction.Down; DoDownClick(); } else if (UtilityFunctions.IsMouseInRectangle(LEFT_BUTTON_LEFT, TOP_BUTTONS_TOP, DIR_BUTTONS_WIDTH, TOP_BUTTONS_HEIGHT)) { _currentDirection = Direction.Left; DoLeftClick(); } else if (UtilityFunctions.IsMouseInRectangle(RIGHT_BUTTON_LEFT, TOP_BUTTONS_TOP, DIR_BUTTONS_WIDTH, TOP_BUTTONS_HEIGHT)) { _currentDirection = Direction.Right; DoRightClick(); } else if (UtilityFunctions.IsMouseInRectangle(RANDOM_BUTTON_LEFT, TOP_BUTTONS_TOP, RANDOM_BUTTON_WIDTH, TOP_BUTTONS_HEIGHT)) { GameController.HumanPlayer.RandomizeDeployment(); } else if (UtilityFunctions.IsMouseInRectangle(MUTE_BUTTON_LEFT, MUTE_BUTTON_TOP, MUTE_BUTTON_WIDTH, MUTW_BUTTON_HEIGHT) && (Sound == true)) { SwinGame.StopMusic(); SwinGame.DrawText("AUDIO ON", Color.Grey, MUTE_BUTTON_LEFT, MUTE_BUTTON_TOP); Sound = false; } else if (UtilityFunctions.IsMouseInRectangle(MUTE_BUTTON_LEFT, MUTE_BUTTON_TOP, MUTE_BUTTON_WIDTH, MUTW_BUTTON_HEIGHT) && (Sound == false)) { SwinGame.PlayMusic(GameResources.GameMusic("Background")); SwinGame.DrawText("AUDIO OFF", Color.White, MUTE_BUTTON_LEFT, MUTE_BUTTON_TOP); Sound = true; } else { DoDeployClick(); } } }
/// <summary> /// Draws the deployment screen showing the field and the ships /// that the player can deploy. /// </summary> public static void DrawDeployment() { UtilityFunctions.DrawField(GameController.HumanPlayer.PlayerGrid, GameController.HumanPlayer, true); SwinGame.DrawBitmap(GameResources.GameImage("LeftButton"), LEFT_BUTTON_LEFT, TOP_BUTTONS_TOP); SwinGame.DrawBitmap(GameResources.GameImage("RightButton"), RIGHT_BUTTON_LEFT, TOP_BUTTONS_TOP); SwinGame.DrawBitmap(GameResources.GameImage("UpButton"), UP_BUTTON_LEFT, TOP_BUTTONS_TOP); SwinGame.DrawBitmap(GameResources.GameImage("DownButton"), DOWN_BUTTON_LEFT, TOP_BUTTONS_TOP); //Draw the Left/Right and Up/Down buttons if (_currentDirection == Direction.Left) { SwinGame.DrawRectangleOnScreen(Color.Yellow, LEFT_BUTTON_LEFT, TOP_BUTTONS_TOP, DIR_BUTTONS_WIDTH, TOP_BUTTONS_HEIGHT); //SwinGame.DrawBitmap(GameResources.GameImage("LeftButton"), LEFT_BUTTON_LEFT, TOP_BUTTONS_TOP); } else if (_currentDirection == Direction.Right) { SwinGame.DrawRectangleOnScreen(Color.Yellow, RIGHT_BUTTON_LEFT, TOP_BUTTONS_TOP, DIR_BUTTONS_WIDTH, TOP_BUTTONS_HEIGHT); //SwinGame.DrawBitmap(GameResources.GameImage("RightButton"), LEFT_BUTTON_LEFT, TOP_BUTTONS_TOP); } else if (_currentDirection == Direction.Up) { SwinGame.DrawRectangleOnScreen(Color.Yellow, UP_BUTTON_LEFT, TOP_BUTTONS_TOP, DIR_BUTTONS_WIDTH, TOP_BUTTONS_HEIGHT); // SwinGame.DrawBitmap(GameResources.GameImage("UpButton"), LEFT_BUTTON_LEFT, TOP_BUTTONS_TOP); } else if (_currentDirection == Direction.Down) { SwinGame.DrawRectangleOnScreen(Color.Yellow, DOWN_BUTTON_LEFT, TOP_BUTTONS_TOP, DIR_BUTTONS_WIDTH, TOP_BUTTONS_HEIGHT); // SwinGame.DrawBitmap(GameResources.GameImage("DownButton"), LEFT_BUTTON_LEFT, TOP_BUTTONS_TOP); } //DrawShips foreach (ShipName sn in Enum.GetValues(typeof(ShipName))) { int i = 0; i = System.Convert.ToInt32(System.Convert.ToInt32(sn) - 1); if (i >= 0) { if (sn == _selectedShip) { SwinGame.DrawBitmap(GameResources.GameImage("SelectedShip"), SHIPS_LEFT, SHIPS_TOP + i * SHIPS_HEIGHT); // SwinGame.FillRectangle(Color.LightBlue, SHIPS_LEFT, SHIPS_TOP + i * SHIPS_HEIGHT, SHIPS_WIDTH, SHIPS_HEIGHT) //Else // SwinGame.FillRectangle(Color.Gray, SHIPS_LEFT, SHIPS_TOP + i * SHIPS_HEIGHT, SHIPS_WIDTH, SHIPS_HEIGHT) } //SwinGame.DrawRectangle(Color.Black, SHIPS_LEFT, SHIPS_TOP + i * SHIPS_HEIGHT, SHIPS_WIDTH, SHIPS_HEIGHT) //SwinGame.DrawText(sn.ToString(), Color.Black, GameResources.GameFont("Courier"), SHIPS_LEFT + TEXT_OFFSET, SHIPS_TOP + i * SHIPS_HEIGHT) } } if (GameController.HumanPlayer.ReadyToDeploy) { SwinGame.DrawBitmap(GameResources.GameImage("PlayButton"), PLAY_BUTTON_LEFT, TOP_BUTTONS_TOP); //SwinGame.FillRectangle(Color.LightBlue, PLAY_BUTTON_LEFT, PLAY_BUTTON_TOP, PLAY_BUTTON_WIDTH, PLAY_BUTTON_HEIGHT) //SwinGame.DrawText("PLAY", Color.Black, GameResources.GameFont("Courier"), PLAY_BUTTON_LEFT + TEXT_OFFSET, PLAY_BUTTON_TOP) } SwinGame.DrawBitmap(GameResources.GameImage("RandomButton"), RANDOM_BUTTON_LEFT, TOP_BUTTONS_TOP); //SwinGame.DrawBitmap(GameResources.GameImage("MenuButton"), MENU_LEFT, MENU_TOP); if (Sound == true) { //SwinGame.DrawBitmap(GameResources.GameImage("SoundButton"), MUTE_BUTTON_LEFT, MUTE_BUTTON_TOP); SwinGame.DrawText("AUDIO", Color.White, MUTE_BUTTON_LEFT, MUTE_BUTTON_TOP); } else { SwinGame.DrawText("AUDIO", Color.Grey, MUTE_BUTTON_LEFT, MUTE_BUTTON_TOP); } UtilityFunctions.DrawMessage(); }
// Draws the deployment screen showing the field and the ships // that the player can deploy. public static void DrawDeployment() { UtilityFunctions.DrawField(GameController.HumanPlayer.PlayerGrid, GameController.HumanPlayer, true); // Draw the Left/Right and Up/Down buttons if ((_currentDirection == Direction.LeftRight)) { SwinGame.DrawBitmap(GameResources.GameImage("LeftRightButton"), LEFT_RIGHT_BUTTON_LEFT, TOP_BUTTONS_TOP); // CHECK // SwinGame.DrawText("U/D", Color.Gray, GameFont("Menu"), UP_DOWN_BUTTON_LEFT, TOP_BUTTONS_TOP) // SwinGame.DrawText("L/R", Color.White, GameFont("Menu"), LEFT_RIGHT_BUTTON_LEFT, TOP_BUTTONS_TOP) // CHECK } else { SwinGame.DrawBitmap(GameResources.GameImage("UpDownButton"), LEFT_RIGHT_BUTTON_LEFT, TOP_BUTTONS_TOP); // CHECK // SwinGame.DrawText("U/D", Color.White, GameFont("Menu"), UP_DOWN_BUTTON_LEFT, TOP_BUTTONS_TOP) // SwinGame.DrawText("L/R", Color.Gray, GameFont("Menu"), LEFT_RIGHT_BUTTON_LEFT, TOP_BUTTONS_TOP) } // CHECK // DrawShips foreach (ShipName sn in Enum.GetValues(typeof(ShipName))) { int i; i = ((int)(sn) - 1); if ((i >= 0)) { if ((sn == _selectedShip)) { SwinGame.DrawBitmap(GameResources.GameImage("SelectedShip"), SHIPS_LEFT, (SHIPS_TOP + (i * SHIPS_HEIGHT))); // CHECK // SwinGame.FillRectangle(Color.LightBlue, SHIPS_LEFT, SHIPS_TOP + i * SHIPS_HEIGHT, SHIPS_WIDTH, SHIPS_HEIGHT) // Else // SwinGame.FillRectangle(Color.Gray, SHIPS_LEFT, SHIPS_TOP + i * SHIPS_HEIGHT, SHIPS_WIDTH, SHIPS_HEIGHT) } // CHECK // SwinGame.DrawRectangle(Color.Black, SHIPS_LEFT, SHIPS_TOP + i * SHIPS_HEIGHT, SHIPS_WIDTH, SHIPS_HEIGHT) // SwinGame.DrawText(sn.ToString(), Color.Black, GameFont("Courier"), SHIPS_LEFT + TEXT_OFFSET, SHIPS_TOP + i * SHIPS_HEIGHT) // CHECK } } if (GameController.HumanPlayer.ReadyToDeploy) { SwinGame.DrawBitmap(GameResources.GameImage("PlayButton"), PLAY_BUTTON_LEFT, TOP_BUTTONS_TOP); //CHECK // SwinGame.FillRectangle(Color.LightBlue, PLAY_BUTTON_LEFT, PLAY_BUTTON_TOP, PLAY_BUTTON_WIDTH, PLAY_BUTTON_HEIGHT) // SwinGame.DrawText("PLAY", Color.Black, GameFont("Courier"), PLAY_BUTTON_LEFT + TEXT_OFFSET, PLAY_BUTTON_TOP) } SwinGame.DrawBitmap(GameResources.GameImage("RandomButton"), RANDOM_BUTTON_LEFT, TOP_BUTTONS_TOP); UtilityFunctions.DrawMessage(); }