예제 #1
0
        private Snakebody Getfood()//获得食物
        {
            Snakebody food = null;
            Random    r    = new Random();
            bool      redo = false;

            while (true)
            {
                redo = false;
                int x = r.Next(this.width);
                int y = r.Next(this.height);
                for (int i = 0; i < this.body.Count; i++)//检查食物所在坐标是否和贪吃蛇冲突
                {
                    Snakebody s = (Snakebody)(this.body[i]);
                    if (s.Point.X == x && s.Point.Y == y)    //有冲突时,在随机找一个坐标
                    {
                        redo = true;
                    }
                }
                if (redo == false)
                {
                    food = new Snakebody(Color.Black, this.size, new Point(x, y));
                    break;
                }
            }
            return(food);
        }
예제 #2
0
        }//设定类属性值,初始化蛇块列表ArrayList

        public void Start()        //开始
        {
            this.food = Getfood(); //生成一个食物
            //初始化计时器
            timerblock           = new System.Timers.Timer();
            timerblock.Elapsed  += new System.Timers.ElapsedEventHandler(OnTimedEvent); //注册计时器的事件
            timerblock.AutoReset = true;                                                //设置是执行一次(false)还是一直执行(true),默认为true
            timerblock.Interval  = 500;
            timerblock.Start();
        }
예제 #3
0
        private bool CheckDead()                        //检查游戏是否结束
        {
            Snakebody head = (Snakebody)(this.body[0]); //取蛇块列表的第一个当作蛇头

            //检查是否超出画布范围
            if (head.Point.X < 0 || head.Point.Y < 0 || head.Point.X >= this.width || head.Point.Y >= this.height)
            {
                return(true);
            }
            for (int i = 1; i < this.body.Count; i++)//检查是否撞上自己
            {
                Snakebody s = (Snakebody)this.body[i];
                if (head.Point.X == s.Point.X && head.Point.Y == s.Point.Y)
                {
                    return(true);
                }
            }
            return(false);
        }
예제 #4
0
        private void Move() //贪吃蛇移动
        {
            Point     p;    //下一个坐标位置
            Snakebody head = (Snakebody)(this.body[0]);

            if (this.dir == Direction.Up)
            {
                p = new Point(head.Point.X, head.Point.Y - 1);
            }
            else if (this.dir == Direction.Down)
            {
                p = new Point(head.Point.X, head.Point.Y + 1);
            }
            else if (this.dir == Direction.Left)
            {
                p = new Point(head.Point.X - 1, head.Point.Y);
            }
            else
            {
                p = new Point(head.Point.X + 1, head.Point.Y);
            }

            //生成新坐标,成为蛇头
            Snakebody s = new Snakebody(Color.Red, this.size, p);

            //如果下一个坐标不是当前食物坐标,那么删除最后一节蛇
            if (this.food.Point != p)
            {
                this.body.RemoveAt(this.body.Count - 1);
            }

            //如果下一个坐标和食物坐标重合,那么就生成一个新食物,即“吃到食物”
            else
            {
                this.food = this.Getfood();
            }

            //把下一个坐标插入到蛇块列表的第一个,使其成为蛇头
            this.body.Insert(0, s);
            this.Paint(this.Palette);//更新画板
        }