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(); }
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(); } }
/// <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; }
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; }
/// <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; }
/// <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; }
private void btnIniSnake_Click(object sender, EventArgs e) { _snake = SnakeHelper.GenSnake(ConfigHelper.Speed, ConfigHelper.SnakeColor); _snake = MapHelper.DrawSnakeOnMap(palMap, _map, _snake); _direction = _snake.Direction; }