Latest

whatsapp (+234)07060722008
email sales@graciousnaija.com

Monday, 29 June 2015

How to design and Simulate Swimming Fish using the .NET - Free Source Code

In one of my Articles I showed you on How to Design Language Translator, Text-To-Speech (Speech Synthesis) with Microsoft Translator Services, today I'm going to show you how to Design and Simulate Swimming Fish.
What I meant by simulate swimming fish, is that, how to write a program to make a fish swim water. i.e in a lay man's understanding.

Technology - Visual studio 2010
Language - C#
Topic - How to Simulate Swimming Fish


Sample output image

Here is a sample C# Code


//##########################################################################
//¡Ã¯¡Ã¯¡Ã¯¡Ã¯¡Ã¯¡Ã¯¡Ã¯           08110288930, 07060722008           ¡Ã¯¡Ã¯¡Ã¯¡Ã¯¡Ã¯¡Ã¯¡Ã¯
//¡Ã¯¡Ã¯              C#, Java, VB & Windows Mobile Developer             ¡Ã¯¡Ã¯
//¡Ã¯¡Ã¯¡Ã¯¡Ã¯¡Ã¯¡Ã¯¡Ã¯               CLETUS IGBE                    ¡Ã¯¡Ã¯¡Ã¯¡Ã¯¡Ã¯¡Ã¯¡Ã¯
//##########################################################################
using System;
using System.IO;
using System.Drawing;
using System.Reflection;
using System.Diagnostics;
using System.Windows.Forms;
using System.Collections.Generic;
using FishTank.Properties;
namespace FishTank
{
    class Program : IDisposable
    {
        private Timer m_timer = new Timer();
        private IList<FishForm> m_fish = new List<FishForm>();
        private IList<FramesetPair> m_framesets = new List<FramesetPair>();
        private Random m_random = new Random(unchecked((int)(DateTime.Now.Ticks)));
        public Program()
        {
            LoadAnimations();  
        }
        private void Show()
        {
            int count = 1;
            if (Settings.Default.FishCount > 1)
                count = Settings.Default.FishCount;
            for (int i = 0; i < count; i++)
                CreateAndAddFish();
            m_timer.Interval = 50;
            m_timer.Enabled = true;
            m_timer.Tick += new EventHandler(timer_Tick);
            Application.Run(m_fish[0]);
        }
        public void Dispose()
        {
            m_timer.Dispose();
            m_fish.Clear();
            foreach (IDisposable d in m_framesets)
                d.Dispose();
            m_framesets.Clear();
        }
        /// <summary>
        /// Main Entry
        /// </summary>
        [STAThread]
        static void Main()
        {
            using (_program = new Program())
                _program.Show();
        }
        private static Program _program;
        public static Program TheProgram
        {
            get
            {
                return _program;
            }
        }
        private void LoadAnimations()
        {
            m_framesets.Add(LoadFramesets("Blue"));
            m_framesets.Add(LoadFramesets("Green"));
            m_framesets.Add(LoadFramesets("Orange"));
            m_framesets.Add(LoadFramesets("Pink"));
            m_framesets.Add(LoadFramesets("Yellow"));
            m_framesets.Add(LoadFramesets("Red"));
        }
        private static FramesetPair LoadFramesets(string color)
        {
            using (Stream left = Assembly.GetExecutingAssembly().GetManifestResourceStream(string.Format("FishTank.Resources.{0}.Left.png", color)))
            using (Stream right = Assembly.GetExecutingAssembly().GetManifestResourceStream(string.Format("FishTank.Resources.{0}.Right.png", color)))
            using (Bitmap leftBitmap = new Bitmap(left))
            using (Bitmap rightBitmap = new Bitmap(right))
                return new FramesetPair(new Frameset(leftBitmap, 20), new Frameset(rightBitmap, 20));
        }
        private void CreateAndAddFish()
        {
            Rectangle tank = new Rectangle();
            foreach (Screen screen in Screen.AllScreens)
                tank = Rectangle.Union(tank, screen.WorkingArea);
            FishAnimation animation = new FishAnimation(tank, GetFramesets());
            FishForm f = null;
            if (m_fish.Count > 0)
                f = new FishForm(animation);
            else
                f = new SysTrayFishForm(animation);
            f.Disposed += new EventHandler(f_Disposed);
            m_fish.Add(f);
        }
        private FramesetPair GetFramesets()
        {
            return m_framesets[m_random.Next(m_framesets.Count)];
        }
        public void AddFish()
        {
            CreateAndAddFish();
            Settings.Default.FishCount = m_fish.Count;
            Settings.Default.Save();
        }
        public void ShowHide()
        {
            m_timer.Enabled = !m_timer.Enabled;
            foreach (FishForm f in m_fish)
                f.Visible = m_timer.Enabled;
        }
        public void RemoveFish()
        {
            if (m_fish.Count >= 1)
            {
                m_fish[m_fish.Count - 1].Dispose();
                Settings.Default.FishCount = m_fish.Count;
                Settings.Default.Save();
            }
        }
        private void f_Disposed(object sender, EventArgs e)
        {
            FishForm f = (FishForm)sender;
            m_fish.Remove(f);
            f.Disposed -= new EventHandler(f_Disposed);
        }
        private void timer_Tick(object sender, EventArgs e)
        {
            foreach (FishForm f in m_fish)
                f.UpdateFish();
        }
    }
}

No comments:

Post a Comment

Add Comment