In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Today, I will talk to you about how to use Silverlight to develop gluttonous snake games, many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.
Introduction
Use Silverlight 3.0 (c #) to develop a gluttonous snake game
Play method
W/S/A/D or ↑ / ↓ / ← / → controls the movement of snakes
Screenshot
Train of thought
1. Each segment of the gluttonous snake is 16 × 16 pixels, and the scene is 640 × 480 pixels, that is to say, the grid is 40 × 30, and the side length of each grid is 16.
2. the location of the food and the change of the direction of the gluttonous snake should be carried out in the relevant grid.
3. The motion of the gluttonous snake is calculated by the method of real-time calculation. When the gluttonous snake moves into the grid (the pixel position of a certain section of the snake% the side length of the grid 0 & & (DateTime.Now-_ prevAddBeanDateTime). TotalSeconds > 3) {List emptyCells = GetEmptyCells () If (emptyCells.Count = = 0) {GameOver (this, EventArgs.Empty); return;} CellPoint point = emptyCells [_ random.Next (0, emptyCells.Count)]; Bean bean = new Bean () Bean.SetValue (Canvas.LeftProperty, point.X * App.CellSize); bean.SetValue (Canvas.TopProperty, point.Y * App.CellSize); _ beans.Add (bean, point); canvasBean.Children.Add (bean) Bean.ani.Completed + = delegate {ripple.ShowRipple (new Point (point.X * App.CellSize + App.CellSize / 2, point.Y * App.CellSize + App.CellSize / 2)); player.PlayDrop ();}; _ needBeansCount--; _ prevAddBeanDateTime = DateTime.Now }} private DateTime _ prevDateTime = DateTime.Now; private double _ leftoverLength = 0d; void CompositionTarget_Rendering (object sender, EventArgs e) {double length = (DateTime.Now-_ prevDateTime). TotalSeconds + _ leftoverLength; while (length > _ dt) {Update () Length-= _ dt;} _ leftoverLength = length; _ prevDateTime = DateTime.Now;} / private void Update () {if (! _ enabled) return Double offset = Math.Round (_ speed * _ dt, _ decimals); / / the location of the snakehead enters the if in the grid point area (Math.Abs (Math.Round ((double) _ bodies.First (). Key.GetValue (Canvas.TopProperty)% App.CellSize, _ decimals))
< offset && Math.Abs(Math.Round((double)_bodies.First().Key.GetValue(Canvas.LeftProperty) % App.CellSize, _decimals)) < offset) { UpdateDirection(); CorrectPosition(); UpdateBodyCell(); CheckEat(); CheckSkin(); CheckCollision(); UpdateBean(); if (_needRaiseAteEvent) { Ate(this.Ate, EventArgs.Empty); _needRaiseAteEvent = false; } } UpdatePosition(); } /// /// 蜕皮 /// private void CheckSkin() { if (_bodies.Count >= _ ateCapacity + _ selfLength) AddSkin (_ ateCapacity) } / collision detection / private void CheckCollision () {if (_ skins.Any (p = > p.Value = = _ bodies.First () .Value) | | _ bodies.Where (p = > p.Key.BodyType = = BodyType.Tail) .Any (p = > p.Value = = _ bodies.First () .Value) {_ enabled = false Player.PlayOver (); GameOver (this, EventArgs.Empty) }} / eat beans / private void CheckEat () {/ / whether there are any beans eaten var bean = _ beans.FirstOrDefault (p = > p.Value = = _ bodies.First () .value). Key If (bean! = null) {_ beans.Remove (bean); canvasBean.Children.Remove (bean); player.PlayEat (); AddTail (); AddBean (); _ needRaiseAteEvent = true } / update the direction of movement of each segment of the snake / private void UpdateDirection () {for (int I = _ bodies.Count-1; I >-1) If -) {if (I = = 0) _ bodies.ElementAt (I). Key.MoveDirection = _ moveDirection; else _ bodies.ElementAt (I). Key.MoveDirection = _ bodies.ElementAt (i- 1). Key.MoveDirection }} / update the position of each segment of the snake / private void UpdatePosition () {double offset = Math.Round (_ speed * _ dt, _ decimals) Foreach (var body in _ bodies.Keys) {if (body.MoveDirection = = Direction.Up) body.SetValue (Canvas.TopProperty, Math.Round ((double) body.GetValue (Canvas.TopProperty)-offset, _ decimals)) Else if (body.MoveDirection = = Direction.Down) body.SetValue (Canvas.TopProperty, Math.Round ((double) body.GetValue (Canvas.TopProperty) + offset, _ decimals)); else if (body.MoveDirection = = Direction.Left) body.SetValue (Canvas.LeftProperty, Math.Round ((double) body.GetValue (Canvas.LeftProperty)-offset, _ decimals)) Else if (body.MoveDirection = = Direction.Right) body.SetValue (Canvas.LeftProperty, Math.Round ((double) body.GetValue (Canvas.LeftProperty) + offset, _ decimals)) }} / private void AddSkin (int count) {player.PlaySkin (); while (count > 0) {KeyValuePair body = _ bodies.ElementAt (_ bodies.Count-1) CellPoint skinPoint = body.Value; Skin skin = new Skin (); skin.SetValue (Canvas.LeftProperty, skinPoint.X * App.CellSize); skin.SetValue (Canvas.TopProperty, skinPoint.Y * App.CellSize); _ skins.Add (skin, skinPoint); canvasSkin.Children.Add (skin) _ emptyCells.Remove (skinPoint); canvasSnake.Children.Remove (body.Key); _ bodies.Remove (body.Key); count-- }} # region helper method / the specified location information is an integer / private int CorrectPosition (double position) {double result; double offset = Math.Round (_ speed * _ dt, _ decimals) Double temp = Math.Round (position% App.CellSize, _ decimals); if (Math.Abs (temp)
< offset) result = Math.Round(position - temp); else result = Math.Round(position - temp) + Math.Sign(temp) * App.CellSize; return (int)result; } /// /// 修正蛇的每一段的位置为整数 /// private void CorrectPosition() { foreach (Body body in _bodies.Keys) { double x = CorrectPosition((double)body.GetValue(Canvas.LeftProperty)); double y = CorrectPosition((double)body.GetValue(Canvas.TopProperty)); if (x == App.Width) x = 0d; else if (x == -App.CellSize) x = App.Width - App.CellSize; else if (y == App.Height) y = 0d; else if (y == -App.CellSize) y = App.Height - App.CellSize; body.SetValue(Canvas.LeftProperty, x); body.SetValue(Canvas.TopProperty, y); } } /// /// 更新蛇的每一段的网格位置信息 /// private void UpdateBodyCell() { for (int i = 0; i < _bodies.Count; i++) { UpdateBodyCell(_bodies.ElementAt(i).Key); } } /// /// 更新指定的 Body 的网格位置信息 /// private void UpdateBodyCell(Body body) { CellPoint point = new CellPoint((int)((double)body.GetValue(Canvas.LeftProperty) / App.CellSize), (int)((double)body.GetValue(Canvas.TopProperty) / App.CellSize)); if (body.MoveDirection == Direction.Up) point.Y--; else if (body.MoveDirection == Direction.Down) point.Y++; else if (body.MoveDirection == Direction.Left) point.X--; else if (body.MoveDirection == Direction.Right) point.X++; point = CorrectCellPoint(point); _bodies[body] = point; } /// /// 修正网格位置 /// private CellPoint CorrectCellPoint(CellPoint point) { if (point.X >_ columns-1) point.X = _ columns-point.X; else if (point.X
< 0) point.X = point.X + _columns; if (point.Y >_ rows-1) point.Y = _ rows-point.Y; else if (point.Y
< 0) point.Y = point.Y + _rows; return point; } /// /// 获取空网格集合 /// private List GetEmptyCells() { List emptyCells = new List(); List aroundHeadCells = new List(); CellPoint headPoint = _bodies.First().Value; for (int i = -5; i < 5; i++) { for (int j = -5; j < 5; j++) { CellPoint point = new CellPoint(headPoint.X + i, headPoint.Y + j); point = CorrectCellPoint(point); aroundHeadCells.Add(point); } } // skin 的占位情况因为确定了就不变了,所以在 AddSkin() 处计算 // 为了以下 LINQ 的可用,需要重写 CellPoint 的 public override bool Equals(object obj) emptyCells = _emptyCells.Where(p =>! _ bodies.Select (x = > x.Value) .Concluded (p). ToList (); emptyCells = emptyCells.Where (p = >! _ beans.Select (x = > x.Value) .Conclusion.ToList (); emptyCells = emptyCells.Where (p = >! aroundHeadCells.Contains (p)) .ToList (); return emptyCells } # endregion # region attribute public Direction MoveDirection {set {Body head = _ bodies.First () .Key; if (head.MoveDirection = = Direction.Up & & value = = Direction.Down) return If (head.MoveDirection = = Direction.Down & & value = = Direction.Up) return; if (head.MoveDirection = = Direction.Left & & value = = Direction.Right) return; if (head.MoveDirection = = Direction.Right & & value = = Direction.Left) return; _ moveDirection = value }} public bool Enabled {get {return _ enabled;} set {_ enabled = value;}} public double Speed {get {return _ speed;} set {_ speed = value }} public int AteCapacity {get {return _ ateCapacity;} set {_ ateCapacity = value;}} # endregion # region events GameOver and Ate public event EventHandler GameOver; public event EventHandler Ate # endregion}} after reading the above, do you have any further understanding of how to use Silverlight to develop gluttonous Snake games? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.