/// <summary> /// Allows the game to perform any initialization it needs to before starting to run. /// This is where it can query for any required services and load any non-graphic /// related content. Calling base.Initialize will enumerate through any components /// and initialize them as well. /// </summary> protected override void Initialize() { // TODO: Add your initialization logic here allAsteroids = new ArrayList(); allItems = new Powerup[20]; rand = new Random((int)DateTime.Now.Ticks & 0x0000FFFF); player = new Player(); currentState = GameStates.StartMenu; buttonTextures = new Texture2D[11]; asteroidModels = new Model[7]; currentDifficulty = GameDifficulties.Medium; currentSize = GameSize.Medium; allExplosions = new Explosion[15]; //should be enough for (int i = 0; i < allExplosions.Length; i++) { allExplosions[i] = new Explosion(); } lives = 3; gameTimer = 0; goodToGo = false; base.Initialize(); }
public static void setDifficulty(GameDifficulties newDifficulty) { curDifficulty = newDifficulty; }
/// <summary> /// The main method of the Program. /// </summary> /// <param name="args">The input arguments.</param> static void Main(string[] args) { try { while (!gameFinished) { #region Menu // The menu stage. if (gameStatus == GameStages.Menu) { #region RefreshDisplay DisplayMenu(); #endregion RefreshDisplay // Repeat the key read until a valid key is pushed. while (gameStatus == GameStages.Menu) { #region HandleInput // A key was pushed. if (Console.KeyAvailable) { // Handle the key input. switch (Console.ReadKey().Key) { case ConsoleKey.D1: case ConsoleKey.NumPad1: gameStatus = GameStages.Game; break; case ConsoleKey.D2: case ConsoleKey.NumPad2: gameStatus = GameStages.Options; break; case ConsoleKey.D3: case ConsoleKey.NumPad3: gameStatus = GameStages.Exit; break; } // Delete the read key input. DeleteKeyInput(); } #endregion HandleInput } } #endregion Menu #region Game // The game stage. else if (gameStatus == GameStages.Game) { #region PreSet // Refresh the tron. trons.Clear(); trons.Add(new Tron(new Position(11, 14), Tron.TronColors.Red, Tron.Directions.Right, true)); trons.Add(new Tron(new Position(11, 63), Tron.TronColors.Blue, Tron.Directions.Left, true)); // Clears the map and repositions the trons. for (int i = 1; i < mapHeight - 1; i++) { for (int j = 1; j < mapWidth - 1; j++) { map[i, j] = ' '; } } // Put the trons on the map. map[trons[0].Position.X, trons[0].Position.Y] = trons[0].Draw(); map[trons[1].Position.X, trons[1].Position.Y] = trons[1].Draw(); // Set the game difficulty. switch (gameDifficulty) { case GameDifficulties.Easy: sleepTime = 65; break; case GameDifficulties.Normal: sleepTime = 50; break; case GameDifficulties.Hard: sleepTime = 40; break; } #endregion PreSet #region RefreshDisplay DisplayMap(); #endregion RefreshDisplay // Repeat while the game is active. while (gameStatus == GameStages.Game) { #region HandleInput // A key was pushed. if (Console.KeyAvailable) { // Handle the key input. switch (Console.ReadKey().Key) { case ConsoleKey.W: if (trons[0].Direction != Tron.Directions.Down) { trons[0].Direction = Tron.Directions.Up; } break; case ConsoleKey.UpArrow: if (trons[1].Direction != Tron.Directions.Down) { trons[1].Direction = Tron.Directions.Up; } break; case ConsoleKey.A: if (trons[0].Direction != Tron.Directions.Right) { trons[0].Direction = Tron.Directions.Left; } break; case ConsoleKey.LeftArrow: if (trons[1].Direction != Tron.Directions.Right) { trons[1].Direction = Tron.Directions.Left; } break; case ConsoleKey.D: if (trons[0].Direction != Tron.Directions.Left) { trons[0].Direction = Tron.Directions.Right; } break; case ConsoleKey.RightArrow: if (trons[1].Direction != Tron.Directions.Left) { trons[1].Direction = Tron.Directions.Right; } break; case ConsoleKey.S: if (trons[0].Direction != Tron.Directions.Up) { trons[0].Direction = Tron.Directions.Down; } break; case ConsoleKey.DownArrow: if (trons[1].Direction != Tron.Directions.Up) { trons[1].Direction = Tron.Directions.Down; } break; case ConsoleKey.Escape: gameStatus = GameStages.Menu; break; } // Delete the read key input. DeleteKeyInput(); } #endregion HandleInput #region HandleChanges // Only handle the changes in the game stage. if (gameStatus == GameStages.Game) { #region MoveTrons // The trons move. MoveTrons(); #endregion MoveTrons #region CollisionDetection if (trons[0].IsAlive == false && trons[1].IsAlive == false) { Console.Write("Draw."); Thread.Sleep(2000); } else if (trons[0].IsAlive == true && trons[1].IsAlive == false) { Console.Write("Red player wins."); Thread.Sleep(2000); } else if (trons[0].IsAlive == false && trons[1].IsAlive == true) { Console.Write("Blue player wins."); Thread.Sleep(2000); } #endregion CollisionDetection } #endregion HandleChanges Thread.Sleep(sleepTime); } } #endregion Game #region Options // The options stage. else if (gameStatus == GameStages.Options) { #region RefreshDisplay DisplayOptions(); optionsPointer = 1; #endregion RefreshDisplay // Repeat the key read until the escape is pushed. while (gameStatus == GameStages.Options) { #region HandleInput // A key was pushed. if (Console.KeyAvailable) { // Handle the key input. switch (Console.ReadKey().Key) { case ConsoleKey.Escape: gameStatus = GameStages.Menu; break; case ConsoleKey.LeftArrow: switch (optionsPointer) { case 1: if (coloredMap) { coloredMap = false; ChangeText(new Position(2, 7), "Off"); } else { coloredMap = true; ChangeText(new Position(2, 7), "On "); } break; case 2: if (gameDifficulty == GameDifficulties.Hard) { gameDifficulty = GameDifficulties.Normal; ChangeText(new Position(4, 12), "Normal"); } else if (gameDifficulty == GameDifficulties.Normal) { gameDifficulty = GameDifficulties.Easy; ChangeText(new Position(4, 12), " Easy "); } break; } break; case ConsoleKey.RightArrow: switch (optionsPointer) { case 1: if (coloredMap) { coloredMap = false; ChangeText(new Position(2, 7), "Off"); } else { coloredMap = true; ChangeText(new Position(2, 7), "On "); } break; case 2: if (gameDifficulty == GameDifficulties.Easy) { gameDifficulty = GameDifficulties.Normal; ChangeText(new Position(4, 12), "Normal"); } else if (gameDifficulty == GameDifficulties.Normal) { gameDifficulty = GameDifficulties.Hard; ChangeText(new Position(4, 12), " Hard "); } break; } break; case ConsoleKey.UpArrow: if (optionsPointer > 1) { optionsPointer--; ChangeOption(optionsPointer, optionsPointer + 1); } else { optionsPointer = optionsNumber; ChangeOption(optionsNumber, 1); } break; case ConsoleKey.DownArrow: if (optionsPointer < optionsNumber) { optionsPointer++; ChangeOption(optionsPointer, optionsPointer - 1); } else { optionsPointer = 1; ChangeOption(1, optionsNumber); } break; } // Delete the read key input. DeleteKeyInput(); } #endregion HandleInput } } #endregion Options #region Exit // The exit stage. else if (gameStatus == GameStages.Exit) { // End the program. gameFinished = true; } #endregion Exit } } catch { } }
/// <summary> /// Allows the game to run logic such as updating the world, /// checking for collisions, gathering input, and playing audio. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> protected override void Update(GameTime gameTime) { if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) { Exit(); } // TODO: Add your update logic here float delta = (float)gameTime.ElapsedGameTime.TotalSeconds; //the change in time since last update; if (currentState == GameStates.StartMenu) { //check for option changes //Mouse.GetState(). this.IsMouseVisible = true; if (Mouse.GetState().LeftButton == ButtonState.Pressed) { Point mousePos = Mouse.GetState().Position; if (mousePos.Y >= 200 && mousePos.Y <= 280) { if (mousePos.X >= 120 && mousePos.X <= 280) { currentDifficulty = GameDifficulties.Easy; } if (mousePos.X >= 320 && mousePos.X <= 480) { currentDifficulty = GameDifficulties.Medium; } if (mousePos.X >= 520 && mousePos.X <= 680) { currentDifficulty = GameDifficulties.Hard; } } if (mousePos.Y >= 330 && mousePos.Y <= 410) { if (mousePos.X >= 120 && mousePos.X <= 280) { currentSize = GameSize.Small; } if (mousePos.X >= 320 && mousePos.X <= 480) { currentSize = GameSize.Medium; } if (mousePos.X >= 520 && mousePos.X <= 680) { currentSize = GameSize.Large; } } } if (Keyboard.GetState().IsKeyDown(Keys.Enter)) { currentState = GameStates.Controls; } } if (currentState == GameStates.Controls) { if (Keyboard.GetState().IsKeyDown(Keys.Enter) && goodToGo) { currentState = GameStates.PrePlaying; } if (Keyboard.GetState().IsKeyUp(Keys.Enter)) { goodToGo = true; } } if (currentState == GameStates.PrePlaying) { //create our starting asteroids int numAsteroids = 0; int outerLimit = 0; if (currentSize == GameSize.Small) { numAsteroids = Constants.SMALLASTEROIDS; outerLimit = Constants.SMALLSIZE; } if (currentSize == GameSize.Medium) { numAsteroids = Constants.MEDIUMASTEROIDS; outerLimit = Constants.MEDIUMSIZE; } if (currentSize == GameSize.Large) { numAsteroids = Constants.LARGEASTEROIDS; outerLimit = Constants.LARGESIZE; } asteroidsStart = numAsteroids; int difficulty = 0; if (currentDifficulty == GameDifficulties.Easy) { difficulty = Constants.EASYMODE; } if (currentDifficulty == GameDifficulties.Medium) { difficulty = Constants.MEDMODE; } if (currentDifficulty == GameDifficulties.Hard) { difficulty = Constants.HARDMODE; } for (int i = 0; i < numAsteroids; i++) { allAsteroids.Add(new Asteroid(rand, outerLimit, difficulty)); asteroidsLeft++; } int j = 0; foreach (Asteroid a in allAsteroids) { if (j % 2 == 0) { a.bigModel = asteroidModels[0]; a.medModel = asteroidModels[1]; a.smallModel = asteroidModels[2]; } else { a.bigModel = asteroidModels[3]; a.medModel = asteroidModels[4]; a.smallModel = asteroidModels[5]; } j++; } player.setBounds(outerLimit); for (int i = 0; i < 20; i++) { Powerup p = new Powerup(rand, outerLimit); p.setModel(fuelModel, multiModel, speedModel); allItems[i] = (p); } this.IsMouseVisible = false; currentState = GameStates.Playing; } if (currentState == GameStates.Playing) { player.playerFlight(); //keyboard flight updateCamera(); //update camera position gameTimer += delta; itemTimer += delta; //spawn an item every 15 seconds if (itemTimer > 15) { allItems[currentItem].spawnPowerup(); currentItem++; currentItem %= (allItems.Length); itemTimer = 0; } //have all asteroid float in space foreach (Asteroid a in allAsteroids) { if (a.isActive) { a.updatePosition(delta); } } //update player stuff player.updateTimer(delta); //update explosions foreach (Explosion e in allExplosions) { if (e.isExploding) { e.updateTimer(delta); } } //bullets also fly through space foreach (Bullet b in player.bulletSupply) { b.updateBullet(); if (b.outOfBounds() && b.isActive) { //EXPLODE! createExplosion(b.position); b.isActive = false; explosionSound.Play(); } } foreach (Powerup p in allItems) { if (p.isActive) { p.updatePosition(delta); } } //see if stuff is colliding with other stuff checkCollisions(); if (asteroidsStart * .25F >= asteroidsLeft) { //You Win! currentState = GameStates.End; } } if (currentState == GameStates.Dead) { if (Keyboard.GetState().IsKeyDown(Keys.Enter)) { player.position = Vector3.Zero; currentState = GameStates.Playing; } if (lives == 0) { currentState = GameStates.End; } } if (currentState == GameStates.End) { if (Keyboard.GetState().IsKeyDown(Keys.Enter)) { Exit(); } } base.Update(gameTime); }
/// <summary> /// Gets the mode configuration. /// </summary> /// <returns>The mode configuration.</returns> /// <param name="mode">Mode.</param> /// <param name="difficulty">Difficulty.</param> public ModeConfigurationItem GetModeConfiguration(GameModes mode, GameDifficulties difficulty) { foreach (var config in ModesConfiguration) { if (config.Mode == mode && config.Difficulty == difficulty) { return config; } } return null; }