Click here to Skip to main content
15,888,113 members
Articles / Desktop Programming / WPF

Snail Quest - A Maze Game Using WPF, A* Search Algorithm, C# Midi Toolkit and Irrklang Audio Engine

Rate me:
Please Sign up or sign in to vote.
4.99/5 (68 votes)
7 Apr 2011CPOL12 min read 142.4K   4.1K   103  
This article covers the creation of maze game all the way from its WPF animations to its music integration with two sound engines.
This article covers the creation of maze game, following it from a text file to a WPF, going through all the WPF animations involved with the game, as well as the non WPF animations, made by switching between differnt configurations. The article also covers how input corresponds to cell coordinates, utilizes the A* Search algorithm for the enemies, and goes over the implementation of the music for the game, which involves two sound engines, Midi Toolkit and IrrKlang.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Media.Animation;

namespace SnailQuest.Controls
{
    /// <summary>
    /// Interaction logic for Pearl.xaml
    /// </summary>
    public partial class Pearl : UserControl
    {
        int animationMs = 300;
        const int cellWidth = 60;
        private int squidX = 2;
        private int squidY = 2;
        const int mazeWidth = 15;
        const int mazeHeight = 10;

        public Pearl()
        {
            InitializeComponent();
        }

        public Point GetCellPoint()
        {
            var left = (double)this.GetValue(Canvas.LeftProperty);
            var top = (double)this.GetValue(Canvas.TopProperty);

            var squidX = (int)(left) / cellWidth;
            var squidY = (int)(top) / cellWidth;
            return new Point(squidX, squidY);
        }

        public Rect GetRect(Canvas canvas)
        {
            GeneralTransform transform = this.TransformToVisual(canvas);
            return transform.TransformBounds(new Rect(0, 0, this.ActualWidth, this.ActualHeight));
        }

        public void PlaceAt(Point cellPoint)
        {
            this.Visibility = System.Windows.Visibility.Visible;
            var leftAnimation = new DoubleAnimation()
            {
                From = cellPoint.X * cellWidth + 15,
                To = cellPoint.X * cellWidth + 15,
                Duration = TimeSpan.FromMilliseconds(0),
            };
            var topAnimation = new DoubleAnimation()
            {
                From = cellPoint.Y * cellWidth + 15,
                To = cellPoint.Y * cellWidth + 15,
                Duration = TimeSpan.FromMilliseconds(0),
            };
            Storyboard.SetTarget(leftAnimation, this);
            Storyboard.SetTargetProperty(leftAnimation, new PropertyPath("(Canvas.Left)"));
            Storyboard.SetTarget(topAnimation, this);
            Storyboard.SetTargetProperty(topAnimation, new PropertyPath("(Canvas.Top)"));

            var sb = new Storyboard();
            sb.Children.Add(leftAnimation);
            sb.Children.Add(topAnimation);
            sb.Begin();
        }
    }
}

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
Instructor / Trainer Alura Cursos Online
Brazil Brazil

Comments and Discussions