public GameLoop() { physicsObjects.Add(snake); ConvertMapToDoubleArray(); GenerateMap(); ghostTarget = new EmptySpace(5, 5); physicsObjects.Add(ghostTarget); redGhost = new Ghost(2, 1, physicsObjects); pinkGhost = new Ghost(1, 21, physicsObjects); redGhost.Visuals = 'R'; pinkGhost.Visuals = 'P'; col = new PhysicsCollision(physicsObjects); direction = Dir.none; Console.SetWindowSize(50, 50); Thread producer = new Thread(ReadKey); producer.Start(); Console.CursorVisible = false; target = snake; Loop(); }
private void Update() { if (direction != Dir.none) { timer++; if (timer > 1) { timer = 0; if (pathRed != null) { if (counter < pathRed.Count) { redGhost.position = pathRed[counter]; counter++; } else { counter = 0; } } if (pathPink != null) { timer = 0; if (counter2 < pathPink.Count) { pinkGhost.position = pathPink[counter2]; counter2++; } else { counter2 = 0; } } } switch (direction) { case Dir.up: if (col.CheckCollisions(snake, 0, -1) != typeof(Map)) { snake.position = new Vector2(snake.position.PosX, Math.Max(0, snake.position.PosY - 1)); int minus = 3; do { ghostTarget.position = new Vector2(snake.position.PosX, Math.Max(snake.position.PosY, snake.position.PosY - minus)); minus--; } while (col.CheckCollisions(ghostTarget, ghostTarget.position.PosX, ghostTarget.position.PosY) == typeof(Map)); lastDirection = direction; } break; case Dir.left: if (col.CheckCollisions(snake, -1, 0) != typeof(Map)) { snake.position = new Vector2(Math.Max(0, snake.position.PosX - 1), snake.position.PosY); int minus = 3; do { ghostTarget.position = new Vector2(Math.Max(0, snake.position.PosX - minus), snake.position.PosY); minus--; } while (col.CheckCollisions(ghostTarget, ghostTarget.position.PosX, ghostTarget.position.PosY) == typeof(Map)); lastDirection = direction; } break; case Dir.down: if (col.CheckCollisions(snake, 0, 1) != typeof(Map)) { snake.position = new Vector2(snake.position.PosX, Math.Min(db.YDim - 1, snake.position.PosY + 1)); int minus = 3; do { ghostTarget.position = new Vector2(Math.Min(db.YDim - 1, snake.position.PosX - minus), snake.position.PosY); minus--; } while (col.CheckCollisions(ghostTarget, ghostTarget.position.PosX, ghostTarget.position.PosY) == typeof(Map)); lastDirection = direction; } break; case Dir.right: if (col.CheckCollisions(snake, 1, 0) != typeof(Map)) { snake.position = new Vector2(Math.Min(db.XDim - 1, snake.position.PosX + 1), snake.position.PosY); int minus = 3; do { ghostTarget.position = new Vector2(Math.Min(db.XDim - 1, snake.position.PosX - minus), snake.position.PosY); minus--; } while (col.CheckCollisions(ghostTarget, ghostTarget.position.PosX, ghostTarget.position.PosY) == typeof(Map)); lastDirection = direction; } break; } if (redGhost.position.Equals(snake.position)) { target = redCorner; } if (redGhost.position.Equals(redCorner.position)) { target = snake; } pathRed = redGhost.CalcuatePath(target); pathPink = pinkGhost.CalcuatePath(ghostTarget); counter2 = 0; counter = 0; } snake.UpdatePhysics(); redGhost.UpdatePhysics(); pinkGhost.UpdatePhysics(); if (col.CheckCollisions(snake) == typeof(Pellet)) { for (int i = 0; i < physicsObjects.Count; i++) { if (physicsObjects[i].position.Equals(snake.position)) { if (physicsObjects[i] != snake) { physicsObjects[i] = new EmptySpace(snake.position.PosX, snake.position.PosY); } mapVisuals[snake.position.PosX, snake.position.PosY] = ' '; } } Score++; } if (col.CheckCollisions(snake) == typeof(Teleporter)) { int toPlace = snake.position.PosX == 0 ? db.XDim - 2 : 1; snake.position = new Vector2(toPlace, snake.position.PosY); } direction = Dir.none; }