Click here to Skip to main content
12,699,054 members (23,996 online)
Click here to Skip to main content

Stats

49K views
1.2K downloads
79 bookmarked
Posted

The Bricks Game for Silverlight

, 8 Sep 2010 CPOL
An article exploring the use of MVVM, styles and templates in game programming in Silverlight 4
Bricks.Silverlight
Assemblies
Bin
Debug
ar
System.ComponentModel.DataAnnotations.resources.dll
bg
System.ComponentModel.DataAnnotations.resources.dll
Bricks.Silverlight.dll
Bricks.Silverlight.pdb
Bricks.Silverlight.xap
ca
System.ComponentModel.DataAnnotations.resources.dll
cp.png
cs
System.ComponentModel.DataAnnotations.resources.dll
da
System.ComponentModel.DataAnnotations.resources.dll
de
System.ComponentModel.DataAnnotations.resources.dll
System.ServiceModel.Web.Extensions.resources.dll
el
System.ComponentModel.DataAnnotations.resources.dll
es
System.ComponentModel.DataAnnotations.resources.dll
System.ServiceModel.Web.Extensions.resources.dll
et
System.ComponentModel.DataAnnotations.resources.dll
eu
System.ComponentModel.DataAnnotations.resources.dll
fi
System.ComponentModel.DataAnnotations.resources.dll
fr
System.ComponentModel.DataAnnotations.resources.dll
System.ServiceModel.Web.Extensions.resources.dll
GalaSoft.MvvmLight.dll
he
System.ComponentModel.DataAnnotations.resources.dll
hr
System.ComponentModel.DataAnnotations.resources.dll
hu
System.ComponentModel.DataAnnotations.resources.dll
id
System.ComponentModel.DataAnnotations.resources.dll
Images
cp.png
it
System.ComponentModel.DataAnnotations.resources.dll
System.ServiceModel.Web.Extensions.resources.dll
ja
System.ComponentModel.DataAnnotations.resources.dll
System.ServiceModel.Web.Extensions.resources.dll
ko
System.ComponentModel.DataAnnotations.resources.dll
System.ServiceModel.Web.Extensions.resources.dll
lt
System.ComponentModel.DataAnnotations.resources.dll
lv
System.ComponentModel.DataAnnotations.resources.dll
ms
System.ComponentModel.DataAnnotations.resources.dll
nl
System.ComponentModel.DataAnnotations.resources.dll
no
System.ComponentModel.DataAnnotations.resources.dll
pl
System.ComponentModel.DataAnnotations.resources.dll
pt
System.ComponentModel.DataAnnotations.resources.dll
pt-BR
System.ComponentModel.DataAnnotations.resources.dll
ro
System.ComponentModel.DataAnnotations.resources.dll
ru
System.ComponentModel.DataAnnotations.resources.dll
System.ServiceModel.Web.Extensions.resources.dll
sk
System.ComponentModel.DataAnnotations.resources.dll
sl
System.ComponentModel.DataAnnotations.resources.dll
sr-Cyrl-CS
System.ComponentModel.DataAnnotations.resources.dll
sr-Latn-CS
System.ComponentModel.DataAnnotations.resources.dll
sv
System.ComponentModel.DataAnnotations.resources.dll
System.ComponentModel.DataAnnotations.dll
System.ComponentModel.DataAnnotations.zip
System.ServiceModel.DomainServices.Client.dll
System.ServiceModel.DomainServices.Client.Web.dll
System.ServiceModel.DomainServices.Client.Web.zip
System.ServiceModel.DomainServices.Client.zip
System.ServiceModel.Web.Extensions.dll
System.ServiceModel.Web.Extensions.zip
th
System.ComponentModel.DataAnnotations.resources.dll
tr
System.ComponentModel.DataAnnotations.resources.dll
uk
System.ComponentModel.DataAnnotations.resources.dll
vi
System.ComponentModel.DataAnnotations.resources.dll
zh-Hans
System.ComponentModel.DataAnnotations.resources.dll
System.ServiceModel.Web.Extensions.resources.dll
zh-Hant
System.ComponentModel.DataAnnotations.resources.dll
System.ServiceModel.Web.Extensions.resources.dll
Bricks.Silverlight.csproj.user
Bricks.Silverlight.suo
Commands
Controls
cp.png
Images
cp.png
obj
Debug
Bricks.Silverlight.dll
Bricks.Silverlight.g.resources
Bricks.Silverlight.pdb
DesignTimeResolveAssemblyReferences.cache
DesignTimeResolveAssemblyReferencesInput.cache
ResolveAssemblyReference.cache
System.ComponentModel.DataAnnotations.zip.cache
System.ServiceModel.DomainServices.Client.Web.zip.cache
System.ServiceModel.DomainServices.Client.zip.cache
System.ServiceModel.Web.Extensions.zip.cache
TempPE
Views
Properties
ViewModels
Views
Bricks.Silverlight.Core
Bin
Debug
Bricks.Silverlight.Core.dll
Bricks.Silverlight.Core.pdb
Bricks.Silverlight.Core.csproj.user
Exceptions
obj
Debug
Bricks.Silverlight.Core.dll
Bricks.Silverlight.Core.pdb
DesignTimeResolveAssemblyReferencesInput.cache
TempPE
Properties
Shapes
Bricks.Silverlight.Web
bin
Bricks.Silverlight.Web.dll
Bricks.Silverlight.Web.pdb
Bricks.Silverlight.Web.csproj.user
ClientBin
Bricks.Silverlight.xap
Debug
Bricks.Silverlight.xap
System.ComponentModel.DataAnnotations.zip
System.ServiceModel.DomainServices.Client.Web.zip
System.ServiceModel.DomainServices.Client.zip
System.ServiceModel.Web.Extensions.zip
Views
Click01.mp3
cp.png
Drums.mp3
obj
Debug
Bricks.Silverlight.Web.dll
Bricks.Silverlight.Web.g.resources
Bricks.Silverlight.Web.pdb
DesignTimeResolveAssemblyReferencesInput.cache
TempPE
Properties
Service References
System.ComponentModel.DataAnnotations.resources.dll
System.ComponentModel.DataAnnotations.resources.dll
Bricks.Silverlight.dll
Bricks.Silverlight.pdb
Bricks.Silverlight.xap
System.ComponentModel.DataAnnotations.resources.dll
cp.png
System.ComponentModel.DataAnnotations.resources.dll
System.ComponentModel.DataAnnotations.resources.dll
System.ComponentModel.DataAnnotations.resources.dll
System.ServiceModel.Web.Extensions.resources.dll
System.ComponentModel.DataAnnotations.resources.dll
System.ComponentModel.DataAnnotations.resources.dll
System.ServiceModel.Web.Extensions.resources.dll
System.ComponentModel.DataAnnotations.resources.dll
System.ComponentModel.DataAnnotations.resources.dll
System.ComponentModel.DataAnnotations.resources.dll
System.ComponentModel.DataAnnotations.resources.dll
System.ServiceModel.Web.Extensions.resources.dll
GalaSoft.MvvmLight.dll
System.ComponentModel.DataAnnotations.resources.dll
System.ComponentModel.DataAnnotations.resources.dll
System.ComponentModel.DataAnnotations.resources.dll
System.ComponentModel.DataAnnotations.resources.dll
cp.png
System.ComponentModel.DataAnnotations.resources.dll
System.ServiceModel.Web.Extensions.resources.dll
System.ComponentModel.DataAnnotations.resources.dll
System.ServiceModel.Web.Extensions.resources.dll
System.ComponentModel.DataAnnotations.resources.dll
System.ServiceModel.Web.Extensions.resources.dll
System.ComponentModel.DataAnnotations.resources.dll
System.ComponentModel.DataAnnotations.resources.dll
System.ComponentModel.DataAnnotations.resources.dll
System.ComponentModel.DataAnnotations.resources.dll
System.ComponentModel.DataAnnotations.resources.dll
System.ComponentModel.DataAnnotations.resources.dll
System.ComponentModel.DataAnnotations.resources.dll
System.ComponentModel.DataAnnotations.resources.dll
System.ComponentModel.DataAnnotations.resources.dll
System.ComponentModel.DataAnnotations.resources.dll
System.ServiceModel.Web.Extensions.resources.dll
System.ComponentModel.DataAnnotations.resources.dll
System.ComponentModel.DataAnnotations.resources.dll
System.ComponentModel.DataAnnotations.resources.dll
System.ComponentModel.DataAnnotations.resources.dll
System.ComponentModel.DataAnnotations.resources.dll
System.ComponentModel.DataAnnotations.dll
System.ComponentModel.DataAnnotations.zip
System.ServiceModel.DomainServices.Client.dll
System.ServiceModel.DomainServices.Client.Web.dll
System.ServiceModel.DomainServices.Client.Web.zip
System.ServiceModel.DomainServices.Client.zip
System.ServiceModel.Web.Extensions.dll
System.ServiceModel.Web.Extensions.zip
System.ComponentModel.DataAnnotations.resources.dll
System.ComponentModel.DataAnnotations.resources.dll
System.ComponentModel.DataAnnotations.resources.dll
System.ComponentModel.DataAnnotations.resources.dll
System.ComponentModel.DataAnnotations.resources.dll
System.ServiceModel.Web.Extensions.resources.dll
System.ComponentModel.DataAnnotations.resources.dll
System.ServiceModel.Web.Extensions.resources.dll
Bricks.Silverlight.csproj.user
Bricks.Silverlight.suo
cp.png
cp.png
Bricks.Silverlight.dll
Bricks.Silverlight.g.resources
Bricks.Silverlight.pdb
DesignTimeResolveAssemblyReferences.cache
DesignTimeResolveAssemblyReferencesInput.cache
ResolveAssemblyReference.cache
System.ComponentModel.DataAnnotations.zip.cache
System.ServiceModel.DomainServices.Client.Web.zip.cache
System.ServiceModel.DomainServices.Client.zip.cache
System.ServiceModel.Web.Extensions.zip.cache
Bricks.Silverlight.Core.csproj.user
Bricks.Silverlight.Core.dll
Bricks.Silverlight.Core.pdb
DesignTimeResolveAssemblyReferencesInput.cache
Bricks.Silverlight.Web.dll
Bricks.Silverlight.Web.pdb
Bricks.Silverlight.Web.csproj.user
Bricks.Silverlight.xap
Bricks.Silverlight.xap
System.ComponentModel.DataAnnotations.zip
System.ServiceModel.DomainServices.Client.Web.zip
System.ServiceModel.DomainServices.Client.zip
System.ServiceModel.Web.Extensions.zip
Click01.mp3
cp.png
Drums.mp3
Bricks.Silverlight.Web.dll
Bricks.Silverlight.Web.g.resources
Bricks.Silverlight.Web.pdb
DesignTimeResolveAssemblyReferencesInput.cache
using System;
//using System.Drawing;
using System.Diagnostics;
using Bricks.Silverlight.Core;
using Bricks.Silverlight.Core.Exceptions;
using System.Windows.Media;

namespace Bricks.Silverlight.Core.Shapes
{
    /// <summary>
    /// Represents the base shape for Bricks game.
    /// </summary>
    public abstract class BaseShape : BaseBricksArray, IShape
    {
        protected int x = 0;
        protected int y = 0;
        protected Color color = Colors.White;
        protected bool anchored = false;
        protected IBoard containerBoard = null;

        public BaseShape(int x, int y, int width, int height, string shapeString)
        {
            if (x < 0)
                throw new ArgumentOutOfRangeException("x");

            if (y < 0)
                throw new ArgumentOutOfRangeException("y");

            this.x = x;
            this.y = y;
            LoadData(width, height, shapeString);
        }

        public BaseShape(int width, int height, string shapeString)
        {
            LoadData(width, height, shapeString);
        }

        private void LoadData(int width, int height, string shapeString)
        {
            if (shapeString.Length != width * height)
            {
                throw new InvalidShapeSizeException();
            }
            else if (HasInvalidShapeCharacter(shapeString))
            {
                throw new InvalidShapeStringCharacterException();
            }
            else
            {
                this.width = width;
                this.height = height;
                this.shapeString = shapeString;
                this.shapeArray = new IBrick[width, height];
                int i = 0;
                for (int row = 0; row < height; row++)
                {
                    for(int column = 0; column < width; column++)
                    {
                        int nColor = Convert.ToInt32(shapeString.Substring(i, 1));
                        IBrick brick = null;
                        if (nColor > 0)
                        {
                            brick = new Brick(column, row, GetShapeColorFromInteger(nColor));
                        }
                        shapeArray[column, row] = brick;
                        i++;
                    }
                }
            }
        }

        private static bool HasInvalidShapeCharacter(string shapeString)
        {
            bool ret = false;
            foreach(Char c in shapeString.ToCharArray())
            {
                if (c.ToString() != "0" && c.ToString() != "1")
                {
                    ret = true;
                }
            }
            return ret;
        }

        public void Anchor()
        {
            anchored = true;
        }

        public bool MoveLeft()
        {
            bool test = false;
            if (!anchored)
            {
                if (containerBoard == null)
                    throw new NullContainerBoardException();

                containerBoard.RemovePieceFromCurrentPosition(this);

                test = containerBoard.TestPieceOnPosition(this, this.X - 1, this.Y);
                if (test)
                {
                    containerBoard.RemovePieceFromCurrentPosition(this);
                    containerBoard.PutPieceOnPosition(this, this.X - 1, this.Y);
                }
            }
            return test;
        }

        public bool MoveRight()
        {
            bool test = false;
            if (!anchored)
            {
                if (containerBoard == null)
                    throw new NullContainerBoardException();

                containerBoard.RemovePieceFromCurrentPosition(this);

                test = containerBoard.TestPieceOnPosition(this, this.X + 1, this.Y);
                if (test)
                {
                    containerBoard.PutPieceOnPosition(this, this.X + 1, this.Y);
                }
            }
            return test;
        }

        public bool MoveDown()
        {
            bool test = false;

            if (!anchored)
            {
                containerBoard.RemovePieceFromCurrentPosition(this);

                //should anchor if shape can't move down from current position
                if (!containerBoard.TestPieceOnPosition(this, this.X, this.Y + 1))
                {
                    containerBoard.PutPieceOnPosition(this, this.X, this.Y);
                    this.Anchor();
                }
                else
                {
                    if (containerBoard == null)
                        throw new NullContainerBoardException();

                    test = containerBoard.TestPieceOnPosition(this, this.X, this.Y + 1);
                    if (test)
                    {
                        containerBoard.PutPieceOnPosition(this, this.X, this.Y + 1);
                    }
                }
            }

            return test;
        }

        public bool Rotate90()
        {
            bool test = false;
            if (!anchored)
            {
                if (containerBoard == null)
                    throw new NullContainerBoardException();

                IBrick[,] newShapeArray = new IBrick[height, width];
                IBrick[,] oldShapeArray = new IBrick[width, height];
                for (int row = 0; row < height; row++)
                {
                    for (int column = 0; column < width; column++)
                    {
                        newShapeArray[height - row - 1, column] = shapeArray[column, row];
                        oldShapeArray[column, row] = shapeArray[column, row];
                    }
                }

                containerBoard.RemovePieceFromCurrentPosition(this);

                int w = width;
                int h = height;
                this.width = h;
                this.height = w;
                this.shapeArray = newShapeArray;

                if (containerBoard.TestPieceOnPosition(this, this.X, this.Y))
                {
                    containerBoard.PutPieceOnPosition(this, this.X, this.Y);
                }
                else
                {
                    this.width = w;
                    this.height = h;
                    this.shapeArray = oldShapeArray;
                    containerBoard.PutPieceOnPosition(this, this.X, this.Y);
                }
            }
            return test;
        }

        public bool Rotate270()
        {
            bool test = false;
            if (!anchored)
            {
                if (containerBoard == null)
                    throw new NullContainerBoardException();

                containerBoard.RemovePieceFromCurrentPosition(this);

                IBrick[,] newShapeArray = new IBrick[height, width];
                IBrick[,] oldShapeArray = new IBrick[width, height];
                for (int row = 0; row < height; row++)
                {
                    for (int column = 0; column < width; column++)
                    {
                        newShapeArray[row, width - column - 1] = shapeArray[column, row];
                        oldShapeArray[column, row] = shapeArray[column, row];
                    }
                }

                int w = width;
                int h = height;
                this.width = h;
                this.height = w;
                this.shapeArray = newShapeArray;

                if (containerBoard.TestPieceOnPosition(this, this.X, this.Y))
                {
                    containerBoard.PutPieceOnPosition(this, this.X, this.Y);
                }
                else
                {
                    this.width = w;
                    this.height = h;
                    this.shapeArray = oldShapeArray;
                    containerBoard.PutPieceOnPosition(this, this.X, this.Y);
                }
            }
            return test;
        }

        public int X
        {
            get { return x; }
            set { x = value; }
        }

        public int Y
        {
            get { return y; }
            set { y = value; }
        }

        public Color Color
        {
            get {return color;}
            set
            {
                color = value;
                for (int row = 0; row < height; row++)
                {
                    for (int column = 0; column < width; column++)
                    {
                        IBrick brick = shapeArray[column, row];
                        if (brick != null)
                        {
                            brick.Color = value;
                        }
                    }
                }
            }
        }

        public IBoard ContainerBoard
        {
            get { return containerBoard; }
            set { containerBoard = value; }
        }

        public bool Anchored
        { 
            get {return anchored;}
        }
    }

    //public enum ShapeColor
    //{
    //    Black = 0,
    //    Blue,
    //    Gold,
    //    Green,
    //    Orange,
    //    Pink,
    //    Red,
    //    Violet,
    //    White,
    //    Yellow
    //}
}

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)

Share

About the Author


You may also be interested in...

Pro
| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170118.1 | Last Updated 8 Sep 2010
Article Copyright 2010 by Marcelo Ricardo de Oliveira
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid