日期:2014-05-17  浏览次数:20867 次

我在网上找的贪吃蛇案例,为什么我在创建事例b是说P是变量无法使用,我改如何改进
 private void Move()
        {
            Point p;
            Block head = (Block)(this._blocks[0]);
            if (this._direction == Direction.Up)
                p = new Point(head.Point.X, head.Point.Y - 1);
            else if (this._direction == Direction.Down)
                p = new Point(head.Point.X, head.Point.Y + 1);
            else if (this._direction == Direction.Left)
                p = new Point(head.Point.X - 1, head.Point.Y);
            else if (this._direction == Direction.Right)
                p = new Point(head.Point.X + 1, head.Point.Y);
            Block b = new Block(Color.Red, this._size, p);//mmmmmmmmm
            if (this._food.Point != p)
                this._blocks.RemoveAt(this._blocks.Count - 1);
            else
                this._food = this.GetFood();
            this._blocks.Insert(0, b);
            this.PaintPalette(this._gpPalette);

        }

------解决方案--------------------
try

Point p = Point.Empty;

------解决方案--------------------
p定义时要初始化一个值
如:
Point p = new Point(0,0);

Point p = Point.Empty;
------解决方案--------------------
我的意思是
else if (this._direction == Direction.Right)
p = new Point(head.Point.X + 1, head.Point.Y);
这个改成
else
p = new Point(head.Point.X + 1, head.Point.Y);
怎么能没有呢?
------解决方案--------------------
引用:
我的意思是
else if (this._direction == Direction.Right)
p = new Point(head.Point.X + 1, head.Point.Y);
这个改成
else
p = new Point(head.Point.X + 1, head.Point.Y);
怎么能没有呢?

这个应该是正确的,
话说这段代码很像是我很久之前传的一个贪吃蛇例子呢。
是不是从这个地址下的?
http://download.csdn.net/detail/aley/969799