Click here to Skip to main content
15,891,136 members
Articles / Mobile Apps

Pocket 1945 - A C# .NET CF Shooter

Rate me:
Please Sign up or sign in to vote.
4.90/5 (101 votes)
2 Jun 2004CPOL10 min read 269.1K   2.7K   152  
An article on Pocket PC game development.
using System;
using System.Drawing;
using System.Drawing.Imaging;

namespace Pocket1945
{
	/// <summary>
	/// This class represents a bullet in the game. Both player
	/// and enemy planes use this class when a new bullet is fired.
	/// </summary>
	public class Bullet : Sprite
	{				
		private int spriteIndex;		
		private BulletType bulletType;				
		private double speed; 

		/// <summary>
		/// The power of the buller. When the bullet hit a enemey or the 
		/// player the shield is decreased by this property.
		/// </summary>
		public int BulletPower;

		/// <summary>
		/// The direction the bullet is moving. If it's moving up it's aimed at 
		/// one of the enemies. If it's moving down it's aimed at the player.
		/// </summary>
		public MoveDirection Direction;

		/// <summary>
		/// Instanciates a new bullet.
		/// </summary>
		/// <param name="outX">The x position the bullet was fired from.</param>
		/// <param name="outY">The y position the bullet was fired from.</param>
		/// <param name="bulletPower">The power of the bullet.</param>
		/// <param name="bulletSpeed">The speed of the bullet.</param>
		/// <param name="bulletType">The type of the bullet.</param>
		/// <param name="direction">The direction of the bullet.</param>
		public Bullet(int outX, int outY, int bulletPower, double bulletSpeed, BulletType bulletType, MoveDirection direction)
		{
			this.speed = bulletSpeed;
			this.x = outX;			
			this.y = outY;
			this.BulletPower = bulletPower;
			this.bulletType = bulletType;
			this.spriteSize.Width = 32;
			this.spriteSize.Height = 32;
			this.Direction = direction;
			this.collitionRectangle = new Rectangle(x, y, spriteSize.Width, spriteSize.Height);
			SetBulletSpesific();
		}

		/// <summary>
		/// Method seting bullet spesific properties like sprite index and collition rectangle.
		/// </summary>
		private void SetBulletSpesific()
		{
			switch(bulletType)
			{
				case BulletType.SingleShot :
					collitionRectangle.Width = 7;
					collitionRectangle.Height = 16;
					if(Direction == MoveDirection.Down)
					{
						spriteIndex = 0;
						collitionPoint.Y = 7;
						collitionPoint.X = 13;
					}
					else
					{
						spriteIndex = 1;
						collitionPoint.Y = 9;
						collitionPoint.X = 12;												
					}
					break;
				case BulletType.BigSingleShot : 
					collitionRectangle.Width = 9;
					collitionRectangle.Height = 20;
					if(Direction == MoveDirection.Down)
					{
						spriteIndex = 2;
						collitionPoint.Y = 5;
						collitionPoint.X = 12;
					}
					else
					{
						spriteIndex = 3;
						collitionPoint.Y = 7;
						collitionPoint.X = 11;						
					}
					break;
				case BulletType.DoubleShot : 
					collitionRectangle.Width = 17;
					collitionRectangle.Height = 16;
					collitionPoint.Y = 8;
					collitionPoint.X = 7;
					if(Direction == MoveDirection.Down)
						spriteIndex = 4;
					else
						spriteIndex = 5;					
					break;
				case BulletType.SmallFireball : 
					collitionRectangle.Width = 9;
					collitionRectangle.Height = 9;
					collitionPoint.Y = 12;
					collitionPoint.X = 13;
					spriteIndex = 9;
					break;
				case BulletType.BigFireball : 
					collitionRectangle.Width = 13;
					collitionRectangle.Height = 13;
					collitionPoint.Y = 10;
					collitionPoint.X = 10;
					spriteIndex = 8;
					break;
				default :
					collitionRectangle.Width = 7;
					collitionRectangle.Height = 16;
					collitionPoint.Y = 7;
					collitionPoint.X = 13;
					spriteIndex = 8;
					break;
			}
		}

		/// <summary>
		/// Method checking if the bullet has focus or not.
		/// </summary>
		/// <returns>True if the bullet is on the screen and has focus.</returns>
		public bool HasFoucs()
		{
			if(Direction == MoveDirection.Down)
				return(y < GameForm.GameArea.Height);
			else
				return(y > -spriteSize.Height);
		}

		/// <summary>
		/// Method moving the bullet.
		/// </summary>
		public void Move()
		{
			if(Direction == MoveDirection.Down)
				y = y + (int)speed;
			else
				y = y - (int)speed;
		}

		/// <summary>
		/// Method returning the sprite index for this bullet.
		/// </summary>
		/// <returns>The index of the sprite in the SpriteList class.</returns>
		public override int GetSpriteIndex()
		{
			return spriteIndex;
		}

		public override void Draw(Graphics g)
		{
			int index = GetSpriteIndex();
			g.DrawImage(SpriteList.Instance.Bullets[index], new Rectangle(x, y, spriteSize.Width, spriteSize.Height), 0, 0, spriteSize.Width, spriteSize.Height, GraphicsUnit.Pixel, imgAttribs);
		}
	}
}

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
Web Developer
Norway Norway
I'm a C# developer from Norway. I'm a co-founder of a small ISV called GreIT AS. Our company site can be found at http://www.greit.no (all information in norwegian).

My everyday work consists of building ASP.NET web applications and work on our content management system, Webpakken. I use my spare time on side projects such as Pocket 1945 (http://workspaces.gotdotnet.com/pocket1945), a shooter game for the Pocket PC platform. It’s almost the opposite of my everyday work since games are small, focus on graphics and entertainment, while our CMS is large, focus on businesses and data.

When I’m not sitting in front of the computer I go snowboarding or skateboarding, depending on which time of the year it is.

I also enjoy fly fishing for salmon and trout in the summer. My personal record is a 10.5 KG salmon caught in Lakselva last summer and a 2 KG trout caught some where secret place in Finnmark. A bragging picture of me holding the salmon can be found at http://jonas.greit.no/pictures/salmon.jpg .

I did start blogging some time back, but I haven’t been to good at updating my blogg. I just got so much stuff going on that it’s hard to find time to add blogg posts. You can view my blogg at http://jonas.greit.no, just don’t expect too much.

Comments and Discussions