| public class Game1 : Microsoft.Xna.Framework.Game |
| { |
| //misc fields |
| GraphicsDeviceManager graphics; |
| SpriteBatch spriteBatch; //necessary for sprites and sprite commands |
| GamePadState previousGamePadState = GamePad.GetState(PlayerIndex.One); |
| KeyboardState previousKeyboardState = Keyboard.GetState(); |
| Texture2D backgroundTexture; |
| Rectangle viewportRect; //to go around the backgroundTexture, it's the window boundaries |
| GameObject spaceCraft, homePad; |
| float speedy = 1.0f, boost = 4.0f; // lowSpeed/highSpeed(holdKey) of the spacecraft |
| GameObject[] torpedos, enemies; //there will be multiple torpedoes and enemies |
| int maxTorp = 10; //set maximum torpedoes to 10 |
| float torpSpeed = 12.0f; //speed of torpedo, make faster than boost, above |
| float maxDistAllowed = 9000f; //kill torpedo after this distance |
| int maxEnemy = 5; //maximum amount of enemies on screen at once |
| Random random = new Random(); //make a random number |
| float timer = 0f; //to hold game's elapsed time (per update) |
| |
| int spriteWidthHoPad = 70, spriteHeightHoPad = 70; //size of each frame in the spriteSheet |
| int spriteWidthTorp = 10, spriteHeightTorp = 10; |
| int spriteWidthEnemy = 35, spriteHeightEnemy = 35; |
| Rectangle srcHoPadRect, srcTorpRect, srcEnemyRect; |
| Color transparentColor = new Color(255, 255, 255, 150); //to make a sprite semi transparent |
| |
| public Game1() |
| { |
| 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> |
| protected override void Initialize() |
| { |
| // TODO: Add your initialization logic here |
| |
| base.Initialize(); |
| } |
| |
| /// <summary> |
| /// LoadContent will be called once per game and is the place to load |
| /// all of your content. |
| /// </summary> |
| protected override void LoadContent() |
| { |
| // Create a new SpriteBatch, which can be used to draw textures. |
| spriteBatch = new SpriteBatch(GraphicsDevice); |
| spaceCraft = new GameObject(Content.Load<Texture2D>("Sprites\\spaceCraft")); //initialise spaceCraft |
| spaceCraft.position = new Vector2( |
| graphics.GraphicsDevice.Viewport.Width / 2, |
| graphics.GraphicsDevice.Viewport.Height / 2); //set initial position of spacecraft to centre of screen |
| |
| homePad = new GameObject(Content.Load<Texture2D>("Sprites\\home")); //initialise homepad |
| homePad.position = new Vector2( |
| (graphics.GraphicsDevice.Viewport.Width), |
| (graphics.GraphicsDevice.Viewport.Height)); |
| homePad.frameCount = 25; |
| homePad.currentFrame = 0; |
| homePad.intervalTime = 1000f / 25f; |
| |
| backgroundTexture = Content.Load<Texture2D>("Sprites\\background"); |
| viewportRect = new Rectangle(0, 0, |
| graphics.GraphicsDevice.Viewport.Width, |
| graphics.GraphicsDevice.Viewport.Height); |
| |
| torpedos = new GameObject[maxTorp]; //create array of GameObjects 'torpedos' of size maxTorp |
| for (int i = 0; i < maxTorp; i++) |
| { |
| torpedos[i] = new GameObject(Content.Load<Texture2D>("Sprites\\torpedoSprite")); |
| torpedos[i].frameCount = 25; |
| torpedos[i].currentFrame = 0; |
| torpedos[i].intervalTime = 1000f / 10f; |
| } |
| |
| enemies = new GameObject[maxEnemy]; //create array of GameObjects 'enemies' of size maxEnemy |
| for (int i = 0; i < maxEnemy; i++) |
| { |
| enemies[i] = new GameObject(Content.Load<Texture2D>("Sprites\\enemy")); |
| enemies[i].frameCount = 25; |
| enemies[i].currentFrame = 0; |
| enemies[i].intervalTime = 1000f / 25f; |
| } |
| } |
| |
| /// <summary> |
| /// UnloadContent will be called once per game and is the place to unload |
| /// all content. |
| /// </summary> |
| 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> |
| protected override void Update(GameTime gameTime) |
| { |
| // Allows the game to exit |
| if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) |
| this.Exit(); //press back to escape |
| |
| else if (Keyboard.GetState().IsKeyDown(Keys.Escape)) |
| this.Exit(); //press esc to escape |
| |
| |
| /****HomePad Sprite Animation****/ |
| timer += (float)gameTime.ElapsedGameTime.TotalMilliseconds; |
| if (timer > homePad.intervalTime) |
| { |
| homePad.intervalTime++; //next frame in the sprite (does nothing except provide a number) |
| if (homePad.currentFrame > homePad.frameCount - 1) |
| { |
| homePad.currentFrame = 0; //if it's reached the end, reset frame number |
| timer = 0; //reset the timer to 0 again |
| } |
| } |
| |
| srcHoPadRect = new Rectangle( |
| homePad.currentFrame * spriteWidthHoPad, //see fields |
| 0, //always at the top (sprite is only one line of images) |
| spriteWidthHoPad, |
| spriteHeightHoPad); |
| /*End HomePad Sprite Animation****/ |
| |
| /****Torpedo Sprite Animation****/ |
| foreach (GameObject torpedo in torpedos) |
| { |
| timer += (float)gameTime.ElapsedGameTime.TotalMilliseconds; |
| if (timer > torpedo.intervalTime) |
| { |
| torpedo.currentFrame++; //next frame in the sprite (does nothing except provide a number) |
| if (torpedo.currentFrame > torpedo.frameCount - 1) |
| { |
| torpedo.currentFrame = 0; //if it's reached the end, reset frame number |
| timer = 0; //reset the timer to 0 again |
| } |
| } |
| |
| |
| srcTorpRect = new Rectangle( |
| torpedo.currentFrame * spriteWidthTorp, //see fields |
| 0, //always at the top (sprite is only one line of images) |
| spriteWidthTorp, |
| spriteHeightTorp); |
| } |
| /*End Torpedo Sprite Animation****/ |
| |
| /****Enemy Sprite Animation****/ |
| foreach (GameObject enemy in enemies) |
| { |
| timer += (float)gameTime.ElapsedGameTime.TotalMilliseconds; |
| if (timer > enemy.intervalTime) |
| { |
| enemy.currentFrame++; //next frame in the sprite (does nothing except provide a number) |
| if (enemy.currentFrame > enemy.frameCount - 1) |
| { |
| enemy.currentFrame = 0; //if it's reached the end, reset frame number |
| timer = 0; //reset the timer to 0 again |
| } |
| } |
| |
| srcEnemyRect = new Rectangle( |
| enemy.currentFrame * spriteWidthEnemy, //see fields |
| 0, //always at the top (sprite is only one line of images) |
| spriteWidthEnemy, |
| spriteHeightEnemy); |
| |
| } |
| /*End Enemy Sprite Animation****/ |
| |
| /*** Moving the Spacecraft ***/ |
| GamePadState gamePadState = GamePad.GetState(PlayerIndex.One); |
| spaceCraft.rotation += gamePadState.ThumbSticks.Left.X * 0.1f; |
|
|
|
| #if !XBOX |
| KeyboardState keyboardState = Keyboard.GetState(); //check the keyboard out, what's happening |
| |
| if (keyboardState.IsKeyDown(Keys.Left) ^ keyboardState.IsKeyDown(Keys.A)) |
| {spaceCraft.rotation -= 0.1f;} //if left is pressed, rotate left |
| |
| if (keyboardState.IsKeyDown(Keys.Right) ^ keyboardState.IsKeyDown(Keys.D)) |
| {spaceCraft.rotation += 0.1f;} //if right is pressed, rotate right |
| |
| if (keyboardState.IsKeyDown(Keys.Up) ^ keyboardState.IsKeyDown(Keys.W)) |
| { |
| spaceCraft.velocity = new Vector2( |
| (float)Math.Cos(spaceCraft.rotation - MathHelper.PiOver2), |
| (float)Math.Sin(spaceCraft.rotation - MathHelper.PiOver2)); |
| spaceCraft.position += spaceCraft.velocity * boost; |
| } //move forward (mathhelper because it originally moved to the right) |
| |
| if (keyboardState.IsKeyDown(Keys.Down) ^ keyboardState.IsKeyDown(Keys.S)) |
| { |
| spaceCraft.velocity = new Vector2( |
| (float)Math.Cos(spaceCraft.rotation + MathHelper.PiOver2), |
| (float)Math.Sin(spaceCraft.rotation + MathHelper.PiOver2)); |
| spaceCraft.position += spaceCraft.velocity * boost; |
| } //move forward (mathhelper because it originally moved to the right) |
| |
| if (keyboardState.IsKeyDown(Keys.E)) |
| { |
| spaceCraft.velocity = new Vector2( |
| (float)-Math.Cos(spaceCraft.rotation + MathHelper.Pi), |
| (float)Math.Sin(spaceCraft.rotation + MathHelper.Pi)); |
| spaceCraft.position += spaceCraft.velocity * boost; |
| } //strafe right |
| |
| if (keyboardState.IsKeyDown(Keys.Q)) |
| { |
| spaceCraft.velocity = new Vector2( |
| (float)Math.Cos(spaceCraft.rotation + MathHelper.Pi), |
| (float)Math.Sin(spaceCraft.rotation + MathHelper.Pi)); |
| spaceCraft.position += spaceCraft.velocity * boost; |
| } //strafe left |
| |
| spaceCraft.position += spaceCraft.velocity * speedy; //move from current position in vector set |
| |
| if (keyboardState.IsKeyDown(Keys.LeftControl)) |
| {spaceCraft.velocity = Vector2.Zero;} //press leftCtrl to stop moving (set velocity to 0) |
| |
| if (keyboardState.IsKeyDown(Keys.LeftShift)) |
| { |
| spaceCraft.position = new Vector2( |
| graphics.GraphicsDevice.Viewport.Width / 2, |
| graphics.GraphicsDevice.Viewport.Height / 2); |
| spaceCraft.velocity = Vector2.Zero; |
| } //press leftShift to reset position to middle of screen, and set speed to 0 (stationary) |
| |
| /******** CONTROLLING SPEED IS A BIT CUMBERSOME |
| if (keyboardState.IsKeyDown(Keys.PageUp)) |
| {speedy++;} //decrease speed of spaceCraft |
| if (keyboardState.IsKeyDown(Keys.PageDown)) |
| { speedy--; } //decrease speed of spaceCraft |
| **********************************************/ |
|
| #endif |
| if (spaceCraft.position.Y < 0) //these four IFs sort out the spacecraft if it goes off screen |
| { spaceCraft.position.Y = graphics.GraphicsDevice.Viewport.Height; } |
| |
| if (spaceCraft.position.Y > graphics.GraphicsDevice.Viewport.Height) |
| { spaceCraft.position.Y = 0; } |
| |
| if (spaceCraft.position.X < 0) |
| { spaceCraft.position.X = graphics.GraphicsDevice.Viewport.Width; } |
| |
| if (spaceCraft.position.X > graphics.GraphicsDevice.Viewport.Width) |
| { spaceCraft.position.X = 0; } |
| |
| /*** End : Moving the Spacecraft ***/ |
| |
| /*** Firing from the Spacecraft ***/ |
| |
| if (gamePadState.Buttons.A == ButtonState.Pressed && |
| previousGamePadState.Buttons.A == ButtonState.Released) |
| { |
| FireTorpedo(); |
| } |
| |
| previousGamePadState = gamePadState; |
|
| #if !XBOX |
| if (keyboardState.IsKeyDown(Keys.Space) && |
| previousKeyboardState.IsKeyUp(Keys.Space)) |
| { |
| FireTorpedo(); |
| } |
| |
| previousKeyboardState = keyboardState; |
| #endif |
| UpdateTorpedo(); |
| UpdateEnemies(); |
| |
| /*** End: Firing from the Spacecraft ***/ |
| |
| |
| base.Update(gameTime); |
| } |
| |
| |
| /* This method sets the velocity of torpedos */ |
| public void FireTorpedo() |
| { |
| foreach (GameObject torpedo in torpedos) |
| { |
| if (!torpedo.alive) |
| { |
| torpedo.alive = true; |
| torpedo.currentPosition = Vector2.Zero; |
| torpedo.distTrav = Vector2.Zero; |
| torpedo.position = spaceCraft.position; //so that centers match up |
| torpedo.startPosition = torpedo.position; //store the original position |
| torpedo.velocity = new Vector2( |
| (float)Math.Cos(spaceCraft.rotation - MathHelper.PiOver2), |
| (float)Math.Sin(spaceCraft.rotation - MathHelper.PiOver2)) * torpSpeed; |
| return; |
| } |
| } |
| } |
| /* End setting velocity of torpedos */ |
| |
| /*Update Enemies* |
| * moves the enemies, resetting alive if dead*/ |
| |
| public void UpdateEnemies() |
| { |
| foreach (GameObject enemy in enemies) |
| { |
| if (enemy.alive) |
| { |
| enemy.insideDestRect = new Rectangle( |
| (int)(enemy.position.X += enemy.velEnX), |
| (int)(enemy.position.Y += enemy.velEnY), |
| enemy.sprite.Width/(enemy.frameCount*2), |
| enemy.sprite.Height/2); |
| |
| if (enemy.position.Y < 0) //these four IFs sort out the spacecraft if it goes off screen |
| { enemy.position.Y = graphics.GraphicsDevice.Viewport.Height; } |
| |
| if (enemy.position.Y > graphics.GraphicsDevice.Viewport.Height) |
| { enemy.position.Y = 0; } |
| |
| if (enemy.position.X < 0) |
| { enemy.position.X = graphics.GraphicsDevice.Viewport.Width; } |
| |
| if (enemy.position.X > graphics.GraphicsDevice.Viewport.Width) |
| { enemy.position.X = 0; } |
| } |
| |
| else //if its dead |
| { |
| if (!enemy.graveYard) |
| { |
| enemy.alive = true; //reactivate the enemy |
| enemy.insideDestRect = new Rectangle( |
| random.Next(0, 800), |
| random.Next(0, 600), |
| spriteWidthEnemy, |
| spriteHeightEnemy); |
| enemy.velEnX = random.Next(-15, 15); //velocity here. |
| enemy.velEnY = random.Next(-15, 15); |
| } |
| } |
| } |
| } |
| |
| |
| |
| |
| /* This method implements the velocity - makes it move. |
| * It also checks to see if the torpedo hit an enemy, |
| * kills the enemy + torpedo and ups the score */ |
| |
| public void UpdateTorpedo() |
| { |
| foreach (GameObject torpedo in torpedos) //does "if" for all the objects in cannonballs |
| { |
| if (torpedo.alive) |
| { |
| torpedo.position += torpedo.velocity; //move it |
| if (torpedo.position.Y < 0) //if ball goes off the screen, redraw on other side |
| { |
| torpedo.position.Y = graphics.GraphicsDevice.Viewport.Height; |
| } |
| |
| if (torpedo.position.Y > graphics.GraphicsDevice.Viewport.Height) //if ball goes off the screen, redraw on other side |
| { |
| torpedo.position.Y = 0; |
| } |
| |
| if (torpedo.position.X > graphics.GraphicsDevice.Viewport.Width) //if ball goes off the screen, redraw on other side |
| { |
| torpedo.position.X = 0; |
| } |
| |
| if (torpedo.position.X < 0) //if ball goes off the screen, redraw on other side |
| { |
| torpedo.position.X = graphics.GraphicsDevice.Viewport.Width; |
| } |
| |
| /*kill torpedo after distance*/ |
| torpedo.currentPosition = new Vector2( |
| torpedo.position.X, |
| torpedo.position.Y); |
| |
| torpedo.distTrav += torpedo.currentPosition; |
| |
| float distance = Vector2.Distance(torpedo.startPosition, torpedo.distTrav); |
| |
| if (distance > maxDistAllowed) |
| { |
| System.Console.WriteLine(distance); |
| torpedo.alive = false; |
| } |
| /*end kill torpedo after distance*/ |
| |
| Rectangle torpedoRect = new Rectangle( |
| (int)torpedo.position.X, |
| (int)torpedo.position.Y, |
| torpedo.sprite.Width, |
| torpedo.sprite.Height); |
| |
| foreach (GameObject enemy in enemies) |
| { |
| |
| if (enemy.insideDestRect.Intersects(torpedoRect)) |
| { |
| torpedo.alive = false; |
| enemy.alive = false; //kill the torpedo and ship |
| enemy.graveYard = true; //if true, will NOT respawn |
| System.Console.WriteLine("Enemy@" + enemy.insideDestRect.X + ", " + enemy.insideDestRect.Y); |
| System.Console.WriteLine("Torp@" + torpedo.currentPosition); |
| //score++; //add one point for every collision |
| break; |
| } |
| } |
| } |
| } |
| } |
| /*END move the torpedos checking if it hit */ |
| |
| /// <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) |
| { |
| graphics.GraphicsDevice.Clear(Color.CornflowerBlue); |
| spriteBatch.Begin(SpriteBlendMode.AlphaBlend); |
| spriteBatch.Draw(backgroundTexture, viewportRect, Color.White); |
| /* Torpedos ******/ |
| foreach (GameObject torpedo in torpedos) //for all that exist |
| { |
| if (torpedo.alive) //no point if it's dead |
| { |
| Rectangle destTorpRect = new Rectangle( //create a rectangle for each torpedo alive |
| (int)torpedo.position.X, |
| (int)torpedo.position.Y, |
| spriteWidthTorp, |
| spriteHeightTorp); |
| |
| spriteBatch.Draw( |
| torpedo.sprite, |
| destTorpRect, |
| srcTorpRect, |
| Color.White); |
| } |
| } |
| /*End Torpedos*/ |
| |
| |
| spriteBatch.Draw( //draw the spaceCraft on the screen |
| spaceCraft.sprite, |
| spaceCraft.position, |
| null, |
| Color.White, |
| spaceCraft.rotation, |
| spaceCraft.center, |
| 1.0f, |
| SpriteEffects.None, 0); |
| |
| /**** Home Pad ****/ |
| Rectangle destHoPadRect = new Rectangle( |
| (int) (graphics.GraphicsDevice.Viewport.Width / 2)- (spriteWidthHoPad/7 *5)/2, //draw it in the centre of the screen |
| (int)(graphics.GraphicsDevice.Viewport.Height / 2)- (spriteWidthHoPad/7 *5)/2, |
| spriteWidthHoPad/7 *5, //draw it slightly smaller |
| spriteHeightHoPad/7 *5); |
| |
| spriteBatch.Draw( |
| homePad.sprite, |
| destHoPadRect, |
| srcHoPadRect, |
| transparentColor); |
| |
| /****end Home Pad****/ |
| |
| /*** Enemy SHips***/ |
| foreach (GameObject enemy in enemies) |
| { |
| spriteBatch.Draw( |
| enemy.sprite, |
| enemy.insideDestRect, |
| srcEnemyRect, |
| transparentColor); |
| } |
| /*** End Enemy Ships ***/ |
| |
| |
| spriteBatch.End(); |
| base.Draw(gameTime); |
| } |
| } |
| } |