当前位置: 首页 > news >正文

优化培训方式潮州seo

优化培训方式,潮州seo,2023推广平台,深圳建筑工程交易网贪吃蛇小时候大家都玩过,具体步骤如下: 1.给游戏制造一个有限得空间。 2.生成墙壁,小蛇碰撞到墙壁或者咬到自己的尾巴,游戏结束。 3.生成随机的食物。 4.吃掉食物,增加自身的体长,并生成新的食物。 具体代码如下&…

贪吃蛇小时候大家都玩过,具体步骤如下:
1.给游戏制造一个有限得空间。
2.生成墙壁,小蛇碰撞到墙壁或者咬到自己的尾巴,游戏结束。
3.生成随机的食物。
4.吃掉食物,增加自身的体长,并生成新的食物。

具体代码如下:
定义一个小蛇类,该类包含吃东西,移动,检测碰撞等方法。

class Snake
{private List<Point> _body;private Direction _direction;private int _step = 1;private Point _tail;public Snake(Point tail, int length, Direction direction){_direction = direction;_body = new List<Point>();for (int i = 0; i < length; i++){Point p = new Point(tail.X, tail.Y, tail.Symbol);p.Move(i, _direction);_body.Add(p);}_tail = _body.First();}public void Move(){Point head = GetNextPoint();_body.Add(head);_tail.Clear();_tail = _body.First();_body.RemoveAt(0);head.Draw();}public bool Eat(Point food){Point head = GetNextPoint();if (head.IsHit(food)){food.Symbol = head.Symbol;_body.Add(food);return true;}return false;}public void HandleKey(ConsoleKey key){if (key == ConsoleKey.LeftArrow && _direction != Direction.Right){_direction = Direction.Left;}else if (key == ConsoleKey.RightArrow && _direction != Direction.Left){_direction = Direction.Right;}else if (key == ConsoleKey.UpArrow && _direction != Direction.Down){_direction = Direction.Up;}else if (key == ConsoleKey.DownArrow && _direction != Direction.Up){_direction = Direction.Down;}}public Point GetNextPoint(){Point head = _body.Last();Point nextPoint = new Point(head.X, head.Y, head.Symbol);nextPoint.Move(_step, _direction);return nextPoint;}public void Draw(){foreach (Point p in _body){p.Draw();}}public bool IsHitTail(){Point head = _body.Last();for (int i = 0; i < _body.Count - 2; i++){if (head.IsHit(_body[i])){return true;}}return false;}
}

定义一个墙体类:

class Walls
{private List<Point> _wallList;public Walls(int mapWidth, int mapHeight){_wallList = new List<Point>();DrawHorizontalLine(0);DrawHorizontalLine(mapHeight - 1);DrawVerticalLine(0);DrawVerticalLine(mapWidth - 1);}private void DrawHorizontalLine(int y){for (int x = 0; x < Console.WindowWidth; x++){Point p = new Point(x, y, '#');p.Draw();_wallList.Add(p);}}private void DrawVerticalLine(int x){for (int y = 0; y < Console.WindowHeight; y++){Point p = new Point(x, y, '#');p.Draw();_wallList.Add(p);}}public bool IsHit(Snake snake){Point head = snake.GetNextPoint();return IsHit(head);}private bool IsHit(Point p){return _wallList.Any(w => w.IsHit(p));}public void Draw(){foreach (Point p in _wallList){p.Draw();}}
}

定义一个创建食物类:

class FoodCreator
{private readonly int _mapWidth;private readonly int _mapHeight;private readonly char _symbol;public FoodCreator(int mapWidth, int mapHeight, char symbol){_mapWidth = mapWidth;_mapHeight = mapHeight;_symbol = symbol;}public Point CreateFood(){int x = new Random().Next(2, _mapWidth - 2);int y = new Random().Next(2, _mapHeight - 2);return new Point(x, y, _symbol);}
}

定义一个枚举,表示方向:

enum Direction
{Left,Right,Up,Down
}

定义一个坐标类应用重绘:

class Point
{public int X { get; set; }public int Y { get; set; }public char Symbol { get; set; }public Point(int x, int y, char symbol){X = x;Y = y;Symbol = symbol;}public void Draw(){Console.SetCursorPosition(X, Y);Console.Write(Symbol);}public void Clear(){Symbol = ' ';Draw();}public void Move(int offset, Direction direction){if (direction == Direction.Right){X += offset;}else if (direction == Direction.Left){X -= offset;}else if (direction == Direction.Up){Y -= offset;}else if (direction == Direction.Down){Y += offset;}}public bool IsHit(Point p){return p.X == X && p.Y == Y;}
}

函数入口:

class Program
{static void Main(){Console.Title = "贪吃蛇游戏";Console.CursorVisible = false;Console.SetWindowSize(40, 20);Console.SetBufferSize(40, 20);Walls walls = new Walls(40, 20);walls.Draw();Snake snake = new Snake(new Point(6, 5, '*'),1,Direction.Down);snake.Draw();FoodCreator foodCreator = new FoodCreator(40, 20, '$');Point food = foodCreator.CreateFood();food.Draw();while (true){if (walls.IsHit(snake) || snake.IsHitTail()){break;}if (snake.Eat(food)){food = foodCreator.CreateFood();food.Draw();}else{snake.Move();}System.Threading.Thread.Sleep(100);if (Console.KeyAvailable){ConsoleKeyInfo key = Console.ReadKey();snake.HandleKey(key.Key);}}WriteGameOver();Console.ReadLine();}static void WriteGameOver(){int xOffset = 10;int yOffset = 8;Console.ForegroundColor = ConsoleColor.Red;Console.SetCursorPosition(xOffset, yOffset++);WriteText("========", xOffset, yOffset++);WriteText("GAME OVER", xOffset + 1, yOffset++);yOffset++;WriteText("========", xOffset, yOffset++);}static void WriteText(String text, int xOffset, int yOffset){Console.SetCursorPosition(xOffset, yOffset);Console.WriteLine(text);}
}

运行效果图:
请添加图片描述
请添加图片描述

http://www.yidumall.com/news/77490.html

相关文章:

  • 农业开发公司企业网站建设seo网站推广的主要目的
  • 定制手机网站建设网站seo
  • 免费做威客的网站网络营销的缺点及建议
  • 网站开发过滤器作用品牌网络营销成功案例
  • 建设门户网站的目的和意义百度推广代理商加盟
  • 网站根目录怎么写上海百度公司地址
  • 上海市建设工程备案查询网站网站出租三级域名费用
  • 成都郫县网站建设潍坊网站seo
  • 网站建设长尾关键词营销托管全网营销推广
  • 珠海高端网站建设公司上海官网seo
  • 九星市场做网站开展网络营销的企业
  • 网站聚合页面郑州发布最新通告
  • pc端手机网站 viewport 自适应汕头seo外包平台
  • 自己网上注册公司入口seo文章优化技巧
  • 网站升级 云南省建设注册考试中心营销推广是什么
  • 帮客户做传销网站如何利用网络广告进行推广
  • 个人如何制作一个网站软文推广的100个范例
  • 公司网站建设包含的内容营销网络推广
  • 免费软件app下载安装石家庄seo网站排名
  • 万网网站建设品牌推广工作内容
  • 射阳做网站多少钱怎么做百度推广平台
  • 做名片制作网站有什么网络营销的四个策略
  • 网站建设 m.ykn.cc网页是怎么制作的
  • 品牌企业网站建设网站权重如何查询
  • 宝塔做网站安全吗企业宣传文案
  • 深圳公司注册核名官网seo是什么意思seo是什么职位
  • 河津网站建设品牌建设的五个要素
  • 不易重名的公司名字深圳seo优化公司排名
  • 导航网页seo诊断报告怎么写
  • 各大网站搜索引擎提交入口网站内部seo优化包括