SHARE
TWEET

Swarm Unfinished Source Code

SuperLemrick Mar 28th, 2015 195 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.  
  27. using SpriteLibrary;
  28.  
  29. namespace Swarm
  30. {
  31.     /// <summary>
  32.     /// This is the main type for your game
  33.     /// </summary>
  34.     public class Swarm : Microsoft.Xna.Framework.Game
  35.     {
  36.         GraphicsDeviceManager graphics;
  37.         SpriteBatch spriteBatch;
  38.        
  39.         // the following constants control how many of each type of sprite are possible
  40.         const int NUM_COLS = 5;
  41.         const int NUM_BEES_PER_COL = 4;
  42.         const int MAX_BEE_SHOTS = 5;
  43.         const int MAX_SMOKE_SHOTS = 2;
  44.  
  45.         const int NUM_HIVES = 3;
  46.         const int NUM_HONEYCOMBS = 20;
  47.  
  48.         // initialize a new random number generator
  49.         Random randomNumGen = new Random(DateTime.Now.Millisecond);
  50.  
  51.         // font used to display user messages
  52.         SpriteFont gameFont;
  53.  
  54.         // textures used in the game
  55.         Texture2D beeTexture;
  56.         Texture2D beeShotTexture;
  57.         Texture2D beeStripTexture;
  58.         Texture2D hive1Texture;
  59.         Texture2D hive2Texture;
  60.         Texture2D smokeTexture;
  61.         Texture2D smokeSprayerTexture;
  62.         Texture2D smokeSprayerStripTexture;
  63.         Texture2D smokeStripTexture;
  64.  
  65.  
  66.         // the following members form the Game State for this program!
  67.  
  68.         // this sprite represents the player at the bottom
  69.         Sprite smokeSprayer;
  70.        
  71.         // these sprites represent the smoke balls fired from the smoke gun
  72.         LinkedList<Sprite> smokeShots = new LinkedList<Sprite>();
  73.  
  74.         // these sprites represent the individual honeycomb sections of the hive bunkers
  75.         LinkedList<Sprite> hiveSections = new LinkedList<Sprite>();
  76.  
  77.         // these sprites represent the bees in the swarm
  78.         LinkedList<Sprite> bees = new LinkedList<Sprite>();
  79.  
  80.         // these sprites represent the stinger shots fired from the bees
  81.         LinkedList<Sprite> beeShots = new LinkedList<Sprite>();
  82.  
  83.         // the time the most recent bee shot was fired
  84.         double lastBeeShotFired = 0;
  85.  
  86.         // the current bee movement speed
  87.         float beeSpeed = 1.0f;
  88.  
  89.         // if true, the current game is over
  90.         bool gameOver = false;
  91.  
  92.         // this string contains the message that is currently displayed to the user
  93.         String displayMessage = "";
  94.  
  95.         // previous keyboard state
  96.         KeyboardState oldKeyboardState;
  97.  
  98.         //previous Xbox controller state
  99.         GamePadState oldGamePadState;
  100.  
  101.         public Swarm()
  102.         {
  103.             graphics = new GraphicsDeviceManager(this);
  104.             Content.RootDirectory = "Content";
  105.         }
  106.  
  107.         /// <summary>
  108.         /// Allows the game to perform any initialization it needs to before starting to run.
  109.         /// This is where it can query for any required services and load any non-graphic
  110.         /// related content.  Calling base.Initialize will enumerate through any components
  111.         /// and initialize them as well.
  112.         /// </summary>
  113.         // This method is provided fully complete as part of the activity starter.
  114.         protected override void Initialize()
  115.         {
  116.             // call base.Initialize() first to get LoadContent called
  117.             // (and therefore all textures initialized) prior to starting game!
  118.             base.Initialize();
  119.  
  120.             startGame();
  121.         }
  122.  
  123.         // This method is provided partially complete as part of the activity starter.
  124.         private void startGame()
  125.         {
  126.             // reset game over flag
  127.             gameOver = false;
  128.  
  129.             // create new smoke sprayer, bees, and hives
  130.             initializeSmokeSprayer();
  131.             initializeBees();
  132.             initializeHives();
  133.  
  134.             // clear all prior shots
  135.             smokeShots.Clear();
  136.             beeShots.Clear();
  137.  
  138.         }
  139.  
  140.  
  141.         // The student will complete this function as part of an activity
  142.         private void initializeSmokeSprayer()
  143.         {
  144.             smokeSprayer = new Sprite();
  145.  
  146.             smokeSprayer.SetTexture(smokeSprayerTexture);
  147.  
  148.             int startingX = GraphicsDevice.Viewport.Width / 2;
  149.             int startingY = GraphicsDevice.Viewport.Height - smokeSprayer.GetHeight() - 20;
  150.             smokeSprayer.UpperLeft = new Vector2(startingX, startingY);
  151.  
  152.             smokeSprayer.IsAlive = true;
  153.         }
  154.  
  155.         // This method is provided fully complete as part of the activity starter.
  156.         private void initializeHives()
  157.         {
  158.             // this method will initialize all of the little hive sections as
  159.             // individual sprites that can be destroyed by shots and bees
  160.             hiveSections.Clear();
  161.  
  162.             // pick starting Y location
  163.             float hiveStartingY = GraphicsDevice.Viewport.Height - 150;
  164.             if (smokeSprayer != null)
  165.                 hiveStartingY = smokeSprayer.UpperLeft.Y - 100;
  166.            
  167.             // spacing between hives
  168.             float hiveSpacing = 200;
  169.  
  170.             // for each hive
  171.             for (int i = 0; i < NUM_HIVES; i++)
  172.             {
  173.                 // for each honeycomb in the hive
  174.                 for (int j = 0; j < NUM_HONEYCOMBS; j++)
  175.                 {
  176.                     // create a new sprite
  177.                     Sprite hiveSection = new Sprite();
  178.  
  179.                     // alternate the colors on odd/even blocks
  180.                     if (j % 2 == 0)
  181.                         hiveSection.SetTexture(hive1Texture);
  182.                     else
  183.                         hiveSection.SetTexture(hive2Texture);
  184.  
  185.                     // first 8 squares go along the bottom
  186.                     if (j < 8)
  187.                     {
  188.                         hiveSection.UpperLeft.Y = hiveStartingY + 3 * hiveSection.GetHeight();
  189.                         hiveSection.UpperLeft.X = ((hiveSpacing * (i + 1)) + (j * hiveSection.GetWidth()));
  190.                     }
  191.                     // next 6 squares go along the middle
  192.                     else if (j < 14)
  193.                     {
  194.                         hiveSection.UpperLeft.Y = hiveStartingY + 2 * hiveSection.GetHeight();
  195.                         hiveSection.UpperLeft.X = ((hiveSpacing * (i + 1)) + ((j - 7) * hiveSection.GetWidth()));
  196.                     }
  197.                     // next 4 squares along the top
  198.                     else if (j < 18)
  199.                     {
  200.                         hiveSection.UpperLeft.Y = hiveStartingY + hiveSection.GetHeight();
  201.                         hiveSection.UpperLeft.X = ((hiveSpacing * (i + 1)) + ((j - 12) * hiveSection.GetWidth()));
  202.                     }
  203.                     // small group of squares at the peak
  204.                     else
  205.                     {
  206.                         hiveSection.UpperLeft.Y = hiveStartingY;
  207.                         hiveSection.UpperLeft.X = ((hiveSpacing * (i + 1)) + ((j - 15) * hiveSection.GetWidth()));
  208.                     }
  209.  
  210.                     // set hive section to alive and add to list
  211.                     hiveSection.IsAlive = true;
  212.  
  213.                     hiveSections.AddLast(hiveSection);
  214.                 }
  215.             }
  216.         }
  217.  
  218.  
  219.         // The student will complete this function as part of an activity
  220.         private void initializeBees()
  221.         {
  222.             bees.Clear();
  223.  
  224.             for (int i = 0; i < NUM_COLS; i++)
  225.             {
  226.                 for (int j = 0; j < NUM_BEES_PER_COL; j++)
  227.                 {
  228.                     Sprite bee = new Sprite();
  229.  
  230.                     bee.UpperLeft = new Vector2((i + 1) * 100.0f, j * 75.0f);
  231.  
  232.                     bee.SetTexture(beeTexture);
  233.                     bees.AddLast(bee);
  234.                     bee.SetSpeedAndDirection(beeSpeed, 0);
  235.                 }
  236.             }
  237.         }
  238.  
  239.         // The student will complete this function as part of an activity
  240.         private void stopGame(bool won, String message)
  241.         {
  242.  
  243.         }
  244.  
  245.  
  246.  
  247.         /// <summary>
  248.         /// LoadContent will be called once per game and is the place to load
  249.         /// all of your content.
  250.         /// </summary>
  251.         // This method is provided partially complete as part of the activity starter.
  252.         protected override void LoadContent()
  253.         {
  254.             // Create a new SpriteBatch, which can be used to draw textures.
  255.             spriteBatch = new SpriteBatch(GraphicsDevice);
  256.  
  257.             // load all textures used in the game
  258.             beeTexture = Content.Load<Texture2D>("Images\\Bee");
  259.             beeShotTexture = Content.Load<Texture2D>("Images\\BeeShot");
  260.             beeStripTexture = Content.Load<Texture2D>("Images\\BeeStrip");
  261.             hive1Texture = Content.Load<Texture2D>("Images\\Hive1");
  262.             hive2Texture = Content.Load<Texture2D>("Images\\Hive2");
  263.             smokeTexture = Content.Load<Texture2D>("Images\\Smoke");
  264.             smokeSprayerTexture = Content.Load<Texture2D>("Images\\SmokeSprayer");
  265.             smokeSprayerStripTexture = Content.Load<Texture2D>("Images\\SmokeSprayerStrip");
  266.             smokeStripTexture = Content.Load<Texture2D>("Images\\SmokeStrip");
  267.  
  268.             // load all fonts used in the game
  269.             gameFont = Content.Load<SpriteFont>("GameFont");
  270.  
  271.            
  272.         }
  273.  
  274.         /// <summary>
  275.         /// UnloadContent will be called once per game and is the place to unload
  276.         /// all content.
  277.         /// </summary>
  278.         // This method is provided fully complete as part of the activity starter.
  279.         protected override void UnloadContent()
  280.         {
  281.             // TODO: Unload any non ContentManager content here
  282.         }
  283.  
  284.         /// <summary>
  285.         /// Allows the game to run logic such as updating the world,
  286.         /// checking for collisions, gathering input, and playing audio.
  287.         /// </summary>
  288.         /// <param name="gameTime">Provides a snapshot of timing values.</param>
  289.         // This method is provided fully complete as part of the activity starter.
  290.         protected override void Update(GameTime gameTime)
  291.         {
  292.             // Allows the game to exit
  293.             if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
  294.                 this.Exit();
  295.  
  296.             // if the game is still going on
  297.             if (!gameOver)
  298.             {
  299.                 // check to see if the player has beaten all the bees
  300.                 if (bees.Count == 0)
  301.                 {
  302.                     stopGame(true,"You have defeated the swarm!");
  303.                 }
  304.                 else
  305.                 {
  306.  
  307.                     // see if any more bee stingers need to be fired
  308.                     checkBeeShots(gameTime);
  309.  
  310.                     // move the bees
  311.                     moveBees(gameTime);
  312.  
  313.                     // move any bee shots that are on the screen
  314.                     moveBeeShots();
  315.  
  316.                     // move any smoke shots that are on the screen
  317.                     moveSmokeShots(gameTime);
  318.  
  319.                     // handle all collisions
  320.                     checkCollisions();
  321.                 }
  322.             }
  323.  
  324.             // if the smoke sprayer is currently animating, advance the frame
  325.             if (smokeSprayer != null)
  326.                 if (smokeSprayer.IsAnimating())
  327.                         smokeSprayer.Animate(gameTime);
  328.  
  329.             // handle all user input
  330.             handleKeyPress();
  331.  
  332.             // Optionally handle Xbox gamepad
  333.             handleXboxGamepad();
  334.            
  335.             base.Update(gameTime);
  336.         }
  337.  
  338.         // The student will complete this function as part of an activity
  339.         private void checkBeeShots(GameTime gameTime)
  340.         {
  341.  
  342.         }
  343.  
  344.         // The student will complete this function as part of an activity
  345.         private void fireBeeShot()
  346.         {
  347.  
  348.         }
  349.  
  350.         // This method is provided fully complete as part of the activity starter.
  351.         private Sprite findEdgeBee(bool leftSide)
  352.         {
  353.             // create variable to trach the bee we think is on the edge
  354.             Sprite edgeSprite = null;
  355.  
  356.             // check all remaining bees
  357.             foreach (Sprite bee in bees)
  358.             {
  359.                 // if this is the first bee, just save it as current edge bee
  360.                 if (edgeSprite == null)
  361.                     edgeSprite = bee;
  362.                 else
  363.                 {
  364.                     // if we are looking for the leftmost bee
  365.                     if (leftSide)
  366.                     {
  367.                         // if this bee is to left of current edge bee
  368.                         if (bee.UpperLeft.X < edgeSprite.UpperLeft.X)
  369.                             edgeSprite = bee;   // we have a new edge bee
  370.                     }
  371.                     else // we are looking for the rightmost bee
  372.                     {
  373.                         // if this bee is to right of current edge bee
  374.                         if (bee.UpperLeft.X > edgeSprite.UpperLeft.X)
  375.                             edgeSprite = bee;   // we have a new edge bee
  376.                     }
  377.                 }
  378.             }
  379.            
  380.             // return the edge bee we found
  381.             return edgeSprite;
  382.         }
  383.  
  384.         // The student will complete this function as part of an activity
  385.         private void moveSmokeShots(GameTime gameTime)
  386.         {
  387.  
  388.         }
  389.  
  390.         // The student will complete this function as part of an activity
  391.         private void moveBeeShots()
  392.         {
  393.  
  394.         }
  395.  
  396.         // The student will complete this function as part of an activity
  397.         private void moveBees(GameTime gameTime)
  398.         {
  399.             Sprite leftmostBee = findEdgeBee(true);
  400.             Sprite rightmostBee = findEdgeBee(false);
  401.             if ((leftmostBee == null) || (rightmostBee == null))
  402.             {
  403.                 return;
  404.             }
  405.  
  406.             if (((leftmostBee.UpperLeft.X) < 0) ||
  407.                  ((rightmostBee.UpperLeft.X + rightmostBee.GetWidth()) > GraphicsDevice.Viewport.Width))
  408.             {
  409.                 foreach (Sprite bee in bees)
  410.                 {
  411.                     Vector2 velocity = bee.GetVelocity();
  412.                     velocity.X *= -1;
  413.                     bee.SetVelocity(velocity.X,velocity.Y);
  414.  
  415.                     bee.UpperLeft.Y += 25;
  416.                 }
  417.             }
  418.             foreach (Sprite bee in bees)
  419.             {
  420.                 bee.Move();
  421.             }
  422.         }
  423.  
  424.         // The student will complete this function as part of an activity
  425.         private void checkCollisions()
  426.         {
  427.  
  428.             // remove all dead sprites from their lists
  429.             pruneList(beeShots);
  430.             pruneList(smokeShots);
  431.             pruneList(bees);
  432.             pruneList(hiveSections);
  433.         }
  434.  
  435.         // The student will complete this function as part of an activity
  436.         private void handleKeyPress()
  437.         {
  438.  
  439.         }
  440.  
  441.         //*************************************************************************************************************
  442.         // OPTIONAL: If the student is using Xbox gamepads, they can optionally complete this method
  443.         // If not, they should complete the handleKeyPress() method mentioned above.
  444.  
  445.         private void handleXboxGamepad()
  446.         {
  447.            
  448.         }
  449.         //*****************************************************************************************************************
  450.  
  451.  
  452.  
  453.         // The student will complete this function as part of an activity
  454.         private void shootSmokeSprayer()
  455.         {
  456.  
  457.         }
  458.  
  459.         /// <summary>
  460.         /// This is called when the game should draw itself.
  461.         /// </summary>
  462.         /// <param name="gameTime">Provides a snapshot of timing values.</param>
  463.         // The student will complete this function as part of an activity
  464.         protected override void Draw(GameTime gameTime)
  465.         {
  466.             GraphicsDevice.Clear(Color.SkyBlue);
  467.  
  468.             spriteBatch.Begin();
  469.  
  470.             // draw the smoke sprayer
  471.             if (smokeSprayer != null)
  472.                 smokeSprayer.Draw(spriteBatch);
  473.  
  474.             // draw all active smoke shots
  475.             foreach (Sprite smokeShot in smokeShots)
  476.             {
  477.                 smokeShot.Draw(spriteBatch);
  478.             }
  479.  
  480.             // draw all active bee shots
  481.             foreach (Sprite beeShot in beeShots)
  482.             {
  483.                 beeShot.Draw(spriteBatch);
  484.             }
  485.  
  486.             // draw all active bees
  487.             foreach (Sprite bee in bees)
  488.             {
  489.                 bee.Draw(spriteBatch);
  490.             }
  491.  
  492.             // draw all remaining hive sections
  493.             foreach (Sprite hiveSection in hiveSections)
  494.             {
  495.                 hiveSection.Draw(spriteBatch);
  496.             }
  497.  
  498.             //*****************************************************************
  499.             // Student will add some "game over" logic here in an activity
  500.  
  501.  
  502.             //*****************************************************************
  503.  
  504.             spriteBatch.End();
  505.  
  506.             base.Draw(gameTime);
  507.         }
  508.  
  509.  
  510.         // This method is provided fully complete as part of the activity starter.
  511.         private void pruneList(LinkedList<Sprite> spriteList)
  512.         {
  513.             // clip out any sprites from the list where IsAlive = false
  514.             for (int i = spriteList.Count - 1; i >= 0; i--)
  515.             {
  516.                 Sprite s = spriteList.ElementAt(i);
  517.                 if (!s.IsAlive)
  518.                 {
  519.                     spriteList.Remove(s);
  520.                 }
  521.             }
  522.         }
  523.  
  524.  
  525.     }
  526. }
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