Exemplo n.º 1
0
        public Player(Color color, Keys[] keys, Game game)
        {
            this.color = color;
            this.keys = keys;
            this.game = game;

            this.snake = createSnake();
        }
Exemplo n.º 2
0
 public static void Start(Snake s, Powerup p)
 {
     switch (p.effect)
     {
         case Effects.greenSpeed:
             s.speed *= 2;
             s.turningSpeed *= 1.5;
             break;
         case Effects.redSpeed:
             s.speed *= 2;
             break;
         case Effects.greenSlow:
             s.speed /= 2;
             break;
         case Effects.redSlow:
             s.speed /= 2;
             s.turningSpeed /= 1.5;
             break;
         case Effects.greenThin:
             s.size /= 2;
             s.RecreateTrailingPen();
             break;
         case Effects.redThick:
             s.size *= 2;
             s.RecreateTrailingPen();
             break;
         case Effects.greenTurns:
             s.turningSpeed *= 1.5;
             break;
         case Effects.redTurns:
             s.turningSpeed /= 1.5;
             break;
         case Effects.greenRightAngle:
         case Effects.redRightAngle:
             s.rightAngleTurns = !s.rightAngleTurns;
             break;
         case Effects.greenInvisibility:
             s.isDrawing = false;
             break;
         case Effects.redInverse:
             s.inverse *= -1;
             break;
         default:
             break;
     }
 }
Exemplo n.º 3
0
        public bool HasCrashed(Snake snake)
        {
            int r = snake.size / 2;

            Dictionary<Point, int> snakeHistory = pointsHistory[snake.id];
            snakeHistory = snakeHistory.Where(pair => pair.Value >= pointsHistoryAge - snake.size*3).ToDictionary(pair => pair.Key, pair => pair.Value);

            for (int i = -r; i <= r; i++)
                for (int j = -r; j <= r; j++)
                    if ((i * i) + (j * j) <= r * r)
                    {
                        if ((snake.x + i < 0) || (snake.y + j < 0) || (snake.x + i >= field.GetLength(0)) || (snake.y + j >= field.GetLength(1)))
                            return true;

                        Point point = new Point(snake.x + i, snake.y + j);

                        if ((snake.isDrawing) && (!snakeHistory.ContainsKey(point)) && (pointsHistory.ContainsKey(field[snake.x + i, snake.y + j])))
                            return true;

                        if (game.IsThisActivePowerup(field[snake.x + i, snake.y + j]))
                            game.SnakeHitPowerup(snake, field[snake.x + i, snake.y + j]);

                        if (game.IsThisActiveCoin(field[snake.x + i, snake.y + j]))
                            game.SnakeHitCoin(snake, field[snake.x + i, snake.y + j]);

                        if (snake.isDrawing)
                            field[snake.x + i, snake.y + j] = snake.id;

                        if ((!snakeHistory.ContainsKey(point)))
                            snakeHistory.Add(point, pointsHistoryAge);
                        else
                            snakeHistory[point] = pointsHistoryAge;
                    }

            pointsHistory[snake.id] = snakeHistory;
            pointsHistoryAge++;
            return false;
        }
Exemplo n.º 4
0
        public void SnakeHitPowerup(Snake snake, int powerupId)
        {
            powerupsActivated[powerupId].AddExpirationTime();

            // took green powerups (for myself)
            if ((int)powerupsActivated[powerupId].effect % 2 == 1)
                snake.AddPowerup(powerupsActivated[powerupId]);
            // took red powerup (for all others)
            else
            {
                foreach (Snake s in snakes)
                {
                    if (s != snake)
                        s.AddPowerup(powerupsActivated[powerupId]);
                }
            }

            powerupsActivated.Remove(powerupId);
        }
Exemplo n.º 5
0
        public void SnakeHitCoin(Snake snake, int coinId)
        {
            for (int i = 0; i < coins.Count; i++)
                if (coins[i].id == coinId)
                    coins.RemoveAt(i);

            snake.player.score += 2;

            redrawScoreBoard();

            createCoin();
        }
Exemplo n.º 6
0
        static void Main(string[] args)
        {
            bool newGame = true;

            do//restart game loop
            {
                //menu setup
                string difficulty = "Easy";
                Dictionary <string, int> diffparameters = setdifficultydata(difficulty);
                List <string>            easyLocation   = new List <string>()
                {
                    "22,12", "23,12"
                };
                List <string> MediumLocation = new List <string>()
                {
                    "26,12", "27,12", "28,12"
                };
                List <string> HardLocation = new List <string>()
                {
                    "31,12", "32,12"
                };
                Snake snake;
                bool  closeMenu = false;
                //MENU
                while (!closeMenu)
                {
                    Console.SetCursorPosition(48, 14);
                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.Write("Easy    ");
                    Console.ForegroundColor = ConsoleColor.Yellow;
                    Console.Write("Medium    ");
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.Write("Hard");
                    Console.ForegroundColor = ConsoleColor.White;
                    Console.SetCursorPosition(52, 0);
                    Console.Write("Move with W-A-S-D");
                    buildBoard(diffparameters);
                    snake = new Snake(diffparameters);
                    while (snake.Alive)
                    {
                        do
                        {
                            snake.Move();
                            Thread.Sleep(100);
                            string diffSelect = snake.getSnakeHeadLocations();
                            if (easyLocation.Contains(diffSelect))
                            {
                                difficulty = "Easy";
                                closeMenu  = true;
                            }
                            if (MediumLocation.Contains(diffSelect))
                            {
                                difficulty = "Medium";
                                closeMenu  = true;
                            }
                            if (HardLocation.Contains(diffSelect))
                            {
                                difficulty = "Hard";
                                closeMenu  = true;
                            }
                            if (!snake.Alive || closeMenu)
                            {
                                break;
                            }
                        }while (!Console.KeyAvailable);
                        if (!snake.Alive || closeMenu)
                        {
                            break;
                        }
                        snake.UpdateDirection(Console.ReadKey().KeyChar);
                    }
                    Console.Clear();
                }

                //game setup
                diffparameters = setdifficultydata(difficulty);
                snake          = new Snake(diffparameters);
                buildBoard(diffparameters);
                double speed = diffparameters["speed"];
                int    score = -1;
                //START GAME!
                while (snake.Alive)
                {
                    do
                    {
                        snake.Move();
                        Thread.Sleep((int)speed);
                        if (!snake.Alive)
                        {
                            break;
                        }
                        if (snake.FoodPosition == "")
                        {
                            snake.FoodPosition = spawnFood(snake.getSnakeLocations(), diffparameters);
                            if (snake.FoodPosition != "")
                            {
                                score++;
                                speed -= 0.2;
                                updateScore(diffparameters, score);
                            }
                        }
                    }while (!Console.KeyAvailable);
                    if (!snake.Alive)
                    {
                        break;
                    }
                    snake.UpdateDirection(Console.ReadKey().KeyChar);
                }
                snake.DeathAnimation();
                //High Score
                Console.SetCursorPosition(49, 0);
                Console.ForegroundColor = ConsoleColor.Green;
                Console.Write("Press ENTER to continue");
                Console.ForegroundColor = ConsoleColor.White;
                Console.SetCursorPosition(0, 1);
                Console.ReadLine();//wait for input
                Console.Clear();
                diffparameters = setdifficultydata("Easy");
                buildBoard(diffparameters);
                highScore(score, difficulty);

                //do we want to restart the game?
                Console.SetCursorPosition(49, 0);
                Console.ForegroundColor = ConsoleColor.Green;
                Console.Write("Start New Game? (Y) (N)");
                Console.ForegroundColor = ConsoleColor.White;
                bool wait = true;
                while (wait)
                {
                    Console.SetCursorPosition(0, 1);
                    char newGameImput = Console.ReadKey().KeyChar;
                    if (newGameImput == 'y')
                    {
                        newGame = true;
                        wait    = false;
                    }
                    else if (newGameImput == 'n')
                    {
                        newGame = false;
                        wait    = false;
                    }
                }
                Console.Clear();
            }while (newGame);