Пример #1
0
        private void UpdateCounters(PaintEventArgs e, int number, int x, int y)
        {
            bool   negative = false;
            string text;

            if (number < 0)
            {
                number  *= -1;
                negative = true;
                text     = number.ToString();
                if (text.Length >= 3)
                {
                    text = (number % 100).ToString();
                }
            }
            else
            {
                text = number.ToString();
                if (number > 999)
                {
                    text = "999";
                }
            }

            if (text.Length == 1)
            {
                text = "00" + text;
            }
            else if (text.Length == 2)
            {
                text = "0" + text;
            }

            int width, height;

            for (int i = 0; i < 3; i++)
            {
                AssetController.DrawAsset(e, negative && i == 0 ? "counter-dash" : "counter-" + text[i].ToString(), x, y, out width, out height);
                x += width + 2;
            }
        }
Пример #2
0
        private void Game_Paint(object sender, PaintEventArgs e)
        {
            /**
             * piece together interface
             */

            int   drawX = 0, drawY = 0;
            int   assetWidth, assetHeight;
            int   boardWidth, boardHeight;
            Image component;
            Tile  result;

            /**
             * Draw top left portion of the interface
             */

            AssetController.DrawAsset(e, "top_left", drawX, drawY, out assetWidth, out assetHeight);
            drawX += assetWidth;

            /**
             * Draw middle portion of the interface
             */

            for (int i = 0; i < GameWidth; i++)
            {
                AssetController.DrawAsset(e, "top_middle", drawX, drawY, out assetWidth, out assetHeight);
                if (assetWidth > -1 && assetHeight > -1)
                {
                    drawX += assetWidth;
                }
                else
                {
                    throw new Exception("image not loaded.");
                }
            }

            /**
             * Draw top right corner of the interface.
             */

            AssetController.DrawAsset(e, "top_right", drawX, drawY, out assetWidth, out assetHeight);
            drawY += assetHeight;

            //the width boundary of the game
            boardWidth = drawX + assetWidth;

            /**
             * Draw right edge of the interface.
             */
            for (int i = 0; i < GameHeight; i++)
            {
                AssetController.DrawAsset(e, "right_side", drawX, drawY, out assetWidth, out assetHeight);
                if (assetWidth > -1 && assetHeight > -1)
                {
                    drawY += assetHeight;
                }
                else
                {
                    throw new Exception("image not loaded.");
                }
            }


            /**
             * Draw bottom right corner of the interface.
             */
            AssetController.DrawAsset(e, "bottom_right_corner", drawX, drawY, out assetWidth, out assetHeight);

            //the height boundary of the game
            boardHeight = drawY + assetHeight;

            drawX -= assetWidth + 6;

            /**
             * Draw bottom edge of the interface
             */
            for (int i = 0; i < GameWidth; i++)
            {
                AssetController.DrawAsset(e, "bottom", drawX, drawY, out assetWidth, out assetHeight);
                if (assetWidth > -1 && assetHeight > -1)
                {
                    drawX -= assetWidth;
                }
                else
                {
                    throw new Exception("image not loaded.");
                }
            }


            /**
             * Draw bottom left corner of the interface
             */
            component = AssetController.GetAsset("bottom_left_corner");

            drawX += component.Width - 5;
            e.Graphics.DrawImage(component, drawX, drawY);
            drawY -= component.Height + 6;

            /**
             * Draw left edge of the interface.
             */
            for (int i = 0; i < GameHeight; i++)
            {
                AssetController.DrawAsset(e, "left_side", drawX, drawY, out assetWidth, out assetHeight);
                if (assetWidth > -1 && assetHeight > -1)
                {
                    drawY -= assetHeight;
                }
                else
                {
                    throw new Exception("image not loaded.");
                }
            }

            /**
             * Draw bomb counter.
             */
            e.Graphics.FillRectangle(Brushes.Black, new Rectangle(drawX = 14, drawY = 14, 39, 23));
            UpdateCounters(e, mineCount - flaggedMines, drawX + 1, drawY + 1);

            /**
             * Draw face button
             */

            string componentName = "face_";

            switch (faceStatus)
            {
            case Face.Idle:
                componentName += "idle";
                break;

            case Face.Ooh:
                componentName += "ooh";
                break;

            case Face.Down:
                componentName += "down";
                break;

            case Face.Dead:
                componentName += "dead";
                break;

            case Face.Win:
                componentName += "win";
                break;
            }
            component = AssetController.GetAsset(componentName);
            e.Graphics.DrawImage(component, (faceX = ((boardWidth / 2) - (component.Size.Width / 2))) - 1, (faceY = (drawY - 1)));

            /**
             * Draw duration counter.
             */
            e.Graphics.FillRectangle(Brushes.Black, new Rectangle(drawX = boardWidth - 16 - 39, drawY, 39, 23));
            UpdateCounters(e, duration, drawX + 1, drawY + 1);

            /**
             * Draw gridlines
             */

            for (int y = 0; y < GameHeight; ++y)
            {
                e.Graphics.DrawLine(gridPen, PlayAreaPadding, PlayAreaPaddingTop + (y * TileSize), boardWidth - 11, PlayAreaPaddingTop + (y * TileSize));
            }

            for (int x = 0; x < GameWidth; ++x)
            {
                e.Graphics.DrawLine(gridPen, PlayAreaPadding + (x * TileSize), PlayAreaPaddingTop, PlayAreaPadding + (x * TileSize), boardHeight - 11);
            }

            /**
             * Draw buttons
             */

            tilesDrawn = 0;
            drawX      = PlayAreaPadding;
            for (int col = 0; col < GameWidth; col++)
            {
                drawY = PlayAreaPaddingTop;
                for (int row = 0; row < GameHeight; row++)
                {
                    result = Board[col, row];

                    /*	if (result.Mine)
                     *      {
                     *              AssetController.DrawAsset(e, "question", drawX, drawY);
                     *              tilesDrawn++;
                     *              drawY += TileSize;
                     *              continue;
                     *      }*/
                    if (result.Revealed)
                    {
                        if (result.Mine)
                        {
                            AssetController.DrawAsset(e, result.Hit ? "mine-hit" : "mine", drawX + 1, drawY + 1);
                        }
                        else
                        {
                            AssetController.DrawAsset(e, result.AdjacentMines.ToString(), drawX + 1, drawY + 1);
                        }
                    }
                    else if (result.Selected)
                    {
                        AssetController.DrawAsset(e, result.Tentative ? "question-down" : "0", drawX + 1, drawY + 1);
                    }
                    else if (IsGameOver() && result.Flagged && !result.Tentative && !result.Mine)
                    {
                        AssetController.DrawAsset(e, "no-mine", drawX + 1, drawY + 1);
                    }
                    else
                    {
                        AssetController.DrawAsset(e, result.Flagged ? result.Tentative ? "question" : "flag" : "button", drawX, drawY);
                        tilesDrawn++;
                    }
                    drawY += TileSize;
                }
                drawX += TileSize;
            }

            if (!win && tilesDrawn == Mines)
            {
                win        = true;
                faceStatus = Face.Win;
                this.Invalidate();
            }
        }