Click here to Skip to main content
15,895,709 members
Articles / Artificial Intelligence

Invasion Game in XNA for Windows Phone 7

Rate me:
Please Sign up or sign in to vote.
4.92/5 (31 votes)
6 Jan 2012CPOL12 min read 111K   6.6K   59  
This is a port of Invasion (originally posted on CodeProject by Mauricio Ritter) to Windows Phone 7 (with some enhancements).
using System;
using System.Diagnostics;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

using Invasion.SpriteManagers;


namespace Invasion.Sprites
{
	/// <summary>
	/// Extra Class
	/// </summary>
    public class Extra : DrawableGameComponent 
	{
        public enum ExtraType { NotSet = -1, PhotonAmmo, Weapons, Hundred, BlasterAmmo, Shield }

        private ExtraType extraType = ExtraType.PhotonAmmo;

        public Vector2 Position {
            get { return position; }
            set {
                position = value;
                destRect.X = (int)position.X;
                destRect.Y = (int)position.Y;
            }
        }
        private Vector2 position;

        public const int NumExtraTypes = 5;

		private int	frame = 0;

        private Texture2D extrasTex = null;
        
        Rectangle srcRect = Rectangle.Empty;
        Rectangle destRect = Rectangle.Empty;

        public int SpeedY {
            get;
            set;
        }


        public Extra(Game game, Vector2 position, ExtraType extraType) : base(game) {
            this.position = position;
            this.extraType = extraType;

            SpeedY = 0;
            SetupTexture();
        }


        public Extra(Game game, Vector2 position, ExtraType extraType, int speedY) : base(game) {
            this.position = position;
            this.extraType = extraType;

            SpeedY = speedY;
            SetupTexture();
        }


        private void SetupTexture() {
            extrasTex = ExtrasManager.ExtrasTex;
            srcRect  = new Rectangle(0, 0, 25, 25);
            destRect = new Rectangle((int)position.X, (int)position.Y, 25, 25);
        }


        public ExtraType Type {
            get { return extraType; }
            set { extraType = value; }
		}


		public void Move() {
            position.Y -= SpeedY;
            destRect.Y = (int)position.Y;
		}


        public override void Update(GameTime gameTime) {
            Update();
            Move();
        }


		public void Update() 
        {
            srcRect.X = frame*25;
            srcRect.Y = (int)extraType*25;

			frame++;

			if (frame == 19)
				frame = 0;
		}


        public override void Draw(GameTime gameTime) {
            InvasionGame.spriteBatch.Draw(extrasTex, position, srcRect, Color.White);
        }


        public Rectangle BoundingBox {
            get { return destRect; }
        }
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
President ImproviSoft LLC
United States United States
Dan is the Founder and President of ImproviSoft LLC (mobile software) and AdStreamer, Inc. (mobile advertising) - both Microsoft BizSpark Plus Startups.

Dan holds a B.S. in Computer Science from Clarkson University and M.S. degrees in Computer Science and Computer Engineering from Syracuse University. He is an ASQ Certified Software Quality Engineer (CSQE) and was a 2012 Microsoft XNA/DirectX MVP.

Prior experience includes Software Engineering, Project Management, and Functional Management in the Aerospace & Defense, Medical Devices, Automotive Engineering, and e-Commerce industries.

Dan's dev-blog is The ImproviSoft Blog.

Comments and Discussions