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

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            PaintBlocks(e.Graphics);
        }

        private void PaintBlocks(Graphics g)
        {
            for (int y = ZERO; y < _height; y++)
            {
                for (int x = ZERO; x < _width; x++)
                {
                    _blocks[y, x].Paint(null, g);
                }
            }
        }

        private void GameFrame_MouseClick(object sender, MouseEventArgs e)
        {
            if (GameState == GameState.Ready)
            {
                GameStart();
            }
            else
            {
                if (GameState != GameState.Started)
                {
                    return;
                }
            }

            int x = e.X / Block.Width;
            int y = e.Y / Block.Width;
            Block block = _blocks[y, x];

            if (block.Bounds.Contains(e.Location))
            {
                if (e.Button == MouseButtons.Right)
                {
                    if (block.State != Block.BlockState.Open)
                    {
                        block.FlagBlock();
                        if (block.State == Block.BlockState.Flag)
                        {
                            _flagNum++;
                            if (block.Bomb)
                            {
                                RightFlagNum++;
                            }
                        }
                        else
                        {
                            _flagNum--;
                            if (block.Bomb)
                            {
                                RightFlagNum--;
                            }
                        }
                    }
                }

                if (e.Button == MouseButtons.Left)
                {
                    if (block.State == Block.BlockState.Close)
                    {
                        OpenBlock(x, y);
                    }
                }
            }

            if (OnGamingClick != null)
            {
                OnGamingClick(this, new EventArgs());
            }
        }

        private void OpenBlock(int x, int y)
        {
            if (x < ZERO || y < ZERO || x >= _width || y >= _height)
            {
                return;
            }
           
            Block block = _blocks[y, x];

            if (block.State != Block.BlockState.Close)
            {
                return;
            }

            block.OpenBlock();

            if (block.Bomb)
            {
                GameEnd();
                return;
            }
            OpenedBlockNum++;
            if (block.Num == ZERO)
            {
                OpenBlock(x - 1, y - 1);
                OpenBlock(x, y - 1);
                OpenBlock(x + 1, y - 1);
                OpenBlock(x - 1, y);
                OpenBlock(x + 1, y);
                OpenBlock(x - 1, y + 1);
                OpenBlock(x, y + 1);
                OpenBlock(x + 1, y + 1);
            }
        }

        public void Showbomb()
        {
            for (int y = ZERO; y < _height; y++)
            {
                for (int x = ZERO; x < _width; x++)
                {
                    Block block = _blocks[y, x];
                    if (block.Bomb && block.State!=Block.BlockState.Flag)
                    {
                        block.OpenBlock();
                    }
                }
            }
            return;
        }

        private bool _clickDown = false;
        private Point _lastLocation = new Point();
        private void GameFrame_MouseDown(object sender, MouseEventArgs e)
        {
            if (GameState != GameState.Ready && GameState != GameState.Started)
            {
                return;
            }
            _clickDown = true;
            int x = e.X / Block.Width;
            int y = e.Y / Block.Width;
            if (x < ZERO || y < ZERO || x >= _width || y >= _height)
            {
                return;
            }
            _lastLocation = new Point(x, y);
            Block block = _blocks[y, x];
            block.OnBlockClickDown();
        }

        private void GameFrame_MouseMove(object sender, MouseEventArgs e)
        {
            if (GameState != GameState.Ready && GameState != GameState.Started)
            {
                return;
            }
            if (_clickDown)
            {
                int x = e.X / Block.Width;
                int y = e.Y / Block.Width;               
               
                if (!_lastLocation.Equals(e.Location))
                {
                    Block b = _blocks[_lastLocation.Y, _lastLocation.X];
                    b.OnBlockClickUp();
                    if (e.X < ZERO || e.Y < ZERO || x < ZERO || y < ZERO || x >= _width || y >= _height)
                    {
                        return;
                    }
                    Block block = _blocks[y, x];
                    _lastLocation = new Point(x, y);
                    block.OnBlockClickDown();
                }
            }
        }

        private void GameFrame_MouseUp(object sender, MouseEventArgs e)
        {
            if (GameState != GameState.Ready && GameState != GameState.Started)
            {
                return;
            }
            _clickDown = false;
        }
    }

终于完了


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

     

©2009 Baidu