百度空间 | 百度首页 
 
查看文章
 
C# 扫雷 面向对象程序设计
2008-08-17 14:53

最近一直在忙CMMI3级评估,比较无聊 写了个扫雷玩玩

程序主要是一个Block类和一个GameFrame类 界面窗体只需要做初始化和响应事件就可以了 还没有做双键按下的效果 比较原始

class Block
    {
        private const int WIDTH_DEF = 16;
        private Color[] _numColors = new Color[] {Color.Blue,Color.Green,Color.DarkRed,Color.DeepSkyBlue,Color.DimGray,Color.DarkOrange,Color.Brown,Color.Black };

        private Color _defColor = Color.White;
        private Color _defBackColor = Color.FromArgb(0xC0, 0xC0, 0xC0);

        private static Image _btImg = null;

        public static Image BtImg
        {
            get { return Block._btImg; }
            set { Block._btImg = value; }
        }

        private static Image _bombImg = null;

        public static Image BombImg
        {
            get { return Block._bombImg; }
            set { Block._bombImg = value; }
        }

        private static Image _flagImg = null;

        public static Image FlagImg
        {
            get { return Block._flagImg; }
            set { Block._flagImg = value; }
        }

        private static Image _backGroundImg = null;

        public static Image BackGroundImg
        {
            get { return Block._backGroundImg; }
            set { Block._backGroundImg = value; }
        }

        public enum BlockState
        {
            Close,
            Open,
            Flag
        };

        private BlockState _state = BlockState.Close;

        public BlockState State
        {
            get { return _state; }
            set { _state = value; }
        }

        private Rectangle _rct;

        public Rectangle Bounds
        {
            get { return _rct; }
            set { _rct = value; }
        }

        private bool _bomb = false;

        public bool Bomb
        {
            get { return _bomb; }
            set { _bomb = value; }
        }
       
        private int _num = 0;

        public int Num
        {
            get { return _num; }
            set
            {
                if (!_bomb)
                { _num = value; }
            }
        }

        private static int _width = WIDTH_DEF;

        public static int Width
        {
            get { return _width; }
            set { _width = value; }
        }

        //public Block() { }
        private static GameFrame _owner = null;

        public static GameFrame Owner
        {
            get { return Block._owner; }
            set { Block._owner = value; }
        }

        public Block(int x,int y,bool bomb)
        {
            _rct.X = x;
            _rct.Y = y;
            _rct.Width = _width;
            _rct.Height = _width;
            _bomb = bomb;
        }

        public Block(int x, int y, int num)
        {
            _rct.X = x;
            _rct.Y = y;
            _rct.Width = _width;
            _rct.Height = _width;
            _bomb = false;
            _num = num;
        }

        public void Paint(object sender, Graphics g)
        {
            g.DrawImage(_backGroundImg, this.Bounds.Location);
            switch (_state)
            {
                case BlockState.Close:
                    g.DrawImage(_btImg, this.Bounds.Location);
                    break;
                case BlockState.Flag:
                    g.DrawImage(_flagImg, this.Bounds.Location);
                    break;
                case BlockState.Open:
                    switch(_bomb)
                    {
                        case false:
                            if (_num != 0)
                            {
                                Point p=this.Bounds.Location;
                                p.X+=1;
                                g.DrawString(_num.ToString(), new Font("宋体", 12, FontStyle.Bold), new SolidBrush(_numColors[Num - 1]), p);
                            }
                            else
                            {
                                g.DrawImage(_backGroundImg, this.Bounds.Location);
                            }
                            break;
                        case true:
                            g.DrawImage(_bombImg, this.Bounds.Location);
                            break;
                    }
                    break;
            }           
        }

        public void OnBlockClickDown()
        {
            Graphics g = Block.Owner.CreateGraphics();
            if (State == BlockState.Close)
            {
                g.DrawImage(_backGroundImg, this.Bounds.Location);
            }
        }

        public void OnBlockClickUp()
        {
            Graphics g = Block.Owner.CreateGraphics();
            if (State == BlockState.Close)
            {
                g.DrawImage(_btImg, this.Bounds.Location);
            }
        }

        public void Refresh()
        {
            Graphics g = Block.Owner.CreateGraphics();
            Paint(null, g);
        }

        public void OpenBlock()
        {
            this.State = BlockState.Open;
            this.Refresh();
        }

        public void FlagBlock()
        {
            this.State = this.State == BlockState.Flag ? BlockState.Close : BlockState.Flag;
            this.Refresh();
        }
    }

放不下了...


类别:Public | 添加到搜藏 | 浏览() | 评论 (0)
 
最近读者:
 
网友评论:
发表评论:
姓 名:
网址或邮箱: (选填)
内 容:
验证码: 请点击后输入四位验证码,字母不区分大小写
      

     

©2009 Baidu