Beispiel #1
0
        // spustenie hry, nastavia sa vsetky potrebne nastavenia a zobrazi sa hracia
        // plocha s puzzle kuskami a oknom s instrukciami(len pri prvom spusteni programu)
        private void button2_Click(object sender, EventArgs e)
        {
            if (gameData.SourcePicture != null)
            {
                textBox1.Visible    = false;
                pictureBox1.Visible = false;
                button1.Visible     = false;
                button2.Visible     = false;
                panel5.Visible      = true; //obsahuje button3==Restart, button4==NewGame

                //
                //nastavime pociatocne data pre hru
                //
                gameData.SourcePicture          = PictureEditor.CropImage(gameData.SourcePicture, gridLayer.StartCutLocation, gridLayer.EndCutLocation);
                gameData.PiecesGridDimensions   = gridLayer.GridDimensions;
                gameData.PiecesCount            = gameData.PiecesGridDimensions.Width * gameData.PiecesGridDimensions.Height;
                gameData.PieceDimensions        = gridLayer.PieceDimensions;
                gameData.PieceSurroundingSize   = (int)Math.Ceiling(gameData.PieceDimensions.Width * 0.163);
                gameData.GameBoard              = this.panel4;
                gameData.GameBoardStartPosition = new Point(50, 50);

                PuzzleGameUtilities.CreatePieces(gameData);
                PuzzleGameUtilities.SetOriginalPiecesLocations(gameData);
                PuzzleGameUtilities.SetPiecesArrangement(gameData);
                PuzzleGameUtilities.SetPiecesImages(gameData);
                PuzzleGameUtilities.SetPiecesOriginalNeighbours(gameData);
                PuzzleGameUtilities.RandomizePiecesLocations(gameData);


                //
                //Pociatocne nastavenia gameboard-u
                //
                gameboard = new Gameboard(this, gameData, panel4.Size);
                //gameboard ako instancia Form nesmie byt nastavena na top level control !!
                gameboard.TopLevel = false;
                panel4.Controls.Add(gameboard);
                gameboard.Visible  = false;
                gameboard.Anchor   = AnchorStyles.None;
                gameboard.Anchor   = (AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Right);
                gameboard.Location = new Point(0, 0); //pozicia vo vnutri panel4
                gameboard.Visible  = true;

                if (showGameInstructions)
                {
                    this.panel6.BringToFront();
                    this.panel6.Visible = true;
                    // zabezpecime aby sa to ukazalo iba pri prvom spusteni
                    showGameInstructions = false;
                }
            }
            else
            {
                noPictureLabel.Visible = true;
                // MessageBox.Show("Nebol vybraný obrazok!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        // vystrihne jednotlive kusku zo zdrojoveho obrazka za pomoci metody CreatePuzzleShapePath
        private static Bitmap[] CreatePuzzleShapeImagesFromSourceImage(PuzzleGameData data)
        {
            Bitmap[] pShapes = new Bitmap[data.PiecesCount];

            //Pre zjednodesenie vyrezavania pridame hranu hrubky SurroundingSize okolo zdrojoveho obrazka,
            //sourceImage == obrazok + hrany dookola
            int hrubkaHrany = data.PieceSurroundingSize;
            int gridWidth   = data.PiecesGridDimensions.Width;
            int gridheight  = data.PiecesGridDimensions.Height;
            int pieceWidth  = data.PieceDimensions.Width;
            int pieceHeight = data.PieceDimensions.Height;

            Bitmap sourceImage = new Bitmap(data.SourcePicture.Width + 2 * hrubkaHrany,
                                            data.SourcePicture.Height + 2 * hrubkaHrany);

            using (Graphics draw = Graphics.FromImage(sourceImage))
            {
                draw.DrawImage(data.SourcePicture, new Point(hrubkaHrany, hrubkaHrany));
            }

            //Vyrezavanie jednotlivych kuskov

            int x = 0, y = 0;
            //Point startP = new Point(x, y);
            //Point endP = new Point((x + pieceWidth + 2 * hrubkaHrany),
            //                       (y + pieceHeight + 2 * hrubkaHrany));
            GraphicsPath shapePath;

            int index;

            for (int j = 0; j < gridheight; j++)
            {
                for (int i = 0; i < gridWidth; i++)
                {
                    index          = j * gridWidth + i;
                    pShapes[index] = PictureEditor.CropImage(sourceImage,
                                                             new Point(x, y),
                                                             new Point((x + pieceWidth + 2 * hrubkaHrany),
                                                                       (y + pieceHeight + 2 * hrubkaHrany)));

                    shapePath      = CreatePuzzleShapePath(hrubkaHrany, data.PieceDimensions, data.Pieces[index].Arrangement);
                    pShapes[index] = PieceCutter.CutOut(pShapes[index], shapePath);

                    x += pieceWidth;
                }
                x  = 0;
                y += pieceHeight;
            }

            return(pShapes);
        }