예제 #1
0
        private void MoveSnakeBody()
        {
            Point part = new Point();

            if (SnakeParts.Count() > 1)
            {
                foreach (Image image in SnakeParts)
                {
                    if (image == SnakeParts.First())
                    {
                    }
                    else if (image == SnakeParts[1])
                    {
                        part = new Point(Canvas.GetLeft(SnakeParts[1]), Canvas.GetTop(SnakeParts[1]));

                        Canvas.SetLeft(SnakeParts[1], Canvas.GetLeft(SnakeParts.First()));
                        Canvas.SetTop(SnakeParts[1], Canvas.GetTop(SnakeParts.First()));
                    }
                    else
                    {
                        Point tempPos = new Point(Canvas.GetLeft(image), Canvas.GetTop(image));

                        Canvas.SetLeft(image, part.X);
                        Canvas.SetTop(image, part.Y);

                        part = tempPos;
                    }
                }
            }
        }
예제 #2
0
        public void Grow()
        {
            double newTailX = SnakeParts[SnakeParts.Count - 1].Position.X;
            double newTailY = SnakeParts[SnakeParts.Count - 1].Position.Y;

            switch (SnakeDirection)
            {
            case SnakeDirection.Left:
                newTailX -= Constants.SNAKESQUARESIZE;
                break;

            case SnakeDirection.Right:
                newTailX += Constants.SNAKESQUARESIZE;
                break;

            case SnakeDirection.Up:
                newTailY -= Constants.SNAKESQUARESIZE;
                break;

            case SnakeDirection.Down:
                newTailY += Constants.SNAKESQUARESIZE;
                break;
            }

            //Adding new tail to the snake to simulate its growth
            ISnakePart newTail = Factory.CreateSnakePart();

            newTail.Position = new Point(newTailX, newTailY);
            SnakeParts.Add(newTail);
        }
예제 #3
0
        private void GenerateSnakePart()
        {
            Point last      = new Point(Canvas.GetLeft(SnakeParts.Last()), Canvas.GetTop(SnakeParts.Last()));
            Image snakePart = new Image
            {
                Source = new BitmapImage(new Uri(_colorPath)),
                Height = _height,
                Width  = _height
            };

            SnakeParts.Add(snakePart);

            switch (SnakeDriection)
            {
            case Direction.Right:
                last.X -= 20;
                break;

            case Direction.Left:
                last.X += 20;
                break;

            case Direction.Up:
                last.Y += 20;
                break;

            case Direction.Down:
                last.Y -= 20;
                break;
            }
            Canvas.SetTop(snakePart, last.Y);
            Canvas.SetLeft(snakePart, last.X);

            _playField.Children.Add(snakePart);
        }
예제 #4
0
        public void GenerateSnake()
        {
            Image snakeHead = new Image
            {
                Source = new BitmapImage(new Uri(_colorPath))
            };

            snakeHead.Height = _height;
            snakeHead.Width  = _height;

            SnakeParts.Add(snakeHead);
            _playField.Children.Add(snakeHead);
        }
예제 #5
0
 private void CheckSnake()
 {
     foreach (Image part in SnakeParts)
     {
         if (part != SnakeParts.First())
         {
             if (Canvas.GetTop(part) == _posY && Canvas.GetLeft(part) == _posX)
             {
                 StopGame();
             }
         }
     }
 }
예제 #6
0
        //Constructor for new snake used at the beginning of each game
        public SnakeObject()
        {
            //Snake initially always consists of 3 tiles, the tile at position 0 is snakes head
            //Snake is initially positioned in the center of game area
            SnakeParts.Add(new SnakePart(isHead: true)
            {
                Position = new Point(Constants.SNAKESQUARESIZE * 11, Constants.SNAKESQUARESIZE * 10),
            });

            SnakeParts.Add(new SnakePart()
            {
                Position = new Point(Constants.SNAKESQUARESIZE * 10, Constants.SNAKESQUARESIZE * 10)
            });

            SnakeParts.Add(new SnakePart()
            {
                Position = new Point(Constants.SNAKESQUARESIZE * 9, Constants.SNAKESQUARESIZE * 10)
            });
        }
예제 #7
0
        //Updating snake parts positions after each time interval to simulate movement
        public void UpdatePartsPositions()
        {
            double newHeadX = SnakeParts[0].Position.X;
            double newHeadY = SnakeParts[0].Position.Y;

            //Checking movement direction to determine where to position head
            switch (SnakeDirection)
            {
            case SnakeDirection.Left:
                newHeadX -= Constants.SNAKESQUARESIZE;
                break;

            case SnakeDirection.Right:
                newHeadX += Constants.SNAKESQUARESIZE;
                break;

            case SnakeDirection.Up:
                newHeadY -= Constants.SNAKESQUARESIZE;
                break;

            case SnakeDirection.Down:
                newHeadY += Constants.SNAKESQUARESIZE;
                break;
            }

            ISnakePart newHead = new SnakePart(isHead: true)
            {
                Position = new Point()
                {
                    X = newHeadX,
                    Y = newHeadY
                }
            };

            //Changing old head tile to become body part
            SnakeParts[0].SwitcHeadToBody();
            //Inserting new head at the beginning of the snake
            SnakeParts.Insert(0, newHead);
            //Removing snakes tail
            SnakeParts.RemoveAt(SnakeParts.Count - 1);
        }
예제 #8
0
        // zum pruefen einer kollision mit dem Schlangenkopf
        private void ProofCollision()
        {
            //HitTestResult collAplle = VisualTreeHelper.HitTest(playground, myApple.GetPosition());
            HitTestResult hitted = VisualTreeHelper.HitTest(playground, snake[0].GetPosition());

            if (hitted != null)
            {
                string name = ((Shape)hitted.VisualHit).Name;
                // was haben wir getroffen?
                if (name == "Border")
                {
                    sound = new SoundPlayer(Properties.Resources.gameOver);
                    sound.Play();
                    EndGame();
                }

                if (name == "Hitbox" || name == "Snake")
                {
                    ProgressBarLife.Value -= 1;
                    if (ProgressBarLife.Value <= 0)
                    {
                        sound = new SoundPlayer(Properties.Resources.gameOver);
                        sound.Play();
                        EndGame();
                    }
                    damageCount = 0;
                    damageDone  = false;
                    timerLoseLife.Start();
                    sound = new SoundPlayer(Properties.Resources.damage);
                    sound.Play();
                    return;
                }
                if (name == "Apple")
                {
                    Apples.ClearSnakePartsList();
                    for (int index = 1; index < snake.Count; index++)
                    {
                        Apples.SetSnakePartsPosition(snake[index].GetPosition());
                    }
                    points                 = gamePoints.VeraenderePunkte(addPoints);
                    showPoints.Content     = points;
                    ProgressBarLife.Value += 0.1;
                    sound = new SoundPlayer(Properties.Resources.bite);
                    sound.Play();
                    // Geschwindigkeit der Schlange erhoehen, wenn nicht das Maximum schon erreicht ist
                    if (points % 50 == 0 && speedSnake > 100)
                    {
                        speedSnake         -= 100;
                        timerSnake.Interval = TimeSpan.FromMilliseconds(speedSnake);
                        if (speedUp)
                        {
                            oldSpeedSnake -= 100;
                        }
                    }
                    if (points % 100 == 0)
                    {
                        sound = new SoundPlayer(Properties.Resources.speedPlusOne);
                        sound.Play();
                        ProgressBarSpeed.Value += 1;
                    }
                    // einen teil hinten in der Schlange anhaengen
                    SnakeParts sPart = new SnakeParts(new Point(snake[snake.Count - 1].GetOldPosition().X, snake[snake.Count - 1].GetOldPosition().Y + snake[snake.Count - 1].GetSize()), Colors.Black);
                    snake.Add(sPart);
                    // den alten Apfel loeschen
                    myApple.RemoveApple(playground);
                    // einen neuen Apfel erzeugen
                    myApple = new Apples(appleSize);
                    myApple.ShowApple(playground, pillarWidth);
                    if (points == levelPoints && _counterLevel < 3)
                    {
                        levelPoints *= 2;
                        GameBreak();
                        sound = new SoundPlayer(Properties.Resources.nextLevel);
                        sound.Play();
                        _counterLevel++;
                        MessageBox.Show($"Glückwunsch! \nDu hast Level { _counterLevel } erreicht!", "Next Level!", MessageBoxButton.OK, MessageBoxImage.Information);
                        NextLevel();
                    }
                }
            }
        }