예제 #1
0
        /// <summary>
        /// 创建地图
        /// </summary>
        /// <param name="rowCount"></param>
        /// <param name="colCount"></param>
        /// <param name="boxWidth"></param>
        /// <param name="boxHeight"></param>
        /// <param name="line"></param>
        /// <param name="box"></param>
        /// <returns></returns>
        public static ModelMap GenMap(int rowCount, int colCount, int boxWidth, int boxHeight, Color line, Color box)
        {
            var map = new ModelMap
                {
                    Row = rowCount,
                    Column = colCount,
                    Box = new ModelBox
                        {
                            Width = boxWidth,
                            Height = boxHeight
                        },
                    Line =  line,
                    Color = box,
                    Body = new List<ModelElement>()
                };
            #region 初始化地图实体
            for (int ri = 0; ri < rowCount; ri++)
            {
                for (int ci = 0; ci < colCount; ci++)
                {
                    map.Body.Add(new ModelElement
                        {
                            Abscissa = ri,
                            Ordinate = ci
                        });
                }
            }
            #endregion

            return map;
        }
예제 #2
0
        /// <summary>
        /// 地图描边
        /// </summary>
        /// <param name="panel"></param>
        /// <param name="map"></param>
        public static void DrawMap(Panel panel, ModelMap map)
        {
            #region 勾画地图
            var g = panel.CreateGraphics();
            #region 画横线
            for (int ri = 0; ri <= map.Row; ri++)
            {
                g.DrawLine(new Pen(Color.Black), 0, ri * map.Box.Height, map.Column * map.Box.Width, ri * map.Box.Height);
            }

            #endregion
            #region 画竖线
            for (int ci = 0; ci <= map.Column; ci++)
            {
                g.DrawLine(new Pen(Color.Black), ci * map.Box.Width, 0, ci * map.Box.Height, map.Row * map.Box.Width);
            }
            #endregion
            #region 勾画方块
            foreach (var b in map.Body)
            {
                DrawMapBox(panel, map.Color, b.Abscissa, b.Ordinate, map.Box.Width, map.Box.Height);
            }
            #endregion
            #endregion
        }
예제 #3
0
 private void tmSpeed_Tick(object sender, EventArgs e)
 {
     tmSpeed.Stop();
     tmSpeed.Interval = _snake.Speed;
     _snake = MapHelper.MoveSnakeOnMap(palMap, _map, _snake, _direction, _back,out _dead);
     if (tmSpeed.Tag.ToString().Equals("0")) _map = MapHelper.ShowBonus(palMap,_map,_snake,ConfigHelper.BonusColor);
     else if (tmSpeed.Tag.ToString().Equals((ConfigHelper.BonusShow - ConfigHelper.BonusHide).ToString(CultureInfo.InvariantCulture)) && !_bonus) _map = MapHelper.HideBonus(palMap, _map);
     tmSpeed.Tag = tmSpeed.Tag.ToString().Equals("0") ? ConfigHelper.BonusShow.ToString(CultureInfo.InvariantCulture) : (int.Parse(tmSpeed.Tag.ToString()) - 1).ToString(CultureInfo.InvariantCulture);
     if (_dead) MessageBox.Show(@"Game Over");
     else tmSpeed.Start();
 }
예제 #4
0
 private void tmSpeed_Tick(object sender, EventArgs e)
 {
     tmSpeed.Stop();
     tmSpeed.Interval = _snake.Speed;
     _snake           = MapHelper.MoveSnakeOnMap(palMap, _map, _snake, _direction, _back, out _dead);
     if (tmSpeed.Tag.ToString().Equals("0"))
     {
         _map = MapHelper.ShowBonus(palMap, _map, _snake, ConfigHelper.BonusColor);
     }
     else if (tmSpeed.Tag.ToString().Equals((ConfigHelper.BonusShow - ConfigHelper.BonusHide).ToString(CultureInfo.InvariantCulture)) && !_bonus)
     {
         _map = MapHelper.HideBonus(palMap, _map);
     }
     tmSpeed.Tag = tmSpeed.Tag.ToString().Equals("0") ? ConfigHelper.BonusShow.ToString(CultureInfo.InvariantCulture) : (int.Parse(tmSpeed.Tag.ToString()) - 1).ToString(CultureInfo.InvariantCulture);
     if (_dead)
     {
         MessageBox.Show(@"Game Over");
     }
     else
     {
         tmSpeed.Start();
     }
 }
예제 #5
0
        /// <summary>
        /// 蛇身在移动图上移动
        /// </summary>
        /// <param name="panel"></param>
        /// <param name="map"></param>
        /// <param name="snake"></param>
        /// <param name="direction"></param>
        /// <param name="enableBack"></param>
        /// <param name="dead"></param>
        /// <returns></returns>
        public static ModelSnake MoveSnakeOnMap(Panel panel, ModelMap map, ModelSnake snake, ModelEnum.Direction direction, bool enableBack, out bool dead)
        {
            dead = false;
            if (!enableBack)
            {
                if (direction.Equals(ModelEnum.Direction.Up) && snake.Direction.Equals(ModelEnum.Direction.Down))
                    direction = snake.Direction;
                else if (direction.Equals(ModelEnum.Direction.Down) && snake.Direction.Equals(ModelEnum.Direction.Up))
                    direction = snake.Direction;
                else if (direction.Equals(ModelEnum.Direction.Left) && snake.Direction.Equals(ModelEnum.Direction.Right))
                    direction = snake.Direction;
                else if (direction.Equals(ModelEnum.Direction.Right) &&
                         snake.Direction.Equals(ModelEnum.Direction.Left))
                    direction = snake.Direction;
            }
            else
            {
                if (direction.Equals(ModelEnum.Direction.Up) && snake.Direction.Equals(ModelEnum.Direction.Down))
                    snake.Body.Reverse();
                else if (direction.Equals(ModelEnum.Direction.Down) && snake.Direction.Equals(ModelEnum.Direction.Up))
                    snake.Body.Reverse();
                else if (direction.Equals(ModelEnum.Direction.Left) && snake.Direction.Equals(ModelEnum.Direction.Right))
                    snake.Body.Reverse();
                else if (direction.Equals(ModelEnum.Direction.Right) &&
                         snake.Direction.Equals(ModelEnum.Direction.Left))
                    snake.Body.Reverse();
            }

            var head = new ModelElement
                {
                    Abscissa = snake.Body[0].Abscissa,
                    Ordinate = snake.Body[0].Ordinate
                };
            switch (direction)
            {
                case ModelEnum.Direction.Left:
                    head.Abscissa--;
                    break;
                case ModelEnum.Direction.Right:
                    head.Abscissa++;
                    break;
                case ModelEnum.Direction.Up:
                    head.Ordinate--;
                    break;
                case ModelEnum.Direction.Down:
                    head.Ordinate++;
                    break;
            }
            if (head.Abscissa < 0) head.Abscissa = map.Column - 1;
            else if (head.Abscissa == map.Column) head.Abscissa = 0;
            if (head.Ordinate < 0) head.Ordinate = map.Row - 1;
            else if (head.Ordinate == map.Row) head.Ordinate = 0;

            var d = snake.Body.SingleOrDefault(t => t.Abscissa == head.Abscissa && t.Ordinate == head.Ordinate);
            if (d != null)
            {
                dead = true;
            }

            var tail = snake.Body[snake.Body.Count - 1];
            var m = map.Body.SingleOrDefault(t => t.Bonus && t.Abscissa == tail.Abscissa && t.Ordinate == tail.Ordinate);
            if (m == null)
            {
                DrawMapBox(panel, map.Color, tail.Abscissa, tail.Ordinate, map.Box.Width, map.Box.Height);
                snake.Body.Remove(tail);
            }
            else
            {
                DrawMapBox(panel, snake.Color, head.Abscissa, head.Ordinate, map.Box.Width, map.Box.Height);
                m.Bonus = false;
            }

            DrawMapBox(panel, snake.Color, head.Abscissa, head.Ordinate, map.Box.Width, map.Box.Height);
            snake.Body.Insert(0, head);
            snake.Direction = direction;

            return snake;
        }
예제 #6
0
 public static ModelMap HideBonus(Panel panel, ModelMap map)
 {
     var ms = map.Body.Where(t => t.Bonus);
     foreach (var m in ms)
     {
         DrawMapBox(panel, map.Color, m.Abscissa, m.Ordinate, map.Box.Width, map.Box.Height);
         m.Bonus = false;
     }
     return map;
 }
예제 #7
0
 public static ModelMap ShowBonus(Panel panel, ModelMap map, ModelSnake snake, Color color)
 {
     var b = NewBonus(map.Row - 1, map.Column - 1);
     while (snake.Body.Count(s => s.Equals(b)) > 0)
     {
         b = NewBonus(map.Row - 1, map.Column - 1);
     }
     var m = map.Body.SingleOrDefault(t => t.Abscissa == b.Abscissa && t.Ordinate == b.Ordinate);
     if (m != null)
     {
         DrawMapBox(panel, color, m.Abscissa, m.Ordinate, map.Box.Width, map.Box.Height);
         m.Bonus = true;
     }
     return map;
 }
예제 #8
0
 /// <summary>
 /// 蛇身描绘至地图
 /// </summary>
 /// <param name="panel"></param>
 /// <param name="map"></param>
 /// <param name="snake"></param>
 /// <returns></returns>
 public static ModelSnake DrawSnakeOnMap(Panel panel, ModelMap map,ModelSnake snake)
 {
     snake = GenSnakeOnMap(map, snake);
     foreach (var b in snake.Body)
     {
         DrawMapBox(panel, snake.Color, b.Abscissa, b.Ordinate, map.Box.Width, map.Box.Height);
     }
     return snake;
 }
예제 #9
0
 /// <summary>
 /// 在地图上初始化蛇身
 /// </summary>
 /// <param name="map"></param>
 /// <param name="snake"></param>
 /// <returns></returns>
 public static ModelSnake GenSnakeOnMap(ModelMap map,ModelSnake snake)
 {
     var sk = snake;
     var centerX = map.Row/2;
     var centerY = map.Column/2;
     sk.Body = new List<ModelElement>
         {
             new ModelElement
                 {
                     Abscissa = centerX,
                     Ordinate = centerY
                 },
             new ModelElement
                 {
                     Abscissa = centerX,
                     Ordinate = centerY - 1
                 }
         };
     return sk;
 }
예제 #10
0
 private void btnIniMap_Click(object sender, EventArgs e)
 {
     _map = MapHelper.GenMap(ConfigHelper.RowCount, ConfigHelper.ColCount, ConfigHelper.BoxWidth, ConfigHelper.BoxHeight, ConfigHelper.LineColor, ConfigHelper.MapColor);
     MapHelper.DrawMap(palMap, _map);
 }
예제 #11
0
 private void btnIniMap_Click(object sender, EventArgs e)
 {
     _map =MapHelper.GenMap(ConfigHelper.RowCount, ConfigHelper.ColCount, ConfigHelper.BoxWidth, ConfigHelper.BoxHeight,ConfigHelper.LineColor, ConfigHelper.MapColor);
     MapHelper.DrawMap(palMap, _map);
 }