Click here to Skip to main content
15,898,036 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.3K   2.7K   152  
An article on Pocket PC game development.
using System;
using System.Drawing;
using System.Drawing.Imaging;

namespace Pocket1945
{
	/// <summary>
	/// This class defines a game bonus element. A bonus can be 
	/// a extra life, a power up, more speed etc. The type of bonus is defined
	/// by the BonusType enum. The player object have a method, GetBonus(Bonus b), 
	/// that process a bonus element.
	/// </summary>
	public class Bonus : Sprite
	{		
		private int spriteIndex;
		private int startPosition;	
		private double tempY;
		private double speed;
		
		public bool Collected;
		public BonusType Type;

		/// <summary>
		/// Instanciates a new bonus object.
		/// </summary>
		/// <param name="x">The x position of the bonus element.</param>
		/// <param name="startPosition">The y start position of the bonus element.</param>
		/// <param name="speed">The speed of the bonus element.</param>
		/// <param name="bonusType">The type of bonus.</param>
		public Bonus(int x, int startPosition, double speed, BonusType bonusType)
		{
			this.x = x;
			this.y = -32;
			this.tempY = this.y;
			this.speed = speed;
			this.startPosition = startPosition;
			this.Type = bonusType;
			this.spriteSize.Height = 32;
			this.spriteSize.Width = 32;		
			SetBonusSpesific();
		}

		/// <summary>
		/// Method setting bonus spesific properties like sprite index and sprite size.
		/// </summary>
		private void SetBonusSpesific()
		{
			switch(Type)
			{
				case BonusType.ExtraLife : 
					spriteIndex = 1;
					collitionRectangle.Width = 23;
					collitionRectangle.Height = 18;
					collitionPoint.X = 4;
					collitionPoint.Y = 6;
					break;
				case BonusType.SmallPowerUpgrade : 
					spriteIndex = 4;
					collitionRectangle.Width = 24;
					collitionRectangle.Height = 25;
					collitionPoint.X = 4;
					collitionPoint.Y = 5;
					break;
				case BonusType.PowerUpgrade :
					spriteIndex = 5;
					collitionRectangle.Width = 24;
					collitionRectangle.Height = 25;
					collitionPoint.X = 4;
					collitionPoint.Y = 5;
					break;
				case BonusType.RankUpgrade : 
					spriteIndex = 2;
					collitionRectangle.Width = 29;
					collitionRectangle.Height = 15;
					collitionPoint.X = 1;
					collitionPoint.Y = 7;					
					break;
				case BonusType.SpeedUpgrade :
					spriteIndex = 3;
					collitionRectangle.Width = 20;
					collitionRectangle.Height = 28;
					collitionPoint.X = 7;
					collitionPoint.Y = 2;
					break;
				case BonusType.ShieldUpgrade : 
					spriteIndex = 6;
					collitionRectangle.Width = 20;
					collitionRectangle.Height = 29;
					collitionPoint.X = 7;
					collitionPoint.Y = 1;
					break;
				case BonusType.WheaponUpgrade : 
				default : 
					spriteIndex = 0;
					collitionRectangle.Width = 20;
					collitionRectangle.Height = 27;
					collitionPoint.X = 7;
					collitionPoint.Y = 3;
					break;
			}			
		}

		/// <summary>
		/// Method returning the sprite index of the bonus.
		/// </summary>
		/// <returns>Returns the sprite index of the bonus sprite.</returns>
		public override int GetSpriteIndex()
		{
			return spriteIndex;
		}

		/// <summary>
		/// Method determing weather the bonus element has focus or not.
		/// </summary>
		/// <returns>True if the bonus is on screen and has focus.</returns>
		public bool HasFocus()
		{
			return(!Collected && startPosition >= GameForm.CurrentLevel.BackgroundMap.Y && GameForm.GameArea.Height > y);			
		}

		/// <summary>
		/// Method moving the bonus element.
		/// </summary>
		public void Move()
		{			
			tempY += speed;
			y = (int)tempY;
		}

		public override void Draw(Graphics g)
		{
			int index = GetSpriteIndex();			
			g.DrawImage(SpriteList.Instance.Bonuses[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