Puzzle 4x4

Description

Un puzzle intéressant, qui illustre le découpage des images en C# en un jeu qui est le puzzle et un algorithme efficace pour permettre que le jeu soit des plus passionnants

Source / Exemple :


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;
using System.Windows;

namespace Puzzle
{
    public partial class puzzle : Form
    {
        public int numBoutonAppuyE;
        public bool Jeu_deja_commencE = false;
        public bool resultat_bouton = false;
        public ImageForm FormOuvert = null;
        Panel lePanelPrincipal;
        bool DeplacementHorizontal;
        bool DeplacementVertical;
        public Image ImageOrigine;
        Panel[] Puzzle;
        int compte = 0;
        bool[] Position;
        string carreVide, carreVideTemp;
        Panel PanelcliquE;
        int x, y, NombreToursTimer;
        bool increment = false;

        public puzzle()
        {
            InitializeComponent();
        }
        private void PlacerPanel(Panel lePanel)
        {
            Random Alea = new Random();
            int valeurTest=0;
            do
            {
                valeurTest = Alea.Next(0, 15);
            }
            while(Position[valeurTest]!=false);
            Position[valeurTest] = true;
            HauteurLargeur(valeurTest);
        }
        void HauteurLargeur(int position)
        {
            
            int Vertical = 0;
            int Horizontal = 0;
            Horizontal = (position % 4);
            Vertical = (position - Horizontal) / 4;
            ((Panel)Puzzle[compte]).Top = Vertical * (lePanelPrincipal.Height / 4);
            ((Panel)Puzzle[compte]).Left = Horizontal * (lePanelPrincipal.Width / 4);
            ((Panel)Puzzle[compte]).Tag = (Horizontal + 1) + "" + (Vertical + 1);
        }
        private void puzzle_Load(object sender, EventArgs e)
        {
            lePanelPrincipal = new Panel();

            ChargerForm();
            Jeu_deja_commencE = true;
        }
        void ChargerForm()
        {
            if (Choix_Image())
            {
            carreVide = "44";
            Puzzle = new Panel[15];
            NombreToursTimer = 0;
            Position = new bool[15];
            for (int i = 0; i < 15; i++)
            {
                Position[i] = false;
            }

            compte = 0;
            
                lePanelPrincipal.Dispose();
                lePanelPrincipal = new Panel();
                lePanelPrincipal.Parent = this;
                lePanelPrincipal.Left = 1;
                lePanelPrincipal.Top = 28;
                lePanelPrincipal.Width = 386;
                lePanelPrincipal.Height = 290;
                lePanelPrincipal.BackColor = Color.Honeydew;
                for (int i = 0; i < ImageOrigine.Height; i += ImageOrigine.Height / 4)
                {
                    for (int j = 0; j < ImageOrigine.Width; j += ImageOrigine.Width / 4)
                    {
                        if (compte < 15)
                        {
                            Rectangle leRectangle = new Rectangle(j, i, ImageOrigine.Width / 4, ImageOrigine.Height / 4);
                            Panel panelCourant = new Panel();
                            panelCourant.BorderStyle = BorderStyle.FixedSingle;
                            panelCourant.BackgroundImageLayout = ImageLayout.Stretch;
                            panelCourant.Height = lePanelPrincipal.Height / 4;
                            panelCourant.Width = lePanelPrincipal.Width / 4;
                            Puzzle[compte] = panelCourant;
                            panelCourant.Cursor = Cursors.Hand;
                            PlacerPanel(panelCourant);
                            panelCourant.BackgroundImage = CopyBitmap(new Bitmap(ImageOrigine), leRectangle);
                            panelCourant.Parent = lePanelPrincipal;
                            panelCourant.Click += new EventHandler(panelCourant_Click);
                        }
                        compte++;
                    }
                }
            }
            if (FormOuvert != null)
                timerImage.Enabled = true;
                
            this.Activate();
        }
        void Click_Proced(Panel le_Panel)
        {
            x = int.Parse(le_Panel.Tag.ToString()[0] + "");
            y = int.Parse(le_Panel.Tag.ToString()[1] + "");
            DeplacementVertical = x + "" == carreVide.Substring(0, 1);
            DeplacementHorizontal = y + "" == carreVide.Substring(1);

            if (DeplacementVertical || DeplacementHorizontal)
            {
                if ((Math.Abs(x - int.Parse(carreVide.Substring(0, 1))) <= 1))
                {

                    if ((Math.Abs(y - int.Parse(carreVide.Substring(1))) <= 1))
                    {
                        //System.Media.SoundPlayer lecteur = new System.Media.SoundPlayer("SURVOL.wav");
                        //lecteur.PlaySync();

                        PanelcliquE = le_Panel;

                        carreVideTemp = le_Panel.Tag.ToString();

                        if (DeplacementVertical)
                            timerVertical.Enabled = true;
                        else
                            timerHorizontal.Enabled = true;
                    }
                }
            }
                
        }
        void panelCourant_Click(object sender, EventArgs e)
        {
            Click_Proced((Panel)sender);

        }
        bool EstRange()
        {
            bool resultat=true;
            for (int i = 0; i < 15; i++)
            {
                int Vertical = 0;
                int Horizontal = 0;
                Horizontal = (i % 4);
                Vertical = (i - Horizontal) / 4;

                if (Puzzle[i].Tag.ToString() != (Horizontal + 1) + "" + (Vertical + 1))
                {
                    resultat = false;
                    break;
                }
            }
            return resultat;
        }

        protected Bitmap CopyBitmap(Bitmap source, Rectangle part)
        {
            Bitmap bmp=new Bitmap(part.Width,part.Height);
            Graphics g = Graphics.FromImage(bmp);
            g.DrawImage(source, 0, 0, part, GraphicsUnit.Pixel);
            g.Dispose();
            return bmp;
        }
        bool Choix_Image()
        {
            do
            {
                try
                {
                    if (openFileDialogImage.ShowDialog() == DialogResult.OK)
                    {
                        resultat_bouton = false;
                        ImageOrigine = Image.FromFile(openFileDialogImage.FileName);
                    }
                    else
                    {
                        BoiteMessage nouveau = new BoiteMessage(this);
                        nouveau.ShowDialog();
                        if (numBoutonAppuyE == 3)
                            return false;
                    }
                }
                catch
                {
                    return false;
                }
            } 
            while (resultat_bouton);
            
            return true;
        }

        private void timerVertical_Tick(object sender, EventArgs e)
        {
            if (y < int.Parse(carreVide.Substring(1)))
                PanelcliquE.Top += ((Panel)Puzzle[0]).Height / 10;
            else
                PanelcliquE.Top -= ((Panel)Puzzle[0]).Height / 10;

            NombreToursTimer++;
            if (TimerArret())
                timerVertical.Enabled = false;
            TestTimer();
        }

        private void timerHorizontal_Tick(object sender, EventArgs e)
        {
            if (x < int.Parse(carreVide.Substring(0,1)))
                PanelcliquE.Left += ((Panel)Puzzle[0]).Width / 10;
            else
                PanelcliquE.Left -= ((Panel)Puzzle[0]).Width / 10;

            NombreToursTimer++;
            if (TimerArret())
                timerHorizontal.Enabled = false;
            TestTimer();
        }
        void TestTimer()
        {
            if (EstRange())
            {
                for (int l = 0; l < 15; l++)
                    Puzzle[l].Visible = false;

                //System.Media.SoundPlayer lecteur = new System.Media.SoundPlayer("door_4.wav");
                //lecteur.Play();
                lePanelPrincipal.BackgroundImageLayout = ImageLayout.Stretch;
                lePanelPrincipal.BackgroundImage = ImageOrigine;

            }
        }

        bool TimerArret()
        {
            if (NombreToursTimer == 4)
            {
                System.Media.SoundPlayer lecteur = new System.Media.SoundPlayer("SURVOL.wav");
                lecteur.Play();
                return false;
            }
            else if (NombreToursTimer == 10)
            {
                PanelcliquE.Top =(int.Parse(carreVide.Substring(1))-1) * (lePanelPrincipal.Height / 4);
                PanelcliquE.Left = (int.Parse(carreVide.Substring(0,1))-1) * (lePanelPrincipal.Width / 4);
                PanelcliquE.Tag = carreVide;
                NombreToursTimer = 0;
                carreVide = carreVideTemp;
                return true; 
            }
            else
                return false;
        }

        private void quitterToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void nouveauToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ChargerForm();
        }

        private void imageToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (FormOuvert==null)
            {
                ImageForm nouveau = new ImageForm(this);
                nouveau.Show();
                
            }
            imageToolStripMenuItem.Enabled = false;
        }

        private void timerImage_Tick(object sender, EventArgs e)
        {
            if (!increment)
            {
                FormOuvert.Opacity-=0.04;
                if (FormOuvert.Opacity == 0)
                {
                    FormOuvert.panelImage.BackgroundImage = ImageOrigine;
                    increment = true;
                    System.Media.SoundPlayer lecteur = new System.Media.SoundPlayer("sbodrop.wav");
                    lecteur.Play();
                }
            }
            else
            {
                FormOuvert.Opacity += 0.02;
                if (FormOuvert.Opacity == 1)
                {
                    increment = false;
                    timerImage.Enabled = false;
                } 
            }
        }
    }
}

Conclusion :


Le reste du code se trouve dans le fichier zippé ci-joint

Codes Sources

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.

Du même auteur (jrscofield)