Click here to Skip to main content
15,896,500 members
Articles / Mobile Apps

Color Invasion for the PocketPC or .NET

Rate me:
Please Sign up or sign in to vote.
3.83/5 (16 votes)
28 May 2004CPOL6 min read 53.1K   304   25  
This is my submission for the .NET CF contest for May 2004. It is a game utilizing the .NET Compact Framework for the PocketPC that demonstrates the ease and power or .NET CF. The game allows singleplayer mode against the computer, or Multiplayer over a network or Internet connection.
// project created on 5/21/2004 at 8:38 AM
using System;
using System.Windows.Forms;
using System.Drawing;
using System.Collections;
using ColorInvasion;

namespace MyFormProject 
{
	class MainForm : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Button Easy;
		private System.Windows.Forms.Button Yellow;
		private System.Windows.Forms.Button Fushia;
		private System.Windows.Forms.Button Medium;
		private System.Windows.Forms.Button Hard;
		private System.Windows.Forms.Button Blue;
		private System.Windows.Forms.Button Green;
		private System.Windows.Forms.Button Black;
		private System.Windows.Forms.Button Red;
		public MainForm()
		{
			InitializeComponent();
		}
	
		// THIS METHOD IS MAINTAINED BY THE FORM DESIGNER
		// DO NOT EDIT IT MANUALLY! YOUR CHANGES ARE LIKELY TO BE LOST
		void InitializeComponent() {
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(MainForm));
			this.Red = new System.Windows.Forms.Button();
			this.Black = new System.Windows.Forms.Button();
			this.Green = new System.Windows.Forms.Button();
			this.Blue = new System.Windows.Forms.Button();
			this.Hard = new System.Windows.Forms.Button();
			this.Medium = new System.Windows.Forms.Button();
			this.Fushia = new System.Windows.Forms.Button();
			this.Yellow = new System.Windows.Forms.Button();
			this.Easy = new System.Windows.Forms.Button();
			this.SuspendLayout();
			// 
			// Red
			// 
			this.Red.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("Red.BackgroundImage")));
			this.Red.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
			this.Red.Location = new System.Drawing.Point(16, 208);
			this.Red.Name = "Red";
			this.Red.Size = new System.Drawing.Size(32, 23);
			this.Red.TabIndex = 0;
			this.Red.Click += new System.EventHandler(this.RedClick);
			// 
			// Black
			// 
			this.Black.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("Black.BackgroundImage")));
			this.Black.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
			this.Black.Location = new System.Drawing.Point(48, 208);
			this.Black.Name = "Black";
			this.Black.Size = new System.Drawing.Size(32, 23);
			this.Black.TabIndex = 2;
			this.Black.Click += new System.EventHandler(this.BlackClick);
			// 
			// Green
			// 
			this.Green.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("Green.BackgroundImage")));
			this.Green.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
			this.Green.Location = new System.Drawing.Point(176, 208);
			this.Green.Name = "Green";
			this.Green.Size = new System.Drawing.Size(32, 23);
			this.Green.TabIndex = 5;
			this.Green.Click += new System.EventHandler(this.GreenClick);
			// 
			// Blue
			// 
			this.Blue.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("Blue.BackgroundImage")));
			this.Blue.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
			this.Blue.Location = new System.Drawing.Point(80, 208);
			this.Blue.Name = "Blue";
			this.Blue.Size = new System.Drawing.Size(32, 23);
			this.Blue.TabIndex = 1;
			this.Blue.Click += new System.EventHandler(this.BlueClick);
			// 
			// Hard
			// 
			this.Hard.Location = new System.Drawing.Point(144, 0);
			this.Hard.Name = "Hard";
			this.Hard.Size = new System.Drawing.Size(64, 23);
			this.Hard.TabIndex = 7;
			this.Hard.Text = "Slow";
			this.Hard.Click += new System.EventHandler(this.HardClick);
			// 
			// Medium
			// 
			this.Medium.Location = new System.Drawing.Point(80, 0);
			this.Medium.Name = "Medium";
			this.Medium.Size = new System.Drawing.Size(64, 23);
			this.Medium.TabIndex = 8;
			this.Medium.Text = "Medium";
			this.Medium.Click += new System.EventHandler(this.MediumClick);
			// 
			// Fushia
			// 
			this.Fushia.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("Fushia.BackgroundImage")));
			this.Fushia.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
			this.Fushia.Location = new System.Drawing.Point(144, 208);
			this.Fushia.Name = "Fushia";
			this.Fushia.Size = new System.Drawing.Size(32, 23);
			this.Fushia.TabIndex = 3;
			this.Fushia.Click += new System.EventHandler(this.FushiaClick);
			// 
			// Yellow
			// 
			this.Yellow.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("Yellow.BackgroundImage")));
			this.Yellow.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
			this.Yellow.Location = new System.Drawing.Point(112, 208);
			this.Yellow.Name = "Yellow";
			this.Yellow.Size = new System.Drawing.Size(32, 23);
			this.Yellow.TabIndex = 4;
			this.Yellow.Click += new System.EventHandler(this.YellowClick);
			// 
			// Easy
			// 
			this.Easy.Location = new System.Drawing.Point(16, 0);
			this.Easy.Name = "Easy";
			this.Easy.Size = new System.Drawing.Size(64, 23);
			this.Easy.TabIndex = 6;
			this.Easy.Text = "Fast";
			this.Easy.Click += new System.EventHandler(this.EasyClick);
			// 
			// MainForm
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(224, 230);
			this.Controls.Add(this.Medium);
			this.Controls.Add(this.Hard);
			this.Controls.Add(this.Easy);
			this.Controls.Add(this.Green);
			this.Controls.Add(this.Yellow);
			this.Controls.Add(this.Fushia);
			this.Controls.Add(this.Black);
			this.Controls.Add(this.Blue);
			this.Controls.Add(this.Red);
			this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
			this.Name = "MainForm";
			this.Text = "Color Invasion";
			this.ResumeLayout(false);
			
			DisableButton();
		}

		Game m_GameObject = new Game();
		protected ArrayList index_array = new ArrayList();
		
		public void DisableButton(){
			/// Disables button based on player2's color
			/// 
			index_array.AddRange(m_GameObject.g_Player2_Tiles.Keys);
			if(m_GameObject.g_Player2_Tiles[index_array[0]] is Tile)
			{
				Black.Visible = true;
				Red.Visible = true;
				Blue.Visible = true;
				Yellow.Visible = true;
				Fushia.Visible = true;
				Green.Visible = true;
				switch(((Tile)m_GameObject.g_Player2_Tiles[index_array[0]]).color)
				{
					case 0:
						{
							Black.Visible = false;
							break;
						}
					case 1:
						{
							Red.Visible = false;
							break;
						}
					case 2:
						{
							Blue.Visible = false;
							break;
						}
					case 3:
						{
							Yellow.Visible = false;
							break;
						}
					case 4:
						{
							Fushia.Visible = false;
							break;
						}
					case 5:
						{
							Green.Visible = false;
							break;
						}
				}
			}
			
			index_array.RemoveRange(0, index_array.Count);
			
		}
		
		protected override void OnPaint(PaintEventArgs pea) {
			base.OnPaint(pea);
			
			Rectangle destRect1 = new Rectangle( 50, 50, 20, 20);
			
			index_array.AddRange(m_GameObject.g_Tiles.Keys);
			foreach(int n in index_array)
			{
				if(m_GameObject.g_Positions.ContainsKey(n))
				{
					destRect1.X = ((ColorInvasion.Position)m_GameObject.g_Positions[n]).x;
					destRect1.Y = ((ColorInvasion.Position)m_GameObject.g_Positions[n]).y;
					
					if(m_GameObject.g_Tiles[n] is Tile)
					{
					pea.Graphics.DrawImage( m_GameObject.g_TileSet, destRect1,m_GameObject.g_TileColors[((Tile)m_GameObject.g_Tiles[n]).color], 0, m_GameObject.width, m_GameObject.height,GraphicsUnit.Pixel);
					}
					else
					{
						m_GameObject.g_Tiles.Remove(n);
					}
				}
			}
			index_array.RemoveRange(0, index_array.Count);
			
			index_array.AddRange(m_GameObject.g_Player1_Tiles.Keys);
			foreach(int n in index_array)
			{
				if(m_GameObject.g_Positions.ContainsKey(n))
				{
					destRect1.X = ((ColorInvasion.Position)m_GameObject.g_Positions[n]).x;
					destRect1.Y = ((ColorInvasion.Position)m_GameObject.g_Positions[n]).y;
					
					pea.Graphics.DrawImage( m_GameObject.g_TileSet, destRect1,m_GameObject.g_TileColors[((Tile)m_GameObject.g_Player1_Tiles[n]).color], 20, m_GameObject.width, m_GameObject.height,GraphicsUnit.Pixel);
				}
			}
			index_array.RemoveRange(0, index_array.Count);
			
			index_array.AddRange(m_GameObject.g_Player2_Tiles.Keys);
			foreach(int n in index_array)
			{
				if(m_GameObject.g_Positions.ContainsKey(n))
				{
					destRect1.X = ((ColorInvasion.Position)m_GameObject.g_Positions[n]).x;
					destRect1.Y = ((ColorInvasion.Position)m_GameObject.g_Positions[n]).y;
					
					pea.Graphics.DrawImage( m_GameObject.g_TileSet, destRect1,m_GameObject.g_TileColors[((Tile)m_GameObject.g_Player2_Tiles[index_array[0]]).color], 20, m_GameObject.width, m_GameObject.height,GraphicsUnit.Pixel);
				}
			}
			index_array.RemoveRange(0, index_array.Count);

		}
			
		[STAThread]
		public static void Main(string[] args)
		{
			Application.Run(new MainForm());
		}
		void RedClick(object sender, System.EventArgs e)
		{
			index_array.AddRange(m_GameObject.g_Player1_Tiles.Keys);
			foreach(int n in index_array)
			{
				if(m_GameObject.g_Player1_Tiles.ContainsKey(n))
				{
					((Tile)m_GameObject.g_Player1_Tiles[n]).color = 1;
				}
			}
			index_array.RemoveRange(0, index_array.Count);
			
			m_GameObject.ScanTiles(true);
			
			m_GameObject.ComputerTurn();
			
			Invalidate(null);
			DisableButton();
			if(!m_GameObject.Playing_Game)WinnerMessage();
		}
		
		void BlueClick(object sender, System.EventArgs e)
		{
			index_array.AddRange(m_GameObject.g_Player1_Tiles.Keys);
			foreach(int n in index_array)
			{
				if(m_GameObject.g_Player1_Tiles.ContainsKey(n))
				{
					((Tile)m_GameObject.g_Player1_Tiles[n]).color = 2;
				}
			}
			index_array.RemoveRange(0, index_array.Count);
			
			m_GameObject.ScanTiles(true);
			
			m_GameObject.ComputerTurn();
			
			Invalidate(null);
			DisableButton();
			if(!m_GameObject.Playing_Game)WinnerMessage();
		}
		
		void YellowClick(object sender, System.EventArgs e)
		{
			index_array.AddRange(m_GameObject.g_Player1_Tiles.Keys);
			foreach(int n in index_array)
			{
				if(m_GameObject.g_Player1_Tiles.ContainsKey(n))
				{
					((Tile)m_GameObject.g_Player1_Tiles[n]).color = 3;
				}
			}
			index_array.RemoveRange(0, index_array.Count);
			
			m_GameObject.ScanTiles(true);
			
			m_GameObject.ComputerTurn();
			Invalidate(null);
			DisableButton();
			if(!m_GameObject.Playing_Game)WinnerMessage();
		}
		
		void BlackClick(object sender, System.EventArgs e)
		{
			index_array.AddRange(m_GameObject.g_Player1_Tiles.Keys);
			foreach(int n in index_array)
			{
				if(m_GameObject.g_Player1_Tiles.ContainsKey(n))
				{
					((Tile)m_GameObject.g_Player1_Tiles[n]).color = 0;
				}
			}
			index_array.RemoveRange(0, index_array.Count);
			
			m_GameObject.ScanTiles(true);
			
			m_GameObject.ComputerTurn();
			Invalidate(null);
			DisableButton();
			if(!m_GameObject.Playing_Game)WinnerMessage();
		}
		
		void FushiaClick(object sender, System.EventArgs e)
		{
			index_array.AddRange(m_GameObject.g_Player1_Tiles.Keys);
			foreach(int n in index_array)
			{
				if(m_GameObject.g_Player1_Tiles.ContainsKey(n))
				{
					((Tile)m_GameObject.g_Player1_Tiles[n]).color = 4;
				}
			}
			index_array.RemoveRange(0, index_array.Count);
			
			m_GameObject.ScanTiles(true);
			
			m_GameObject.ComputerTurn();
			
			Invalidate(null);
			DisableButton();
			if(!m_GameObject.Playing_Game)WinnerMessage();
		}
		
		void GreenClick(object sender, System.EventArgs e)
		{
			index_array.AddRange(m_GameObject.g_Player1_Tiles.Keys);
			foreach(int n in index_array)
			{
				if(m_GameObject.g_Player1_Tiles.ContainsKey(n))
				{
					((Tile)m_GameObject.g_Player1_Tiles[n]).color = 5;
				}
			}
			index_array.RemoveRange(0, index_array.Count);
			
			m_GameObject.ScanTiles(true);
			
			m_GameObject.ComputerTurn();
			Invalidate(null);
			DisableButton();
		}
		
		public void WinnerMessage()
		{
			DialogResult result = MessageBox.Show(m_GameObject.Winner + "\nDo you want to Play Again?", "Game Over!", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1);
			switch(result) {
				case DialogResult.Yes:
					m_GameObject.InitGame();
					Invalidate(null);
					break;
				case DialogResult.No:
					break;
				case DialogResult.Ignore:
					break;
			}
			
			DisableButton();

		}
		
		void EasyClick(object sender, System.EventArgs e)
		{
			m_GameObject.g_easy = true;
			m_GameObject.g_medium = false;
			m_GameObject.g_hard = false;
			
			m_GameObject.InitGame();
			DisableButton();
			Invalidate(null);
		}
		
		void MediumClick(object sender, System.EventArgs e)
		{
			m_GameObject.g_easy = false;
			m_GameObject.g_medium = true;
			m_GameObject.g_hard = false;
			
			m_GameObject.InitGame();
			DisableButton();
			
			Invalidate(null);
		}
		
		void HardClick(object sender, System.EventArgs e)
		{
			m_GameObject.g_easy = false;
			m_GameObject.g_medium = false;
			m_GameObject.g_hard = true;
			
			m_GameObject.InitGame();
			DisableButton();
			
			Invalidate(null);
		}
		
	}			
}

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
Software Developer (Senior) Philips
United States United States
I'm a senior software engineer at Philips. I work on Precision Diagnostics software.

Comments and Discussions