//Default Constructor public Snake() { SnakeParts head = new SnakeParts(); Nagini.Add(head); direction = 1; }
/// <summary> /// Constructor. /// </summary> public SnakeMoveController(SnakeParts snakeParts) { _nextMoveTimer = new Stopwatch(); _dirDependencies = new SnakeDirectionDependencies(); _snakeParts = snakeParts; _curWorldMoveDir = Direction.UP; }
/// <summary> /// Constructor. Creates instances. /// </summary> public SnakeController(Floor floor) { _parts = new SnakeParts(); _moveController = new SnakeMoveController(_parts); _snakeEatingSystem = new SnakeEatingController(_parts); _snakeGrowingSystem = new SnakeGrowingController(_parts); _snakeCollisionController = new SnakeCollisionController(_parts, floor); }
//----------------------YILANIN HAREKET ETTİĞİ , KUYRUĞUNUN BAŞINI TAKİP ETTİĞİ KISIM----------------------------------// public void Move(Direction direction) { ourDirection = direction; for (int i = snakePart.Length - 1; i > 0; i--) // Yılanın parçalarının , yılanın başını takip ettiği döngümüz. { snakePart[i] = new SnakeParts(snakePart[i - 1].x_, snakePart[i - 1].y_); } snakePart[0] = new SnakeParts(snakePart[0].x_ + direction._x, snakePart[0].y_ + direction._y); }
public void StartGame() { gameover = false; currentscore = 0; direction = 1; Nagini.Clear(); Snake head = new Snake(); int NewPartX = 32, NewPartY = 13; SnakeParts newPart = new SnakeParts(NewPartX, NewPartY); Nagini.Insert(0, newPart); Food food = new Food(); food.GenerateFood(); }
public bool Move() //return true successful move else false { int CurrentHeadX, CurrentHeadY, NewHeadX = 0, NewHeadY = 0; CurrentHeadX = Nagini[0].GetXPosition(); CurrentHeadY = Nagini[0].GetYPosition(); if (direction == 1) // up { NewHeadX = CurrentHeadX; NewHeadY = CurrentHeadY - 1; } else if (direction == 2) //down { NewHeadX = CurrentHeadX; NewHeadY = CurrentHeadY + 1; } else if (direction == 3) //left { NewHeadX = CurrentHeadX - 1; NewHeadY = CurrentHeadY; } else if (direction == 4) // right { NewHeadX = CurrentHeadX + 1; NewHeadY = CurrentHeadY; } if (NewHeadX < 0 || NewHeadX > 63 || NewHeadY < 0 || NewHeadY > 24) { gameover = true; return(false); } else { SnakeParts newHead = new SnakeParts(NewHeadX, NewHeadY); Nagini.Insert(0, newHead); Nagini.RemoveAt(Nagini.Count - 1); gameover = false; return(true); } }
public void Grow() { int NewPartX = 0, NewPartY = 0, CurrentPartX, CurrentPartY; CurrentPartX = Nagini[0].GetXPosition(); CurrentPartY = Nagini[0].GetYPosition(); if (direction == 1) // up { NewPartX = CurrentPartX; NewPartY = CurrentPartY - 1; } else if (direction == 2) //down { NewPartX = CurrentPartX; NewPartY = CurrentPartY + 1; } else if (direction == 3) //left { NewPartX = CurrentPartX - 1; NewPartY = CurrentPartY; } else if (direction == 4) // right { NewPartX = CurrentPartX + 1; NewPartY = CurrentPartY; } SnakeParts newPart = new SnakeParts(NewPartX, NewPartY); Nagini.Insert(0, newPart); }
Direction ourDirection; // public Snake() { snakePart = new SnakeParts[1]; snakeSize = 1; // Yılanın büyüklüğünün değerini en başta 1 (başı) yapıyoruz. snakePart[0] = new SnakeParts(170, 170); // Yılanın başının başlangıç noktasını buradan ayarlıyoruz. }
/// <summary> /// Constructor. Set snakeParts reference. /// </summary> public SnakeMoveEventArgs(SnakeParts snakeParts) { this.snakeParts = snakeParts; }
/// <summary> /// Constructor. Aggregates SnakeParts and Floor objects. /// </summary> public SnakeCollisionController(SnakeParts snakeParts, Floor floor) { _snakeParts = snakeParts; _floor = floor; }
/// <summary> /// Constructor. Aggregates snake parts. /// </summary> public SnakeEatingController(SnakeParts snakeParts) { _snakeParts = snakeParts; }
public void UpdateSnakePositions() { Snake snakeHead = SnakeParts[0]; Snake snakeTail = SnakeParts[SnakeParts.Count - 1]; if (snakeHead.HeadDirection == Direction.Left) { if (snakeHead.UnitPosition.Column == 0 && !(snakeHead.UnitPosition.Row == 0)) { snakeHead.HeadDirection = Direction.Up; snakeHead.UnitPosition = new Position(snakeHead.UnitPosition.Row - 1, snakeHead.UnitPosition.Column); } else if (snakeHead.UnitPosition.Column == 0 && snakeHead.UnitPosition.Row == 0) { snakeHead.HeadDirection = Direction.Down; snakeHead.UnitPosition = new Position(snakeHead.UnitPosition.Row + 1, snakeHead.UnitPosition.Column); } else { snakeHead.UnitPosition = new Position(snakeHead.UnitPosition.Row, snakeHead.UnitPosition.Column - 1); } if (GameMap.MapUnits[snakeHead.UnitPosition.Row, snakeHead.UnitPosition.Column] is Bomb) { GameOver = true; } else if ((GameMap.MapUnits[snakeHead.UnitPosition.Row, snakeHead.UnitPosition.Column] is Snake) && !(snakeHead.UnitPosition == snakeTail.UnitPosition)) { GameOver = true; } else if (GameMap.MapUnits[snakeHead.UnitPosition.Row, snakeHead.UnitPosition.Column] is Fruit) { Pontos++; GameMap.AddUnit(new Snake(snakeHead.UnitPosition)); Fruits.RemoveAll(x => x.UnitPosition == snakeHead.UnitPosition); SnakeParts[0] = snakeHead; for (int i = 1; i < SnakeParts.Count - 1; i++) { SnakeParts[i].UnitPosition = SnakeParts[i - 1].PreviousPosition; } SnakeParts.Insert(SnakeParts.Count - 1, new Snake((SnakeParts[SnakeParts.Count - 2].PreviousPosition))); for (int i = 0; i < SnakeParts.Count; i++) { SnakeParts[i].PreviousPosition = SnakeParts[i].UnitPosition; } } else { GameMap.AddUnit(new Snake(snakeHead.UnitPosition)); GameMap.RemoveUnit(snakeTail.UnitPosition); SnakeParts[0] = snakeHead; for (int i = 1; i < SnakeParts.Count; i++) { SnakeParts[i].UnitPosition = SnakeParts[i - 1].PreviousPosition; } for (int i = 0; i < SnakeParts.Count; i++) { SnakeParts[i].PreviousPosition = SnakeParts[i].UnitPosition; } } } else if (snakeHead.HeadDirection == Direction.Right) { if (snakeHead.UnitPosition.Column == (GameMap.NumberOfColumns - 1) && snakeHead.UnitPosition.Row != 0) { snakeHead.HeadDirection = Direction.Up; snakeHead.UnitPosition = new Position(snakeHead.UnitPosition.Row - 1, snakeHead.UnitPosition.Column); } else if (snakeHead.UnitPosition.Column == (GameMap.NumberOfColumns - 1) && snakeHead.UnitPosition.Row == 0) { snakeHead.HeadDirection = Direction.Down; snakeHead.UnitPosition = new Position(snakeHead.UnitPosition.Row + 1, snakeHead.UnitPosition.Column); } else { snakeHead.UnitPosition = new Position(snakeHead.UnitPosition.Row, snakeHead.UnitPosition.Column + 1); } if (GameMap.MapUnits[snakeHead.UnitPosition.Row, snakeHead.UnitPosition.Column] is Bomb) { GameOver = true; } else if ((GameMap.MapUnits[snakeHead.UnitPosition.Row, snakeHead.UnitPosition.Column] is Snake) && !(snakeHead.UnitPosition == snakeTail.UnitPosition)) { GameOver = true; } else if (GameMap.MapUnits[snakeHead.UnitPosition.Row, snakeHead.UnitPosition.Column] is Fruit) { Pontos++; GameMap.AddUnit(new Snake(snakeHead.UnitPosition)); Fruits.RemoveAll(x => x.UnitPosition == snakeHead.UnitPosition); SnakeParts[0] = snakeHead; for (int i = 1; i < SnakeParts.Count - 1; i++) { SnakeParts[i].UnitPosition = SnakeParts[i - 1].PreviousPosition; } SnakeParts.Insert(SnakeParts.Count - 1, new Snake((SnakeParts[SnakeParts.Count - 2].PreviousPosition))); for (int i = 0; i < SnakeParts.Count; i++) { SnakeParts[i].PreviousPosition = SnakeParts[i].UnitPosition; } } else { GameMap.AddUnit(new Snake(snakeHead.UnitPosition)); GameMap.RemoveUnit(snakeTail.UnitPosition); SnakeParts[0] = snakeHead; for (int i = 1; i < SnakeParts.Count; i++) { SnakeParts[i].UnitPosition = SnakeParts[i - 1].PreviousPosition; } for (int i = 0; i < SnakeParts.Count; i++) { SnakeParts[i].PreviousPosition = SnakeParts[i].UnitPosition; } } } else if (snakeHead.HeadDirection == Direction.Up) { if (snakeHead.UnitPosition.Row == 0 && snakeHead.UnitPosition.Column != 0) { snakeHead.HeadDirection = Direction.Left; snakeHead.UnitPosition = new Position(snakeHead.UnitPosition.Row, snakeHead.UnitPosition.Column - 1); } else if (snakeHead.UnitPosition.Row == 0 && snakeHead.UnitPosition.Column == 0) { snakeHead.HeadDirection = Direction.Right; snakeHead.UnitPosition = new Position(snakeHead.UnitPosition.Row, snakeHead.UnitPosition.Column + 1); } else { snakeHead.UnitPosition = new Position(snakeHead.UnitPosition.Row - 1, snakeHead.UnitPosition.Column); } if (GameMap.MapUnits[snakeHead.UnitPosition.Row, snakeHead.UnitPosition.Column] is Bomb) { GameOver = true; } else if ((GameMap.MapUnits[snakeHead.UnitPosition.Row, snakeHead.UnitPosition.Column] is Snake) && !(snakeHead.UnitPosition == snakeTail.UnitPosition)) { GameOver = true; } else if (GameMap.MapUnits[snakeHead.UnitPosition.Row, snakeHead.UnitPosition.Column] is Fruit) { Pontos++; GameMap.AddUnit(new Snake(snakeHead.UnitPosition)); Fruits.RemoveAll(x => x.UnitPosition == snakeHead.UnitPosition); SnakeParts[0] = snakeHead; for (int i = 1; i < SnakeParts.Count - 1; i++) { SnakeParts[i].UnitPosition = SnakeParts[i - 1].PreviousPosition; } SnakeParts.Insert(SnakeParts.Count - 1, new Snake((SnakeParts[SnakeParts.Count - 2].PreviousPosition))); for (int i = 0; i < SnakeParts.Count; i++) { SnakeParts[i].PreviousPosition = SnakeParts[i].UnitPosition; } } else { GameMap.AddUnit(new Snake(snakeHead.UnitPosition)); GameMap.RemoveUnit(snakeTail.UnitPosition); SnakeParts[0] = snakeHead; for (int i = 1; i < SnakeParts.Count; i++) { SnakeParts[i].UnitPosition = SnakeParts[i - 1].PreviousPosition; } for (int i = 0; i < SnakeParts.Count; i++) { SnakeParts[i].PreviousPosition = SnakeParts[i].UnitPosition; } } } else if (snakeHead.HeadDirection == Direction.Down) { if (snakeHead.UnitPosition.Row == (GameMap.NumberOfRows - 1) && snakeHead.UnitPosition.Column != 0) { snakeHead.HeadDirection = Direction.Left; snakeHead.UnitPosition = new Position(snakeHead.UnitPosition.Row, snakeHead.UnitPosition.Column - 1); } else if (snakeHead.UnitPosition.Row == (GameMap.NumberOfRows - 1) && snakeHead.UnitPosition.Column == 0) { snakeHead.HeadDirection = Direction.Right; snakeHead.UnitPosition = new Position(snakeHead.UnitPosition.Row, snakeHead.UnitPosition.Column + 1); } else { snakeHead.UnitPosition = new Position(snakeHead.UnitPosition.Row + 1, snakeHead.UnitPosition.Column); } if (GameMap.MapUnits[snakeHead.UnitPosition.Row, snakeHead.UnitPosition.Column] is Bomb) { GameOver = true; } else if ((GameMap.MapUnits[snakeHead.UnitPosition.Row, snakeHead.UnitPosition.Column] is Snake) && !(snakeHead.UnitPosition == snakeTail.UnitPosition)) { GameOver = true; } else if (GameMap.MapUnits[snakeHead.UnitPosition.Row, snakeHead.UnitPosition.Column] is Fruit) { Pontos++; GameMap.AddUnit(new Snake(snakeHead.UnitPosition)); Fruits.RemoveAll(x => x.UnitPosition == snakeHead.UnitPosition); SnakeParts[0] = snakeHead; for (int i = 1; i < SnakeParts.Count - 1; i++) { SnakeParts[i].UnitPosition = SnakeParts[i - 1].PreviousPosition; } SnakeParts.Insert(SnakeParts.Count - 1, new Snake((SnakeParts[SnakeParts.Count - 2].PreviousPosition))); for (int i = 0; i < SnakeParts.Count; i++) { SnakeParts[i].PreviousPosition = SnakeParts[i].UnitPosition; } } else { GameMap.AddUnit(new Snake(snakeHead.UnitPosition)); GameMap.RemoveUnit(snakeTail.UnitPosition); SnakeParts[0] = snakeHead; for (int i = 1; i < SnakeParts.Count; i++) { SnakeParts[i].UnitPosition = SnakeParts[i - 1].PreviousPosition; } for (int i = 0; i < SnakeParts.Count; i++) { SnakeParts[i].PreviousPosition = SnakeParts[i].UnitPosition; } } } }
/// <summary> /// Contructor. Aggregates snake parts. /// </summary> public SnakeGrowingController(SnakeParts parts) { _parts = parts; }