Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ////////////////////////////////////////////////////////////////
- // Copyright 2013, CompuScholar, Inc.
- //
- // This source code is for use by the students and teachers who
- // have purchased the corresponding TeenCoder or KidCoder product.
- // It may not be transmitted to other parties for any reason
- // without the written consent of CompuScholar, Inc.
- // This source is provided as-is for educational purposes only.
- // CompuScholar, Inc. makes no warranty and assumes
- // no liability regarding the functionality of this program.
- //
- ////////////////////////////////////////////////////////////////
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Audio;
- using Microsoft.Xna.Framework.Content;
- using Microsoft.Xna.Framework.GamerServices;
- using Microsoft.Xna.Framework.Graphics;
- using Microsoft.Xna.Framework.Input;
- using Microsoft.Xna.Framework.Media;
- using Microsoft.Xna.Framework.Net;
- using Microsoft.Xna.Framework.Storage;
- using SpriteLibrary;
- namespace Swarm
- {
- /// <summary>
- /// This is the main type for your game
- /// </summary>
- public class Swarm : Microsoft.Xna.Framework.Game
- {
- GraphicsDeviceManager graphics;
- SpriteBatch spriteBatch;
- // the following constants control how many of each type of sprite are possible
- const int NUM_COLS = 5;
- const int NUM_BEES_PER_COL = 4;
- const int MAX_BEE_SHOTS = 5;
- const int MAX_SMOKE_SHOTS = 2;
- const int NUM_HIVES = 3;
- const int NUM_HONEYCOMBS = 20;
- // initialize a new random number generator
- Random randomNumGen = new Random(DateTime.Now.Millisecond);
- // font used to display user messages
- SpriteFont gameFont;
- // textures used in the game
- Texture2D beeTexture;
- Texture2D beeShotTexture;
- Texture2D beeStripTexture;
- Texture2D hive1Texture;
- Texture2D hive2Texture;
- Texture2D smokeTexture;
- Texture2D smokeSprayerTexture;
- Texture2D smokeSprayerStripTexture;
- Texture2D smokeStripTexture;
- // the following members form the Game State for this program!
- // this sprite represents the player at the bottom
- Sprite smokeSprayer;
- // these sprites represent the smoke balls fired from the smoke gun
- LinkedList<Sprite> smokeShots = new LinkedList<Sprite>();
- // these sprites represent the individual honeycomb sections of the hive bunkers
- LinkedList<Sprite> hiveSections = new LinkedList<Sprite>();
- // these sprites represent the bees in the swarm
- LinkedList<Sprite> bees = new LinkedList<Sprite>();
- // these sprites represent the stinger shots fired from the bees
- LinkedList<Sprite> beeShots = new LinkedList<Sprite>();
- // the time the most recent bee shot was fired
- double lastBeeShotFired = 0;
- // the current bee movement speed
- float beeSpeed = 1.0f;
- // if true, the current game is over
- bool gameOver = false;
- // this string contains the message that is currently displayed to the user
- String displayMessage = "";
- // previous keyboard state
- KeyboardState oldKeyboardState;
- //previous Xbox controller state
- GamePadState oldGamePadState;
- public Swarm()
- {
- graphics = new GraphicsDeviceManager(this);
- Content.RootDirectory = "Content";
- }
- /// <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>
- // This method is provided fully complete as part of the activity starter.
- protected override void Initialize()
- {
- // call base.Initialize() first to get LoadContent called
- // (and therefore all textures initialized) prior to starting game!
- base.Initialize();
- startGame();
- }
- // This method is provided partially complete as part of the activity starter.
- private void startGame()
- {
- // reset game over flag
- gameOver = false;
- // create new smoke sprayer, bees, and hives
- initializeSmokeSprayer();
- initializeBees();
- initializeHives();
- // clear all prior shots
- smokeShots.Clear();
- beeShots.Clear();
- }
- // The student will complete this function as part of an activity
- private void initializeSmokeSprayer()
- {
- smokeSprayer = new Sprite();
- smokeSprayer.SetTexture(smokeSprayerTexture);
- int startingX = GraphicsDevice.Viewport.Width / 2;
- int startingY = GraphicsDevice.Viewport.Height - smokeSprayer.GetHeight() - 20;
- smokeSprayer.UpperLeft = new Vector2(startingX, startingY);
- smokeSprayer.IsAlive = true;
- }
- // This method is provided fully complete as part of the activity starter.
- private void initializeHives()
- {
- // this method will initialize all of the little hive sections as
- // individual sprites that can be destroyed by shots and bees
- hiveSections.Clear();
- // pick starting Y location
- float hiveStartingY = GraphicsDevice.Viewport.Height - 150;
- if (smokeSprayer != null)
- hiveStartingY = smokeSprayer.UpperLeft.Y - 100;
- // spacing between hives
- float hiveSpacing = 200;
- // for each hive
- for (int i = 0; i < NUM_HIVES; i++)
- {
- // for each honeycomb in the hive
- for (int j = 0; j < NUM_HONEYCOMBS; j++)
- {
- // create a new sprite
- Sprite hiveSection = new Sprite();
- // alternate the colors on odd/even blocks
- if (j % 2 == 0)
- hiveSection.SetTexture(hive1Texture);
- else
- hiveSection.SetTexture(hive2Texture);
- // first 8 squares go along the bottom
- if (j < 8)
- {
- hiveSection.UpperLeft.Y = hiveStartingY + 3 * hiveSection.GetHeight();
- hiveSection.UpperLeft.X = ((hiveSpacing * (i + 1)) + (j * hiveSection.GetWidth()));
- }
- // next 6 squares go along the middle
- else if (j < 14)
- {
- hiveSection.UpperLeft.Y = hiveStartingY + 2 * hiveSection.GetHeight();
- hiveSection.UpperLeft.X = ((hiveSpacing * (i + 1)) + ((j - 7) * hiveSection.GetWidth()));
- }
- // next 4 squares along the top
- else if (j < 18)
- {
- hiveSection.UpperLeft.Y = hiveStartingY + hiveSection.GetHeight();
- hiveSection.UpperLeft.X = ((hiveSpacing * (i + 1)) + ((j - 12) * hiveSection.GetWidth()));
- }
- // small group of squares at the peak
- else
- {
- hiveSection.UpperLeft.Y = hiveStartingY;
- hiveSection.UpperLeft.X = ((hiveSpacing * (i + 1)) + ((j - 15) * hiveSection.GetWidth()));
- }
- // set hive section to alive and add to list
- hiveSection.IsAlive = true;
- hiveSections.AddLast(hiveSection);
- }
- }
- }
- // The student will complete this function as part of an activity
- private void initializeBees()
- {
- bees.Clear();
- for (int i = 0; i < NUM_COLS; i++)
- {
- for (int j = 0; j < NUM_BEES_PER_COL; j++)
- {
- Sprite bee = new Sprite();
- bee.UpperLeft = new Vector2((i + 1) * 100.0f, j * 75.0f);
- bee.SetTexture(beeTexture);
- bees.AddLast(bee);
- bee.SetSpeedAndDirection(beeSpeed, 0);
- }
- }
- }
- // The student will complete this function as part of an activity
- private void stopGame(bool won, String message)
- {
- }
- /// <summary>
- /// LoadContent will be called once per game and is the place to load
- /// all of your content.
- /// </summary>
- // This method is provided partially complete as part of the activity starter.
- protected override void LoadContent()
- {
- // Create a new SpriteBatch, which can be used to draw textures.
- spriteBatch = new SpriteBatch(GraphicsDevice);
- // load all textures used in the game
- beeTexture = Content.Load<Texture2D>("Images\\Bee");
- beeShotTexture = Content.Load<Texture2D>("Images\\BeeShot");
- beeStripTexture = Content.Load<Texture2D>("Images\\BeeStrip");
- hive1Texture = Content.Load<Texture2D>("Images\\Hive1");
- hive2Texture = Content.Load<Texture2D>("Images\\Hive2");
- smokeTexture = Content.Load<Texture2D>("Images\\Smoke");
- smokeSprayerTexture = Content.Load<Texture2D>("Images\\SmokeSprayer");
- smokeSprayerStripTexture = Content.Load<Texture2D>("Images\\SmokeSprayerStrip");
- smokeStripTexture = Content.Load<Texture2D>("Images\\SmokeStrip");
- // load all fonts used in the game
- gameFont = Content.Load<SpriteFont>("GameFont");
- }
- /// <summary>
- /// UnloadContent will be called once per game and is the place to unload
- /// all content.
- /// </summary>
- // This method is provided fully complete as part of the activity starter.
- protected override void UnloadContent()
- {
- // TODO: Unload any non ContentManager content here
- }
- /// <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>
- // This method is provided fully complete as part of the activity starter.
- protected override void Update(GameTime gameTime)
- {
- // Allows the game to exit
- if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
- this.Exit();
- // if the game is still going on
- if (!gameOver)
- {
- // check to see if the player has beaten all the bees
- if (bees.Count == 0)
- {
- stopGame(true,"You have defeated the swarm!");
- }
- else
- {
- // see if any more bee stingers need to be fired
- checkBeeShots(gameTime);
- // move the bees
- moveBees(gameTime);
- // move any bee shots that are on the screen
- moveBeeShots();
- // move any smoke shots that are on the screen
- moveSmokeShots(gameTime);
- // handle all collisions
- checkCollisions();
- }
- }
- // if the smoke sprayer is currently animating, advance the frame
- if (smokeSprayer != null)
- if (smokeSprayer.IsAnimating())
- smokeSprayer.Animate(gameTime);
- // handle all user input
- handleKeyPress();
- // Optionally handle Xbox gamepad
- handleXboxGamepad();
- base.Update(gameTime);
- }
- // The student will complete this function as part of an activity
- private void checkBeeShots(GameTime gameTime)
- {
- }
- // The student will complete this function as part of an activity
- private void fireBeeShot()
- {
- }
- // This method is provided fully complete as part of the activity starter.
- private Sprite findEdgeBee(bool leftSide)
- {
- // create variable to trach the bee we think is on the edge
- Sprite edgeSprite = null;
- // check all remaining bees
- foreach (Sprite bee in bees)
- {
- // if this is the first bee, just save it as current edge bee
- if (edgeSprite == null)
- edgeSprite = bee;
- else
- {
- // if we are looking for the leftmost bee
- if (leftSide)
- {
- // if this bee is to left of current edge bee
- if (bee.UpperLeft.X < edgeSprite.UpperLeft.X)
- edgeSprite = bee; // we have a new edge bee
- }
- else // we are looking for the rightmost bee
- {
- // if this bee is to right of current edge bee
- if (bee.UpperLeft.X > edgeSprite.UpperLeft.X)
- edgeSprite = bee; // we have a new edge bee
- }
- }
- }
- // return the edge bee we found
- return edgeSprite;
- }
- // The student will complete this function as part of an activity
- private void moveSmokeShots(GameTime gameTime)
- {
- }
- // The student will complete this function as part of an activity
- private void moveBeeShots()
- {
- }
- // The student will complete this function as part of an activity
- private void moveBees(GameTime gameTime)
- {
- Sprite leftmostBee = findEdgeBee(true);
- Sprite rightmostBee = findEdgeBee(false);
- if ((leftmostBee == null) || (rightmostBee == null))
- {
- return;
- }
- if (((leftmostBee.UpperLeft.X) < 0) ||
- ((rightmostBee.UpperLeft.X + rightmostBee.GetWidth()) > GraphicsDevice.Viewport.Width))
- {
- foreach (Sprite bee in bees)
- {
- Vector2 velocity = bee.GetVelocity();
- velocity.X *= -1;
- bee.SetVelocity(velocity.X,velocity.Y);
- bee.UpperLeft.Y += 25;
- }
- }
- foreach (Sprite bee in bees)
- {
- bee.Move();
- }
- }
- // The student will complete this function as part of an activity
- private void checkCollisions()
- {
- // remove all dead sprites from their lists
- pruneList(beeShots);
- pruneList(smokeShots);
- pruneList(bees);
- pruneList(hiveSections);
- }
- // The student will complete this function as part of an activity
- private void handleKeyPress()
- {
- }
- //*************************************************************************************************************
- // OPTIONAL: If the student is using Xbox gamepads, they can optionally complete this method
- // If not, they should complete the handleKeyPress() method mentioned above.
- private void handleXboxGamepad()
- {
- }
- //*****************************************************************************************************************
- // The student will complete this function as part of an activity
- private void shootSmokeSprayer()
- {
- }
- /// <summary>
- /// This is called when the game should draw itself.
- /// </summary>
- /// <param name="gameTime">Provides a snapshot of timing values.</param>
- // The student will complete this function as part of an activity
- protected override void Draw(GameTime gameTime)
- {
- GraphicsDevice.Clear(Color.SkyBlue);
- spriteBatch.Begin();
- // draw the smoke sprayer
- if (smokeSprayer != null)
- smokeSprayer.Draw(spriteBatch);
- // draw all active smoke shots
- foreach (Sprite smokeShot in smokeShots)
- {
- smokeShot.Draw(spriteBatch);
- }
- // draw all active bee shots
- foreach (Sprite beeShot in beeShots)
- {
- beeShot.Draw(spriteBatch);
- }
- // draw all active bees
- foreach (Sprite bee in bees)
- {
- bee.Draw(spriteBatch);
- }
- // draw all remaining hive sections
- foreach (Sprite hiveSection in hiveSections)
- {
- hiveSection.Draw(spriteBatch);
- }
- //*****************************************************************
- // Student will add some "game over" logic here in an activity
- //*****************************************************************
- spriteBatch.End();
- base.Draw(gameTime);
- }
- // This method is provided fully complete as part of the activity starter.
- private void pruneList(LinkedList<Sprite> spriteList)
- {
- // clip out any sprites from the list where IsAlive = false
- for (int i = spriteList.Count - 1; i >= 0; i--)
- {
- Sprite s = spriteList.ElementAt(i);
- if (!s.IsAlive)
- {
- spriteList.Remove(s);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement