示例#1
0
        private void InitBonuses(int count)
        {
            for (int i = 0; i < count; i++)
            {
                Bonus bonus = new Bonus();
                bonus.Radius   = 10;
                bonus.Position = new Point(
                    rnd.Next(bonus.Radius, windowWidth - bonus.Radius),
                    rnd.Next(bonus.Radius, windowHeight - bonus.Radius));

                gameObjects.Add(bonus);

                if (i == 5)
                {
                    SuperBonus superBonus = new SuperBonus();
                    superBonus.Radius   = 5;
                    superBonus.Position = new Point(
                        rnd.Next(superBonus.Radius, windowWidth - superBonus.Radius),
                        rnd.Next(superBonus.Radius, windowHeight - superBonus.Radius));

                    gameObjects.Add(superBonus);
                }

                else if (i == 7)
                {
                    SpeedBonus speedBonus = new SpeedBonus();
                    speedBonus.Radius   = 15;
                    speedBonus.Position = new Point(
                        rnd.Next(speedBonus.Radius, windowWidth - speedBonus.Radius),
                        rnd.Next(speedBonus.Radius, windowHeight - speedBonus.Radius));

                    gameObjects.Add(speedBonus);
                }

                else if (i == 8)
                {
                    EvilBonus evilBonus = new EvilBonus();
                    evilBonus.Radius   = 10;
                    evilBonus.Position = new Point(
                        rnd.Next(evilBonus.Radius, windowWidth - evilBonus.Radius),
                        rnd.Next(evilBonus.Radius, windowHeight - evilBonus.Radius));

                    gameObjects.Add(evilBonus);
                }
            }
        }
示例#2
0
        private void GameTick(object sender, EventArgs e)
        {
            Snake snake = gameObjects[0] as Snake;

            // update all game objects
            Bonus      hitBonus      = null;
            SuperBonus hitSuperBonus = null;
            SpeedBonus hitSpeedBonus = null;

            foreach (IGameObject gameObject in gameObjects)
            {
                gameObject.Update();

                // check hits to bonuses
                Bonus      bonus      = gameObject as Bonus;
                SuperBonus superBonus = gameObject as SuperBonus;
                SpeedBonus speedBonus = gameObject as SpeedBonus;
                EvilBonus  evilBonus  = gameObject as EvilBonus;
                if (bonus != null)
                {
                    if (Math.Abs(bonus.Position.X - snake.Position.X) < bonus.Radius + snake.Radius &&
                        Math.Abs(bonus.Position.Y - snake.Position.Y) < bonus.Radius + snake.Radius)
                    {
                        // its a hit
                        // store the bonus that hits with the snake
                        hitBonus = bonus;
                    }
                }

                if (superBonus != null)
                {
                    if (Math.Abs(superBonus.Position.X - snake.Position.X) < superBonus.Radius + snake.Radius &&
                        Math.Abs(superBonus.Position.Y - snake.Position.Y) < superBonus.Radius + snake.Radius)
                    {
                        // its a hit
                        // store the bonus that hits with the snake
                        hitSuperBonus = superBonus;
                    }
                }

                if (speedBonus != null)
                {
                    if (Math.Abs(speedBonus.Position.X - snake.Position.X) < speedBonus.Radius + snake.Radius &&
                        Math.Abs(speedBonus.Position.Y - snake.Position.Y) < speedBonus.Radius + snake.Radius)
                    {
                        // its a hit
                        // store the bonus that hits with the snake
                        hitSpeedBonus = speedBonus;
                    }
                }

                if (evilBonus != null)
                {
                    if (Math.Abs(evilBonus.Position.X - snake.Position.X) < evilBonus.Radius + snake.Radius + 5 &&
                        Math.Abs(evilBonus.Position.Y - snake.Position.Y) < evilBonus.Radius + snake.Radius + 5)
                    {
                        // bonus will run away
                        evilBonus.Position = new Point(evilBonus.Position.Y + 10, evilBonus.Position.X + 10);
                    }
                }
            }

            if (hitBonus != null)
            {
                gameObjects.Remove(hitBonus);
                score++;
                scoreText.Text = "SCORE: " + score;
                snake.IncLength(10);

                if (gameObjects.Count < 30)
                {
                    InitBonuses(10);
                }
            }

            if (hitSuperBonus != null)
            {
                gameObjects.Remove(hitSuperBonus);
                score         += 10;
                scoreText.Text = "SCORE: " + score;
                snake.IncLength(10);

                if (gameObjects.Count < 30)
                {
                    InitBonuses(10);
                }
            }

            if (hitSpeedBonus != null)
            {
                gameObjects.Remove(hitSpeedBonus);
                score         += 50;
                scoreText.Text = "SCORE: " + score;
                snake.IncLength(10);
                snake.IncSpeed(1);
                if (gameObjects.Count < 30)
                {
                    InitBonuses(10);
                }
            }

            // check game over condition
            if (snake.Position.X < snake.Radius || snake.Position.X > windowWidth - snake.Radius ||
                snake.Position.Y < snake.Radius || snake.Position.Y > windowHeight - snake.Radius ||
                snake.HitsItself())
            {
                GameOver();
            }


            // draw game
            var drawingContext = backingStore.Open();

            Render(drawingContext);
            drawingContext.Close();
        }