Esempio n. 1
0
        public override void Update(ComputerUI parent, Rectangle screenBounds)
        {
            Main.blockInput = true;

            if (snake.segments.Count >= 390)
            {
                gameOver = true;

                endText = "You win!";
            }

            if (PressedExit || (gameOver && countToBeginning > 120))
            {
                parent.StopVideoGame();
            }

            if (countToBeginning < 120 && !gameOver)
            {
                countToBeginning++;
            }
            else if (countToBeginning >= 120 && !gameOver)
            {
                gameTimer++;

                Vector2 move = GetMoveDir();

                if (lastMove == Vector2.Zero && move == Vector2.UnitY)
                {
                    move = lastMove;
                }

                if (move != lastMove)
                {
                    lastMove = move;
                }

                if (gameTimer % 12 == 0)
                {
                    if (move != Vector2.Zero)
                    {
                        snakeSegmentsCache.Insert(0, snake.segments[0]);

                        if (snakeSegmentsCache.Count > snake.segments.Count)
                        {
                            snakeSegmentsCache.RemoveAt(snakeSegmentsCache.Count - 1);
                        }

                        snake.segments[0] += move * 10;

                        for (int i = 1; i < snakeSegmentsCache.Count - 1; i++)
                        {
                            snake.segments[i] = snakeSegmentsCache[i];
                        }

                        snakeSegmentsCache.RemoveAt(snakeSegmentsCache.Count - 1);
                    }
                }

                if (snakeFood == null && move != Vector2.Zero)
                {
                    Vector2 possiblePos = Main.rand.Next(possibleFoodSpawns);

                    while (new Rectangle((int)possiblePos.X, (int)possiblePos.Y, 10, 10).Intersects(new Rectangle((int)snake.segments[0].X, (int)snake.segments[0].Y, 10, 10)))
                    {
                        possiblePos = Main.rand.Next(possibleFoodSpawns);
                    }

                    snakeFood = new SnakeFood(possiblePos + screenBounds.GetPos() + offset);
                }

                if (snakeFood != null && new Rectangle((int)snakeFood.position.X, (int)snakeFood.position.Y, 10, 10).Intersects(new Rectangle((int)snake.segments[0].X, (int)snake.segments[0].Y, 10, 10)))
                {
                    score++;

                    snakeFood = null;

                    snake.segments.Add(snakeSegmentsCache[snakeSegmentsCache.Count - 1]);
                }

                if (!GetBounds(screenBounds).Contains(snake.segments[0].ToPoint()) || AnySegmentsAtSeg0())
                {
                    gameOver = true;

                    countToBeginning = 0;
                }
            }

            if (gameOver)
            {
                countToBeginning++;
            }

            oldKeyState = Key;
        }
Esempio n. 2
0
        public override void Update(ComputerUI parent, Rectangle screenBounds)
        {
            Rectangle actualBounds = new Rectangle((int)offset.X, (int)offset.Y, 320, 150);

            Main.blockInput = true;

            if (PressedExit || (gameOver && countToBeginning > 120))
            {
                parent.StopVideoGame();
            }

            if (countToBeginning < 120 && !gameOver)
            {
                countToBeginning++;
            }
            else if (countToBeginning >= 120 && !gameOver)
            {
                if (delay > 0)
                {
                    delay--;
                }
                else
                {
                    if (MovedUp && yourPaddle.position.Y > actualBounds.Y)
                    {
                        yourPaddle.position.Y -= paddleSpeed;
                    }
                    else if (MovedDown && yourPaddle.position.Y + 40 < actualBounds.Y + actualBounds.Height)
                    {
                        yourPaddle.position.Y += paddleSpeed;
                    }

                    if (ball.position.X > actualBounds.X + (actualBounds.Width / 2))
                    {
                        AIPaddle(actualBounds);
                    }

                    Rectangle yourGoal = new Rectangle(actualBounds.X, actualBounds.Y, 8, 150);
                    Rectangle aiGoal   = new Rectangle(actualBounds.X + actualBounds.Width - 8, actualBounds.Y, 8, 150);

                    if (ball.Hitbox.Intersects(yourGoal))
                    {
                        aiScore++;

                        BoardReset(screenBounds);
                    }
                    else if (ball.Hitbox.Intersects(aiGoal))
                    {
                        yourScore++;

                        BoardReset(screenBounds);
                    }

                    if (ball.Hitbox.Intersects(yourPaddle.Hitbox))
                    {
                        MakePositive(ref ball.velocity.X);
                    }
                    else if (ball.Hitbox.Intersects(aiPaddle.Hitbox))
                    {
                        MakeNegative(ref ball.velocity.X);
                    }

                    if (ball.position.Y < actualBounds.Y || ball.position.Y + 10 > actualBounds.Y + actualBounds.Height)
                    {
                        ball.velocity.Y *= -1;
                    }

                    ball.position += ball.velocity;
                }
            }

            if (yourScore == 10 || aiScore == 10)
            {
                gameOver = true;

                countToBeginning = 0;
            }

            if (gameOver)
            {
                countToBeginning++;
            }

            oldKeyState = Key;
        }