public PointF[] head(Direction compass, Coordinates _position) { PointF[] _points; switch (compass) { case Direction.East: _points = new[] { new PointF (_position.X, _position.Y), new PointF (_position.X + 8, _position.Y), new PointF (_position.X + 12, _position.Y + 6), new PointF (_position.X + 8, _position.Y + 12), new PointF (_position.X, _position.Y + 12), new PointF (_position.X, _position.Y) }; break; case Direction.West: _points = new[] { new PointF (_position.X, _position.Y + 6), new PointF (_position.X + 4, _position.Y), new PointF (_position.X + 12, _position.Y), new PointF (_position.X + 12, _position.Y + 12), new PointF (_position.X + 4, _position.Y + 12), new PointF (_position.X, _position.Y + 6) }; break; case Direction.North: _points = new[] { new PointF (_position.X + 6, _position.Y), new PointF (_position.X + 12, _position.Y + 4), new PointF (_position.X + 12, _position.Y + 12), new PointF (_position.X, _position.Y + 12), new PointF (_position.X, _position.Y + 4), new PointF (_position.X + 6, _position.Y) }; break; case Direction.South: _points = new[] { new PointF (_position.X, _position.Y), new PointF (_position.X + 12, _position.Y), new PointF (_position.X + 12, _position.Y + 8), new PointF (_position.X + 6, _position.Y + 12), new PointF (_position.X, _position.Y + 8), new PointF (_position.X, _position.Y) }; break; default: _points = new[] { new PointF (_position.X + 6, _position.Y), new PointF (_position.X + 12, _position.Y + 4), new PointF (_position.X + 12, _position.Y + 12), new PointF (_position.X, _position.Y + 12), new PointF (_position.X, _position.Y + 4), new PointF (_position.X + 6, _position.Y) }; break; } return _points; }
public PointF[] body(Coordinates _position) { PointF[] _points; _points = new[] { new PointF (_position.X, _position.Y), new PointF (_position.X + 12, _position.Y), new PointF (_position.X + 12, _position.Y + 12), new PointF (_position.X, _position.Y + 12), new PointF (_position.X, _position.Y) }; return _points; }
public PointF[] apple(Coordinates _position) { PointF[] _points = new[] { new PointF (_position.X, _position.Y + 2), new PointF (_position.X + 2, _position.Y), new PointF (_position.X + 3, _position.Y), new PointF (_position.X + 6, _position.Y + 3), new PointF (_position.X + 9, _position.Y), new PointF (_position.X + 10, _position.Y), new PointF (_position.X + 12, _position.Y + 2), new PointF (_position.X + 12, _position.Y + 10), new PointF (_position.X + 10, _position.Y + 12), new PointF (_position.X + 2, _position.Y + 12), new PointF (_position.X, _position.Y + 10), new PointF (_position.X, _position.Y + 2) }; return _points; }
private void BuildApples() { Coordinates newApple = null; bool collision = true; while (collision) { int newX = 1; int newY = 1; while (newX % 12 != 0) newX = 12 + random.Next (xMax - 24); while (newY % 12 != 0) newY = 12 + random.Next (yMax - 24); newApple = new Coordinates (newX, newY); collision = false; foreach (Coordinates position in snake) { if (position == newApple) collision = true; } } apple.Add (newApple); _applePoints.Add (drawObjects.apple (newApple)); }
private void BuildWalls() { _wallPoints = new List<PointF[]> (); Coordinates _position; for (int x = 0; x < xMax; x += 12) { _position = new Coordinates (x, 0); _wallPoints.Add (drawObjects.wall (_position)); _position = new Coordinates (x, yMax - 12); _wallPoints.Add (drawObjects.wall (_position)); } for (int y = 1; y < yMax; y += 12) { _position = new Coordinates (0, y); _wallPoints.Add (drawObjects.wall (_position)); _position = new Coordinates (xMax - 12, y); _wallPoints.Add (drawObjects.wall (_position)); } }
private void AddApple() { Coordinates coordinates = null; bool invalidCoordinate = true; while (invalidCoordinate) { int randomX = 1 + random.Next (xMax - 2); int randomY = 1 + random.Next (yMax - 2); coordinates = new Coordinates (randomX, randomY); foreach (Coordinates _snake in snake) { if (_snake.X.Equals (coordinates.X) && _snake.Y.Equals (coordinates.Y)) invalidCoordinate = true; } invalidCoordinate = false; } apples.Add (coordinates); }
private void BuildSnake() { bool isGrowing = false; Coordinates head = snake [0]; Coordinates newHead = new Coordinates (1, 1); switch (direction) { case Direction.East: newHead = new Coordinates (head.X + 1, head.Y); break; case Direction.West: newHead = new Coordinates (head.X - 1, head.Y); break; case Direction.North: newHead = new Coordinates (head.X, head.Y - 1); break; case Direction.South: newHead = new Coordinates (head.X, head.Y + 1); break; } if ((newHead.X < 1) || (newHead.Y < 1) || (newHead.X > xMax - 2) || (newHead.Y > yMax - 2)) { playing = false; return; } foreach (Coordinates _snake in snake) { if (_snake.X.Equals (newHead.X) && _snake.Y.Equals (newHead.Y)) { playing = false; return; } } foreach (Coordinates apple in apples) { if (apple.X.Equals (newHead.X) && apple.Y.Equals (newHead.Y)) { apples.Remove (apple); AddApple (); Score++; textScore.Text = string.Format ("Score: {0} ", Score.ToString ()); delayMove -= 20; isGrowing = true; break; } } snake.Insert (0, newHead); if (!isGrowing) snake.RemoveAt (snake.Count - 1); int index = 0; foreach (Coordinates c in snake) { if (index == 0) { switch (direction) { case Direction.East: SetObject (GameObjects.HeadRight, c.X, c.Y); break; case Direction.West: SetObject (GameObjects.HeadLeft, c.X, c.Y); break; case Direction.North: SetObject (GameObjects.HeadUP, c.X, c.Y); break; case Direction.South: SetObject (GameObjects.HeadDown, c.X, c.Y); break; } } else if (direction == Direction.East || direction == Direction.West) SetObject (GameObjects.BodyH, c.X, c.Y); else SetObject (GameObjects.BodyV, c.X, c.Y); index++; } }