public void Move(Direction direction) { PreviousX = X; PreviousY = Y; if (direction == Direction.UP) { Y -= 1; } if (direction == Direction.LEFT) { X -= 1; } if (direction == Direction.DOWN) { Y += 1; } if (direction == Direction.RIGHT) { X += 1; } if (X > 9) { X -= MapLenX; } if (X < 0) { X += MapLenX; } if (Y > 3) { Y -= MapLenY; } if (Y < 0) { Y += MapLenY; } if (Child != null) { Child.Move(PreviousX, PreviousY); } }
public void Update() { if (LoseTimer > 0) { LoseTimer--; if (LoseTimer == 0) { Initialize(); } RefreshLoseScreen(); return; } if (WinTimer > 0) { WinTimer--; if (WinTimer == 0) { Initialize(); } RefreshWinScreen(); return; } Head.Move(CurrentDirection); var piece = Head.Child; while (piece != null) { if (piece.X == Head.X && piece.Y == Head.Y) { GameLose(); RefreshLoseScreen(); return; } piece = piece.Child; } if (Head.X == Food.X && Head.Y == Food.Y) { Head.Grow(); Length++; SpawnFood(); if (Length >= WinThreshold) { GameWin(); RefreshWinScreen(); return; } } RefreshScreen(); }