Click here to Skip to main content
15,885,954 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 142K   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 SplashScreen.xaml
    /// </summary>
    public partial class SplashScreen : UserControl
    {
        List<Ellipse> bubbles = new List<Ellipse>();
        List<Storyboard> storyBoards = new List<Storyboard>();
        TimeSpan ts = TimeSpan.FromMilliseconds(3000);
        public SplashScreen()
        {
            InitializeComponent();

            CreateBubbles();
        }


        private void CreateBubbles()
        {
            Storyboard sbPressSpace = this.FindResource("sbPressSpace") as Storyboard;
            sbPressSpace.Begin();

            var linearBubbleBrush = new LinearGradientBrush() { StartPoint = new Point(1, 0), EndPoint = new Point(0, 1) };
            linearBubbleBrush.GradientStops.Add(new GradientStop(Color.FromArgb(0xFF, 0x00, 0x20, 0x40), 0.0));
            linearBubbleBrush.GradientStops.Add(new GradientStop(Color.FromArgb(0x00, 0xFF, 0xFF, 0xFF), 1.0));

            var radialBubbleBrush = new RadialGradientBrush() { Center = new Point(0.25, 0.75), RadiusX = .3, RadiusY = .2, GradientOrigin = new Point(0.35, 0.75) };
            radialBubbleBrush.GradientStops.Add(new GradientStop(Color.FromArgb(0xFF, 0xFF, 0xFF, 0xFF), 0.0));
            radialBubbleBrush.GradientStops.Add(new GradientStop(Color.FromArgb(0x00, 0xFF, 0xFF, 0xFF), 1.0));

            for (var i = 0; i < 10; i++)
            {
                var diameter = 10 + (i % 4) * 10;
                var ms = 1000 + i % 7 * 500;

                var ellBubble = new Ellipse()
                {
                    Width = diameter,
                    Height = diameter,
                    Stroke = linearBubbleBrush,
                    Fill = radialBubbleBrush,
                    StrokeThickness = 3
                };

                ellBubble.SetValue(Canvas.LeftProperty, i * (40.0 + 40.0 - diameter / 2));
                ellBubble.SetValue(Canvas.TopProperty, 0.0 + 40.0 - diameter / 2);

                cnvBubbles.Children.Add(ellBubble);

                var leftAnimation = new DoubleAnimation()
                {
                    From = 40.0 * i,
                    To = 40.0 * i,
                    Duration = TimeSpan.FromMilliseconds(ms)
                };
                var topAnimation = new DoubleAnimation()
                {
                    From = 200,
                    To = 0,
                    Duration = TimeSpan.FromMilliseconds(ms)
                };
                var opacityAnimation = new DoubleAnimation()
                {
                    From = 1.0,
                    To = 0.0,
                    Duration = TimeSpan.FromMilliseconds(ms)
                };
                Storyboard.SetTarget(leftAnimation, ellBubble);
                Storyboard.SetTargetProperty(leftAnimation, new PropertyPath("(Canvas.Left)"));
                Storyboard.SetTarget(topAnimation, ellBubble);
                Storyboard.SetTargetProperty(topAnimation, new PropertyPath("(Canvas.Top)"));
                Storyboard.SetTarget(opacityAnimation, ellBubble);
                Storyboard.SetTargetProperty(opacityAnimation, new PropertyPath("Opacity"));
                leftAnimation.EasingFunction = new BackEase() { Amplitude = 0.5, EasingMode = EasingMode.EaseOut };
                topAnimation.EasingFunction = new BackEase() { Amplitude = 0.5, EasingMode = EasingMode.EaseOut };

                var sb = new Storyboard();
                sb.Children.Add(leftAnimation);
                sb.Children.Add(topAnimation);
                sb.Children.Add(opacityAnimation);
                sb.RepeatBehavior = RepeatBehavior.Forever;

                bubbles.Add(ellBubble);
                storyBoards.Add(sb);

                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