private void UpdatePlayers(GameTime gameTime) { Bear bear0 = bears[0]; if (!bear0.Hit) { //can't move if antonio's been hit bear0.Idle = true; // Use the Keyboard / Dpad to update antonio if (currentKeyboardState.IsKeyDown(Keys.F) && previousKeyboardState.IsKeyUp(Keys.F) && !bear0.inAir) { bear0.inAir = true; bear0.Idle = false; bear0.velocity = 8; } else if (currentKeyboardState.IsKeyDown(Keys.D) && previousKeyboardState.IsKeyUp(Keys.D) && !bear0.Attacking) { bear0.Attacking = true; bear0.Idle = false; if (!bear0.inAir) { bear0.previousPunchTime = gameTime.TotalGameTime; } } else if (!(bear0.Attacking && !bear0.inAir)) // can move as long as you're not punching { if (currentKeyboardState.IsKeyDown(Keys.Left) || currentGamePadState.DPad.Left == ButtonState.Pressed) { bear0.Position.X -= playerMoveSpeed; bear0.Idle = false; //make sure player faces left if you move left bear0.FacingRight = false; } if (currentKeyboardState.IsKeyDown(Keys.Right) || currentGamePadState.DPad.Right == ButtonState.Pressed) { bear0.Position.X += playerMoveSpeed; bear0.Idle = false; //make sure player faces right if you move right bear0.FacingRight = true; } if (currentKeyboardState.IsKeyDown(Keys.Up) || currentGamePadState.DPad.Up == ButtonState.Pressed) { bear0.Position.Y -= playerMoveSpeed; bear0.Idle = false; } if (currentKeyboardState.IsKeyDown(Keys.Down) || currentGamePadState.DPad.Down == ButtonState.Pressed) { bear0.Position.Y += playerMoveSpeed; bear0.Idle = false; } } } if (players == 2) { Bear bear1 = bears[1]; // Gamepad controls player 2 if (!bear1.Hit) { //can't move if jose's punching or he's been hit bear1.Idle = true; // Use the Keyboard / Dpad to update antonio if (currentGamePadState.IsButtonDown(Buttons.A) && !previousGamePadState.IsButtonUp(Buttons.A) && !bear1.inAir) { bear1.inAir = true; bear1.Idle = false; bear1.velocity = 8; } else if (currentGamePadState.IsButtonDown(Buttons.X) && previousGamePadState.IsButtonUp(Buttons.X) && !bear1.Attacking) { bear1.Attacking = true; bear1.Idle = false; bear1.previousPunchTime = gameTime.TotalGameTime; } else if (!(bear1.Attacking && !bear1.inAir)) { float gamePadX = currentGamePadState.ThumbSticks.Left.X / Math.Max(.5f, Math.Abs(currentGamePadState.ThumbSticks.Left.X)); float gamePadY = currentGamePadState.ThumbSticks.Left.Y / Math.Max(.5f, Math.Abs(currentGamePadState.ThumbSticks.Left.Y)); bear1.Position.X += gamePadX * playerMoveSpeed; bear1.Position.Y -= gamePadY * playerMoveSpeed; if ((gamePadX == 0) && (gamePadY == 0)) { bear1.Idle = true; } else { bear1.Idle = false; } //Make sure Jose is facing the right direction if (gamePadX == 1) { bear1.FacingRight = true; } else if (gamePadX == -1) { bear1.FacingRight = false; } } } } int frameWidth = GraphicsDevice.Viewport.Width; int frameHeight = GraphicsDevice.Viewport.Height; int skyHeight = frameHeight - 280; foreach (Bear bear in bears) { // Make sure that the player does not go out of bounds bear.Position.X = MathHelper.Clamp(bear.Position.X, xDistanceTraveled, xDistanceTraveled + frameWidth); bear.Position.Y = MathHelper.Clamp(bear.Position.Y, skyHeight, frameHeight); } //Logic for scrolling if (!enemiesPresent) { for (int i = 0; i < bears.Count; i++) { if (((bears[i].Position.X - xDistanceTraveled) > ((GraphicsDevice.Viewport.Width * 3) / 4)) && !bears[i].Idle) // && !bears[i].Attacking) { if (players == 2) //if 2 player, make sure other player isn't preventing scrolling { Bear otherBear = bears[(i + 1) % 2]; if (otherBear.Position.X <= xDistanceTraveled && otherBear.Active) { //Don't scroll if the other bear is on the left scrolling = false; break; } else { //other bear isn't on left side - scroll away scrolling = true; xDistanceTraveled += (int)playerMoveSpeed; break; } } else { scrolling = true; xDistanceTraveled += (int)playerMoveSpeed; } } else { scrolling = false; } } } foreach (Bear bear in bears) { bear.Update(gameTime); } }
/// <summary> /// This is called when the game should draw itself. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.LightBlue); // Start drawing spriteBatch.Begin(); if (level > 0) { //draw the background //spriteBatch.Draw(backgroundTexture, backgroundPosition, null, Color.White, 0f, Vector2.Zero, 1f, SpriteEffects.None, 0f); //desert.Draw(spriteBatch); skyBackground.Draw(spriteBatch); mountainsBackground.Draw(spriteBatch); desertBackground.Draw(spriteBatch); /* debugText += "Antonio: " + antonio.Position.X + ", " + antonio.Position.Y; * * if (enemies.Count > 0) * { * string cactpos = "" + enemies[0].Position.X + ", " + enemies[0].Position.Y; * debugText += "\nCactus: " + cactpos; * } */ //debugText += "Kills: " + killCounter + "\n"; //debugText += "Antonio: " + antonio.Health; // Draw my Debug text //reset the debug text debugText = ""; scoreBar = ""; //Make a list to hold all the non-background stuff that needs to get drawn //sort it by position List <DrawObject> stuffToDraw = new List <DrawObject>(); foreach (Bear bear in bears) { stuffToDraw.Add(bear); } // stuffToDraw.Add(taco); foreach (Box box in currentBoxes) { stuffToDraw.Add(box); } foreach (Taco taco in currentTacos) { stuffToDraw.Add(taco); } for (int i = 0; i < enemies.Count; i++) { stuffToDraw.Add(enemies[i]); } stuffToDraw.Sort(); for (int i = 0; i < stuffToDraw.Count; i++) { DrawObject obj = stuffToDraw[i]; obj.Draw(spriteBatch, xDistanceTraveled); } Bear bear0 = bears[0]; spriteBatch.Draw(AntonioHead, new Vector2(GraphicsDevice.Viewport.TitleSafeArea.X, GraphicsDevice.Viewport.TitleSafeArea.Y), null, Color.White, 0f, Vector2.Zero, 1f, SpriteEffects.None, 0f); for (int i = 0; i < bear0.Health; i++) { spriteBatch.Draw(TacoTexture, new Vector2(GraphicsDevice.Viewport.TitleSafeArea.X + AntonioHead.Width + (i * TacoTexture.Width * .5f), GraphicsDevice.Viewport.TitleSafeArea.Y), null, Color.White, 0f, Vector2.Zero, 1f, SpriteEffects.None, 0f); } if (bears.Count == 2) { spriteBatch.Draw(JoseHead, new Vector2(GraphicsDevice.Viewport.TitleSafeArea.Width - JoseHead.Width, GraphicsDevice.Viewport.TitleSafeArea.Y), null, Color.White, 0f, Vector2.Zero, 1f, SpriteEffects.None, 0f); for (int i = bears[1].Health; i > 0; i--) { //logic for jose's tacos (health) - have to draw left to right and end up at the edge X.X Vector2 TacoVector = new Vector2(GraphicsDevice.Viewport.TitleSafeArea.Width - JoseHead.Width - (.5f * TacoTexture.Width) - (i * TacoTexture.Width * .5f), GraphicsDevice.Viewport.TitleSafeArea.Y); spriteBatch.Draw(TacoTexture, TacoVector, null, Color.White, 0f, Vector2.Zero, 1f, SpriteEffects.None, 0f); } } //spriteBatch.Draw(RawrTexture, new Vector2(GraphicsDevice.Viewport.TitleSafeArea.Width / 2, GraphicsDevice.Viewport.TitleSafeArea.Y), null, Color.White, 0f, Vector2.Zero, 1f, SpriteEffects.None, 0f); instructionSign.Draw(spriteBatch, 0); //Draw all the text at the top and scoreboard stuff at end int scoreBarY = GraphicsDevice.Viewport.TitleSafeArea.Y; scoreBar += "Kills: " + killCounter; spriteBatch.DrawString(font, scoreBar, new Vector2((GraphicsDevice.Viewport.TitleSafeArea.Width / 2) - 50, scoreBarY), Color.Red); scoreBar = ""; scoreBarY += 50; if (gameOver && takingPlayerName) { scoreBar += ("\nGAME OVER\n\nEnter Name:" + playerName + currentLetter); spriteBatch.DrawString(font, scoreBar, new Vector2((GraphicsDevice.Viewport.TitleSafeArea.Width / 2) - 50, scoreBarY), Color.Red); } else if (gameOver && !takingPlayerName) { scoreBar += "GAME OVER\n----------------"; spriteBatch.DrawString(font, scoreBar, new Vector2((GraphicsDevice.Viewport.TitleSafeArea.Width / 2) - 50, scoreBarY), Color.Red); scoreBar = ""; scoreBarY += 100; for (int i = 1; i <= savedata.PlayerKills.Count; i++) { scoreBar += i + ". " + savedata.PlayerNames[i - 1] + " - " + savedata.PlayerKills[i - 1] + "\n"; spriteBatch.DrawString(font, scoreBar, new Vector2((GraphicsDevice.Viewport.TitleSafeArea.Width / 2) - 50, scoreBarY), Color.Red); scoreBar = ""; scoreBarY += 50; } } } else //draw menu { spriteBatch.Draw(menuScreen, Vector2.Zero, null, Color.White); spriteBatch.Draw(menuIcon, new Vector2(380, 310 + menuIconPosition * 50), null, Color.White); } // Stop drawing spriteBatch.End(); base.Draw(gameTime); }
/// <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() { // Initialize the bears Bear antonio = new Bear(); Bear jose = new Bear(); bears = new List <Bear>(); bears.Add(antonio); bears.Add(jose); instructionSign = new InstructionSign(); // Set a constant player move speed playerMoveSpeed = 2.0f; //kill counter starts at zero killCounter = 0; enemiesRemaining = 0; enemiesPresent = false; //enemySpawner = new EnemySpawner(); levelSpawner = new LevelSpawner(); currentBoxes = new List <Box>(); currentTacos = new List <Taco>(); mountainsBackground = new Background(); desertBackground = new Background(); skyBackground = new Background(); scrolling = false; xDistanceTraveled = 0; //menu is level 0 level = 0; menuIconPosition = 0; // Initialize the enemies list enemies = new List <Enemy>(); // Set the time keepers to zero previousSpawnTime = TimeSpan.Zero; // Used to determine how fast enemy respawns enemySpawnTime = TimeSpan.FromSeconds(.39f); // Initialize our random number generator random = new Random(); debugText = ""; scoreBar = ""; savedata = new SaveGameData() { PlayerNames = new List <string> { "Don" }, PlayerKills = new List <int> { 12 } }; gameOver = false; takingPlayerName = false; playerName = ""; base.Initialize(); }