Click here to Skip to main content
15,896,118 members
Articles / Web Development / ASP.NET

Silverlight Alien Sokoban

Rate me:
Please Sign up or sign in to vote.
4.88/5 (59 votes)
11 Nov 200717 min read 134K   1K   138  
A fun Silverlight implementation of the game Sokoban. Contrasting Silverlight 1.1 and WPF, while showcasing some new features of C# 3.0, Expression Design, Expression Blend, and Visual Studio 2008.
/*	<File>
		<Author Name="Daniel Vaughan" Email="dbvaughan NOSPAM at NOSPAM gmail NOSPAM dot NOSPAM com"/>
		<CreationDate>2007/11/5 12:10</CreationDate>
		<LastSubmissionDate>$Date: $</LastSubmissionDate>
		<Version>$Revision: $</Version>
		<License>
Copyright © 2007, Daniel Vaughan
All rights reserved.
http://www.orpius.com

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:

- Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.

- Neither Daniel Vaughan, nor the names of its
contributors may be used to endorse or promote products
derived from this software without specific prior written 
permission. 

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
POSSIBILITY OF SUCH DAMAGE.
		</License>
	</File>
*/

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Orpius.Sokoban.Controls
{
	/// <summary>
	/// Allows the user to control a <see cref="Orpius.Sokoban.Game"/> instance.
	/// </summary>
	public class PlayControl : Control
	{
		FrameworkElement root;
		TextBlock textBlock_LevelNumber;
		TextBlock textBlock_MoveCount;
		TextBlock textBlock_LevelCode;
		Storyboard restartLevelMouseOverStoryboard;
		Storyboard restartLevelMouseOutStoryboard;

		int levelCount;
		int levelNumber;
		int moveCount;
		string levelCode;

		/// <summary>
		/// Gets or sets the level count on the control.
		/// </summary>
		/// <value>The level count.</value>
		public int LevelCount
		{
			get
			{
				return levelCount;
			}
			set
			{
				levelCount = value;
				FormatLevelNumber();
			}
		}

		/// <summary>
		/// Gets or sets the level number displayed on the control.
		/// </summary>
		/// <value>The level number.</value>
		public int LevelNumber
		{
			get
			{
				return levelNumber;
			}
			set
			{
				levelNumber = value;
				FormatLevelNumber();
			}
		}

		/// <summary>
		/// Gets or sets the move count displayed on the control.
		/// </summary>
		/// <value>The move count.</value>
		public int MoveCount
		{
			get
			{
				return moveCount;
			}
			set
			{
				moveCount = value;
				textBlock_MoveCount.Text = moveCount + " Moves";
			}
		}

		/// <summary>
		/// Gets or sets the level code displayed on the control.
		/// </summary>
		/// <value>The level code.</value>
		public string LevelCode
		{
			get
			{
				return levelCode;
			}
			set
			{
				levelCode = value;
				textBlock_LevelCode.Text = value;
			}
		}

		void FormatLevelNumber()
		{
			textBlock_LevelNumber.Text = levelNumber + "/" + levelCount;
		}

		public PlayControl()
		{
			System.IO.Stream s = this.GetType().Assembly.GetManifestResourceStream("Orpius.Sokoban.Controls.PlayControl.xaml");
			root = InitializeFromXaml(new System.IO.StreamReader(s).ReadToEnd());

			Rectangle button_RestartLevel = (Rectangle)root.FindName("button_RestartLevel");
			button_RestartLevel.MouseEnter += new MouseEventHandler(button_RestartLevel_MouseEnter);
			button_RestartLevel.MouseLeave += new EventHandler(button_RestartLevel_MouseLeave);
			button_RestartLevel.MouseLeftButtonUp += new MouseEventHandler(button_RestartLevel_MouseLeftButtonUp);
			textBlock_LevelNumber = (TextBlock)root.FindName("textBlock_LevelNumber");
			textBlock_MoveCount = (TextBlock)root.FindName("textBlock_MoveCount");
			textBlock_LevelCode = (TextBlock)root.FindName("textBlock_LevelCode");

			restartLevelMouseOverStoryboard = (Storyboard)root.FindName("RestartLevelMouseOver");
			restartLevelMouseOutStoryboard = (Storyboard)root.FindName("RestartLevelMouseOut");

			LevelCount = 0;
			LevelNumber = 0;
			MoveCount = 0;
		}

		void button_RestartLevel_MouseEnter(object sender, MouseEventArgs e)
		{
			restartLevelMouseOverStoryboard.Begin();
		}

		void button_RestartLevel_MouseLeave(object sender, EventArgs e)
		{
			restartLevelMouseOutStoryboard.Begin();
		}

		void button_RestartLevel_MouseLeftButtonUp(object sender, MouseEventArgs e)
		{
			OnRestartLevel(e);
		}

		#region Restart Level event
		event EventHandler restartLevel;

		/// <summary>
		/// Occurs when the user presses the restart level button.
		/// </summary>
		public event EventHandler RestartLevel
		{
			add
			{
				restartLevel += value;
			}
			remove
			{
				restartLevel -= value;
			}
		}

		/// <summary>
		/// Raises the <see cref="E:RestartLevel"/> event.
		/// </summary>
		/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
		protected void OnRestartLevel(EventArgs e)
		{
			if (restartLevel != null)
			{
				restartLevel(this, e);
			}
		}
		#endregion
		#region Level Code Changed event
		event EventHandler levelCodeChanged;

		/// <summary>
		/// Occurs when a level code is entered into the text box.
		/// This indicates that the level should be changed 
		/// to one matching the level code.
		/// </summary>
		public event EventHandler LevelCodeChanged
		{
			add
			{
				levelCodeChanged += value;
			}
			remove
			{
				levelCodeChanged -= value;
			}
		}

		protected void OnLevelCodeChanged(EventArgs e)
		{
			if (levelCodeChanged != null)
			{
				levelCodeChanged(this, e);
			}
		}
		#endregion
	}
}

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Engineer
Switzerland Switzerland
Daniel is a former senior engineer in Technology and Research at the Office of the CTO at Microsoft, working on next generation systems.

Previously Daniel was a nine-time Microsoft MVP and co-founder of Outcoder, a Swiss software and consulting company.

Daniel is the author of Windows Phone 8 Unleashed and Windows Phone 7.5 Unleashed, both published by SAMS.

Daniel is the developer behind several acclaimed mobile apps including Surfy Browser for Android and Windows Phone. Daniel is the creator of a number of popular open-source projects, most notably Codon.

Would you like Daniel to bring value to your organisation? Please contact

Blog | Twitter


Xamarin Experts
Windows 10 Experts

Comments and Discussions