SHARE
TWEET

Snowball Fight

SuperLemrick May 25th, 2015 248 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ////////////////////////////////////////////////////////////////
  2. // Copyright 2013, CompuScholar, Inc.
  3. //
  4. // This source code is for use by the students and teachers who
  5. // have purchased the corresponding TeenCoder or KidCoder product.
  6. // It may not be transmitted to other parties for any reason
  7. // without the written consent of CompuScholar, Inc.
  8. // This source is provided as-is for educational purposes only.
  9. // CompuScholar, Inc. makes no warranty and assumes
  10. // no liability regarding the functionality of this program.
  11. //
  12. ////////////////////////////////////////////////////////////////
  13.  
  14. using System;
  15. using System.Collections.Generic;
  16. using System.Linq;
  17. using Microsoft.Xna.Framework;
  18. using Microsoft.Xna.Framework.Audio;
  19. using Microsoft.Xna.Framework.Content;
  20. using Microsoft.Xna.Framework.GamerServices;
  21. using Microsoft.Xna.Framework.Graphics;
  22. using Microsoft.Xna.Framework.Input;
  23. using Microsoft.Xna.Framework.Media;
  24. using Microsoft.Xna.Framework.Net;
  25. using Microsoft.Xna.Framework.Storage;
  26. using SpriteLibrary;
  27.  
  28.  
  29. namespace SnowBall_Fight
  30. {
  31.     /// <summary>
  32.     /// This is the main type for your game
  33.     /// </summary>
  34.     public class SnowBallFight : Microsoft.Xna.Framework.Game
  35.     {
  36.         // define the maximum possible speed for the snowball
  37.         const float MAX_SPEED = 10;
  38.  
  39.         // content and graphcics-related objects
  40.         private GraphicsDeviceManager graphics;
  41.         private SpriteBatch spriteBatch;
  42.  
  43.         // Textures used in this game
  44.         Texture2D arrowTexture;
  45.         Texture2D groundTexture;
  46.         Texture2D player1Texture;
  47.         Texture2D player2Texture;
  48.         Texture2D snowballTexture;
  49.         Texture2D snowfortTexture;
  50.        
  51.         // the members below are part of the game state
  52.         // and are provided as part of the Activity Starter.
  53.         private KeyboardState oldKeyboardState;
  54.  
  55.         private GamePadState oldGamePadState1;
  56.         private GamePadState oldGamePadState2;
  57.  
  58.         // this variable shows how fast the snowball was thrown
  59.         private float thrownSpeed = 0;
  60.  
  61.         // sprites for all of the game objects
  62.         private Sprite snowball;
  63.  
  64.         private Sprite snowFort1;
  65.         private Sprite snowFort2;
  66.  
  67.         private Sprite ground;
  68.  
  69.         private Sprite player1;
  70.         private Sprite player2;
  71.  
  72.         private Sprite directionArrow;
  73.  
  74.         // this variable keeps track of the current player turn
  75.         private int currentPlayer = 1;
  76.  
  77.         // this variable shows whether or not the game is over
  78.         private bool gameOver = false;
  79.  
  80.         // font and string for message display
  81.         private SpriteFont gameFont;
  82.         private String displayMessage = "";
  83.  
  84.         private bool isPlayer1Throwing = false;
  85.         private bool isPlayer2Throwing = false;
  86.  
  87.         private bool isPlayer1Hit = false;
  88.         private bool isPlayer2Hit = false;
  89.  
  90.         // This method is provided fully complete as part of the activity starter.
  91.         public SnowBallFight()
  92.         {
  93.             graphics = new GraphicsDeviceManager(this);
  94.             Content.RootDirectory = "Content";
  95.         }
  96.  
  97.         /// <summary>
  98.         /// Allows the game to perform any initialization it needs to before starting to run.
  99.         /// This is where it can query for any required services and load any non-graphic
  100.         /// related content.  Calling base.Initialize will enumerate through any components
  101.         /// and initialize them as well.
  102.         /// </summary>
  103.         // This method is provided fully complete as part of the activity starter.
  104.         protected override void Initialize()
  105.         {
  106.             // call base.Initialize() first to get LoadContent called
  107.             // (and therefore all textures initialized) prior to starting game!
  108.             base.Initialize();
  109.  
  110.             // now that we have loaded the textures, create sprites and assign images to sprites
  111.             initializeSprites();
  112.  
  113.             // now that all of the sprites are initialized, start the game
  114.             startGame();
  115.  
  116.  
  117.         }
  118.  
  119.         // This method is provided fully complete as part of the activity starter.
  120.         private void startGame()
  121.         {
  122.             // reset to a new game by clearing the gameOver flag and message
  123.             gameOver = false;
  124.             displayMessage = "";
  125.  
  126.             // start again with player 1
  127.             setPlayer(1);
  128.         }
  129.  
  130.         // This method is provided fully complete as part of the activity starter.
  131.         private void stopGame(String msg)
  132.         {
  133.             // update the display message and game over flag
  134.             displayMessage = msg;
  135.             gameOver = true;
  136.  
  137.             // hide the snow ball
  138.             snowball.IsAlive = false;
  139.         }
  140.  
  141.         // This method is provided fully complete as part of the activity starter.
  142.         private void initializeSprites()
  143.         {
  144.             // create a new snowball sprite, set the texture, and hide it initially
  145.             snowball = new Sprite();
  146.             snowball.SetTexture(snowballTexture);
  147.             snowball.IsAlive = false;
  148.             snowball.MaxSpeed = MAX_SPEED;  // can't throw any faster than this!
  149.  
  150.             // initialize and position the ground sprite
  151.             ground = new Sprite();
  152.             ground.SetTexture(groundTexture);
  153.             ground.UpperLeft = new Vector2(0, GraphicsDevice.Viewport.Height - 50);
  154.  
  155.             // initialize and position the two snow forts
  156.             snowFort1 = new Sprite();
  157.             snowFort1.SetTexture(snowfortTexture);
  158.             snowFort1.UpperLeft = new Vector2(150, ground.UpperLeft.Y - snowFort1.GetHeight());
  159.  
  160.             snowFort2 = new Sprite();
  161.             snowFort2.SetTexture(snowfortTexture);
  162.             snowFort2.UpperLeft = new Vector2(GraphicsDevice.Viewport.Width - 150 - snowFort2.GetWidth(), ground.UpperLeft.Y - snowFort2.GetHeight());
  163.  
  164.             // initialize and position the two player sprites
  165.             player1 = new Sprite();
  166.             player1.SetTexture(player1Texture, 7);
  167.             player1.ContinuousAnimation = false;    // this is not a continuous animation loop!
  168.             player1.AnimationInterval = 200;        // advance current frame each 200 ms when animating
  169.             player1.UpperLeft = new Vector2(20, GraphicsDevice.Viewport.Height - ground.GetHeight()- player1.GetHeight() + 5);
  170.  
  171.             player2 = new Sprite();
  172.             player2.SetTexture(player2Texture, 7);
  173.             player2.ContinuousAnimation = false;    // this is not a continuous animation loop!
  174.             player2.AnimationInterval = 200;        // advance current frame each 200 ms when animating
  175.             player2.UpperLeft = new Vector2(GraphicsDevice.Viewport.Width - 25 - player2.GetWidth(), GraphicsDevice.Viewport.Height - ground.GetHeight() - player2.GetHeight()+ 5);
  176.  
  177.             // initialize the arrow texture
  178.             // (all other arrrow sprite parameters will be set when we change players)
  179.             directionArrow = new Sprite();
  180.             directionArrow.SetTexture(arrowTexture);
  181.         }
  182.  
  183.         // This method is provided fully complete as part of the activity starter.
  184.         private void changePlayer()
  185.         {
  186.             // switch to player 2 if player 1, or vice versa
  187.             if (currentPlayer == 1)
  188.                 setPlayer(2);
  189.             else
  190.                 setPlayer(1);
  191.         }
  192.  
  193.         // This method is provided fully complete as part of the activity starter.
  194.         private void setPlayer(int newPlayer)
  195.         {
  196.             // if we are changing to player 1's turn
  197.             if (newPlayer == 1)
  198.             {
  199.                 // initialize arrow for player 1
  200.                 directionArrow.UpperLeft = new Vector2(player1.UpperLeft.X + player1.GetWidth(), player1.UpperLeft.Y);
  201.                 directionArrow.RotationAngle = 0.0f;
  202.                 directionArrow.Origin = new Vector2(0, directionArrow.GetHeight() / 2);
  203.                 directionArrow.Scale.X = 0.5f;
  204.  
  205.                 player2.setCurrentFrame(3);
  206.                 player1.setCurrentFrame(0);
  207.  
  208.             }
  209.             else // we are changing to player 2's turn
  210.             {
  211.                 // initialize arrow for player 2
  212.                 directionArrow.UpperLeft = new Vector2(player2.UpperLeft.X, player2.UpperLeft.Y);
  213.                 directionArrow.RotationAngle = 180.0f;
  214.                 directionArrow.Origin = new Vector2(0, directionArrow.GetHeight() / 2);
  215.                 directionArrow.Scale.X = 0.5f;
  216.  
  217.                 player1.setCurrentFrame(3);
  218.                 player2.setCurrentFrame(0);
  219.                 }
  220.  
  221.             // update current player
  222.             currentPlayer = newPlayer;
  223.            
  224.             // reset current thrown speed
  225.             thrownSpeed = 0;
  226.         }
  227.  
  228.  
  229.         /// <summary>
  230.         /// LoadContent will be called once per game and is the place to load
  231.         /// all of your content.
  232.         /// </summary>
  233.         // This method is provided fully complete as part of the activity starter.
  234.         protected override void LoadContent()
  235.         {
  236.             // Create a new SpriteBatch, which can be used to draw textures.
  237.             spriteBatch = new SpriteBatch(GraphicsDevice);
  238.  
  239.  
  240.             gameFont = Content.Load<SpriteFont>("GameFont");
  241.  
  242.             // load all of the textures used in the game
  243.             arrowTexture = Content.Load<Texture2D>("Images\\Arrow");
  244.             groundTexture = Content.Load<Texture2D>("Images\\Ground");
  245.             player1Texture = Content.Load<Texture2D>("Images\\Reindeer1_Strip");
  246.             player2Texture = Content.Load<Texture2D>("Images\\Reindeer2_Strip");
  247.             snowballTexture = Content.Load<Texture2D>("Images\\SnowBall");
  248.             snowfortTexture = Content.Load<Texture2D>("Images\\SnowFort");
  249.  
  250.         }
  251.  
  252.         /// <summary>
  253.         /// UnloadContent will be called once per game and is the place to unload
  254.         /// all content.
  255.         /// </summary>
  256.         // This method is provided fully complete as part of the activity starter.
  257.         protected override void UnloadContent()
  258.         {
  259.             // TODO: Unload any non ContentManager content here
  260.         }
  261.  
  262.         /// <summary>
  263.         /// Allows the game to run logic such as updating the world,
  264.         /// checking for collisions, gathering input, and playing audio.
  265.         /// </summary>
  266.         /// <param name="gameTime">Provides a snapshot of timing values.</param>
  267.         // This method is provided fully complete as part of the activity starter.
  268.         protected override void Update(GameTime gameTime)
  269.         {
  270.             // Allows the game to exit
  271.             if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
  272.                 this.Exit();
  273.  
  274.             // has some animation short already started
  275.             if (isPlayer1Throwing)
  276.             {
  277.                 player1.Animate(gameTime);   // advance to next frame if it's time
  278.                 if (!player1.IsAnimating())  // see if animation short has completed
  279.                 {
  280.                     snowball.IsAlive = true;
  281.                     isPlayer1Throwing = false;
  282.                 }
  283.             }
  284.             else if (isPlayer2Throwing)
  285.             {
  286.                 player2.Animate(gameTime);   // advance to next frame if it's time
  287.                 if (!player2.IsAnimating())  // see if animation short has completed
  288.                 {
  289.                     snowball.IsAlive = true;
  290.                     isPlayer2Throwing = false;
  291.                 }
  292.             }
  293.             else if (isPlayer1Hit)
  294.             {
  295.                 player1.Animate(gameTime);   // advance to next frame if it's time
  296.                 if (!player1.IsAnimating())  // see if animation short has completed
  297.                 {
  298.                     isPlayer1Hit = false;
  299.                 }
  300.             }
  301.             else if (isPlayer2Hit)
  302.             {
  303.                 player2.Animate(gameTime);   // advance to next frame if it's time
  304.                 if (!player2.IsAnimating())  // see if animation short has completed
  305.                 {
  306.                     isPlayer2Hit = false;
  307.                 }
  308.             }
  309.            
  310.             // handle all user input
  311.             handleKeyPress();
  312.  
  313.             //Optionally handle Xbox gamepad input
  314.             handleXboxGamepads();
  315.  
  316.             // move the snowball if in flight
  317.             moveSnowball();
  318.  
  319.             // check to see if the snowball hits anything
  320.             checkCollisions();
  321.  
  322.             base.Update(gameTime);
  323.         }
  324.  
  325.         // The student will complete part of this function during an activity
  326.         private void handleKeyPress()
  327.         {
  328.  
  329.             // ****************************************************************************************
  330.             // This part of the function provided complete as part of the starter activity
  331.  
  332.             if (currentKeyboard.IsKeyDown(Keys.Space) &&
  333.                 (!snowball.IsAlive))
  334.             {
  335.                 if (thrownSpeed < MAX_SPEED)
  336.                 {
  337.                     thrownSpeed += .2f;
  338.                     directionArrow.Scale.X += 0.05f;
  339.                 }
  340.             }
  341.             else if ((oldKeyboardState.IsKeyDown(Keys.Space)) &&
  342.             (!currentKeyboard.IsKeyDown(Keys.Space)))
  343.             {
  344.                 if (gameOver)
  345.                 {
  346.                     startGame();
  347.                 }
  348.                 else
  349.                 {
  350.                     if (!snowball.IsAlive)
  351.                     {
  352.                         throwSnowball();
  353.                     }
  354.                 }
  355.             }
  356.             // save old key state
  357.             KeyboardState currentKeyboard = Keyboard.GetState();
  358.  
  359.             if (oldKeyboardState == null)
  360.                 oldKeyboardState = currentKeyboard;
  361.  
  362.              // if right arrow is held down, make angle smaller
  363.             if (currentKeyboard.IsKeyDown(Keys.Right))
  364.             {
  365.                 directionArrow.RotationAngle -= 1;
  366.             }
  367.  
  368.             // if left arrow is held down, make angle bigger
  369.             if (currentKeyboard.IsKeyDown(Keys.Left))
  370.             {
  371.                 directionArrow.RotationAngle += 1;
  372.             }
  373.  
  374.             // keep angle with [0,90] degrees for player 1
  375.             if (currentPlayer == 1)
  376.             {
  377.                 if (directionArrow.RotationAngle < 0)
  378.                     directionArrow.RotationAngle = 0;
  379.                 if (directionArrow.RotationAngle > 90)
  380.                     directionArrow.RotationAngle = 90;
  381.             }
  382.             // keep angle with [90,180] degrees for player 2
  383.             if (currentPlayer == 2)
  384.             {
  385.                 if (directionArrow.RotationAngle < 90)
  386.                     directionArrow.RotationAngle = 90;
  387.                 if (directionArrow.RotationAngle > 180)
  388.                     directionArrow.RotationAngle = 180;
  389.             }
  390.  
  391.             // ****************************************************************************************
  392.             // Student will complete this section as part of the activity
  393.  
  394.            
  395.             // ****************************************************************************************
  396.  
  397.             oldKeyboardState = currentKeyboard;
  398.            
  399.         }
  400.  
  401.        
  402.         // OPTIONAL: The student may complete part of this function during an activity, if they are using Xbox gamepads
  403.         // If the student is not using gamepads, they do NOT have to complete this activity
  404.         private void handleXboxGamepads()
  405.         {
  406.             // ****************************************************************************************
  407.             // This part of the function provided complete as part of the starter activity
  408.  
  409.             //save the old gamepad states
  410.             GamePadState currentGamePad;
  411.             GamePadState oldGamePad;
  412.  
  413.             // If we are using gamepads, retreive the current state for the gamepad
  414.             // and set the correct old gamepad state
  415.             if (currentPlayer == 1)
  416.             {
  417.                 // get the state of player 1's gamepad
  418.                 currentGamePad = GamePad.GetState(PlayerIndex.One);
  419.                 oldGamePad = oldGamePadState1;
  420.             }
  421.             else
  422.             {
  423.                 // get the state of player 2's gamepad
  424.                 currentGamePad = GamePad.GetState(PlayerIndex.Two);
  425.                 oldGamePad = oldGamePadState2;
  426.             }
  427.  
  428.             // if right arrow is held down, make angle smaller
  429.             if (currentGamePad.ThumbSticks.Left.X > 0)
  430.             {
  431.                 directionArrow.RotationAngle -= 1;
  432.             }
  433.  
  434.             // if left arrow is held down, make angle bigger
  435.             if (currentGamePad.ThumbSticks.Left.X < 0)
  436.             {
  437.                 directionArrow.RotationAngle += 1;
  438.             }
  439.  
  440.             // keep angle with [0,90] degrees for player 1
  441.             if (currentPlayer == 1)
  442.             {
  443.                 if (directionArrow.RotationAngle < 0)
  444.                     directionArrow.RotationAngle = 0;
  445.                 if (directionArrow.RotationAngle > 90)
  446.                     directionArrow.RotationAngle = 90;
  447.             }
  448.             // keep angle with [90,180] degrees for player 2
  449.             if (currentPlayer == 2)
  450.             {
  451.                 if (directionArrow.RotationAngle < 90)
  452.                     directionArrow.RotationAngle = 90;
  453.                 if (directionArrow.RotationAngle > 180)
  454.                     directionArrow.RotationAngle = 180;
  455.             }
  456.  
  457.            
  458.  
  459.             //*****************************************************************************************************
  460.             // OPTIONAL: Student will complete this section as part of the activity if using Xbox Gamepad controllers
  461.  
  462.            
  463.  
  464.             //*****************************************************************************************************
  465.  
  466.             if (currentPlayer == 1)
  467.                 oldGamePadState1 = currentGamePad;
  468.             else
  469.                 oldGamePadState2 = currentGamePad;
  470.         }
  471.  
  472.         // This method is provided fully complete as part of the activity starter.
  473.         private void animateThrow()
  474.         {
  475.             // begin short throwing animation
  476.             if (currentPlayer == 1)
  477.             {
  478.                 isPlayer1Throwing = true;
  479.                 player1.StartAnimationShort(0, 2, 0);   // begin animation short
  480.             }
  481.             else
  482.             {
  483.                 isPlayer2Throwing = true;
  484.                 player2.StartAnimationShort(0, 2, 0);   // begin animation short
  485.             }
  486.         }
  487.  
  488.         // The student will complete this function as part of an activity
  489.         private void throwSnowball()
  490.         {
  491.             if (currentPlayer == 1)
  492.             {
  493.                 snowball.UpperLeft = new Vector2(player1.UpperLeft.X + 75, player1.UpperLeft.Y);
  494.             }
  495.             else
  496.             {
  497.                 snowball.UpperLeft = new Vector2(player2.UpperLeft.X, player1.UpperLeft.Y);
  498.             }
  499.             snowball.SetSpeedAndDirection(thrownSpeed, directionArrow.RotationAngle);
  500.  
  501.             animateThrow();
  502.         }
  503.  
  504.        
  505.         // The student will complete this function as part of an activity
  506.         private void moveSnowball()
  507.         {
  508.             if (snowball.IsAlive)
  509.             {
  510.                 snowball.Accelerate(0.0, .04);
  511.  
  512.                 snowball.MoveAndVanish(GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height);
  513.  
  514.                 if (snowball.IsAlive == false)
  515.                 {
  516.                     changePlayer();
  517.                 }
  518.             }
  519.  
  520.         }
  521.  
  522.         // This method is provided fully complete as part of the activity starter.
  523.         private void checkCollisions()
  524.         {
  525.             // see if snowball has hit player1
  526.             if (snowball.IsCollided(player1))
  527.             {
  528.                 // begin animation short showing hit player
  529.                 isPlayer1Hit = true;
  530.                 player1.StartAnimationShort(4, 6, 6);
  531.  
  532.                 // game is over
  533.                 stopGame("Player 2 wins!");
  534.             }
  535.  
  536.             // see if snowball has hit player2
  537.             if (snowball.IsCollided(player2))
  538.             {
  539.                 // begin animation short showing hit player
  540.                 isPlayer2Hit = true;
  541.                 player2.StartAnimationShort(4, 6, 6);
  542.  
  543.                 // game is over
  544.                 stopGame("Player 1 wins!");
  545.             }
  546.  
  547.             // see if snowball has hit ground
  548.             if (snowball.IsCollided(ground))
  549.             {
  550.                 // hide snowball and move to next player's turn
  551.                 snowball.IsAlive = false;
  552.                 changePlayer();
  553.             }
  554.  
  555.             // see if snowball has hit snow fort 1
  556.             if (snowball.IsCollided(snowFort1))
  557.             {
  558.                 // hide snowball and move to next player's turn
  559.                 snowball.IsAlive = false;
  560.                 changePlayer();
  561.             }
  562.  
  563.             // see if snowball has hit snow fort 2
  564.             if (snowball.IsCollided(snowFort2))
  565.             {
  566.                 // hide snowball and move to next player's turn
  567.                 snowball.IsAlive = false;
  568.                 changePlayer();
  569.             }
  570.         }
  571.  
  572.         /// <summary>
  573.         /// This is called when the game should draw itself.
  574.         /// </summary>
  575.         /// <param name="gameTime">Provides a snapshot of timing values.</param>
  576.         // This method is provided fully complete as part of the activity starter.
  577.         protected override void Draw(GameTime gameTime)
  578.         {
  579.             // set the sky to a nice blue
  580.             GraphicsDevice.Clear(Color.LightBlue);
  581.  
  582.             spriteBatch.Begin();
  583.  
  584.             // Draw the ground, snow forts, person, snowball, and arrow
  585.             ground.Draw(spriteBatch);
  586.            
  587.             snowFort1.Draw(spriteBatch);
  588.             snowFort2.Draw(spriteBatch);
  589.  
  590.             player1.Draw(spriteBatch);
  591.             player2.Draw(spriteBatch);
  592.  
  593.             // if game is over
  594.             if (gameOver)
  595.             {
  596.                 // show the game-over message
  597.                 spriteBatch.DrawString(gameFont, displayMessage, new Vector2(50, 100), Color.Black);
  598.             }
  599.             else
  600.             {
  601.                 // draw the snowball if it's alive
  602.                 snowball.Draw(spriteBatch);
  603.  
  604.                 // draw the current direction arrow
  605.                 directionArrow.Draw(spriteBatch);
  606.             }
  607.  
  608.             spriteBatch.End();
  609.  
  610.             base.Draw(gameTime);
  611.         }
  612.  
  613.  
  614.     }
  615. }
RAW Paste Data
Want to get better at C#?
Learn to code C# in 2017
Pastebin PRO Summer Special!
Get 40% OFF on Pastebin PRO accounts!
Top