Example #1
0
        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            player1 = new GrandMother(Content, "Pictures/grandma", GameConstants.PLAYER_1_START_POSITION_X,
                                      GameConstants.PLAYERS_START_POSITION_Y, GameConstants.Player.One);
            player2 = new GrandMother(Content, "Pictures/grandpa", GameConstants.PLAYER_2_START_POSITION_X,
                                      GameConstants.PLAYERS_START_POSITION_Y, GameConstants.Player.Two);

            // position of score report on the canvas
            player1ScorePosition = new Vector2(GameConstants.PLAYER1_SCORE_POSITION_X, GameConstants.PLAYER1_SCORE_POSITION_Y);
            player2ScorePosition = new Vector2(GameConstants.PLAYER2_SCORE_POSITION_X, GameConstants.PLAYER2_SCORE_POSITION_Y);

            base.Initialize();
        }
Example #2
0
        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows players to control characters
            KeyboardState keyboard = Keyboard.GetState();

            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            {
                this.Exit();
            }

            // Movements support
            this.elapsedTime += gameTime.ElapsedGameTime.Milliseconds;


            // give possibility for cars spread on the road
            // playing introduction message
            if (this.elapsedIntroTime > GameConstants.PLAYERS_START_DELAY)
            {
                this.introduction.Active = false;
                this.player1.Update(gameTime, keyboard);
                this.player2.Update(gameTime, keyboard);
            }
            else
            {
                this.introduction.Active = true;
                this.showIntro();
                this.elapsedIntroTime += gameTime.ElapsedGameTime.Milliseconds;
            }

            // cars spawning
            if (this.elapsedTime > GameConstants.CARS_SPAWN_DELAY)
            {
                this.elapsedTime = 0;
                this.SpawnCar();
            }

            // updating game objects
            foreach (Car car in this.cars)
            {
                car.Update(gameTime);
            }
            foreach (Blood ink in this.bloodInks)
            {
                ink.Update(gameTime);
            }

            // check if someone win round
            if (this.player1.CollisionRactangle.Bottom < GameConstants.ROAD_OFFSET)
            {
                this.player_1_score  += GameConstants.SCORE_GET_FINISH;
                this.elapsedIntroTime = 0;
                player1 = new GrandMother(Content, "Pictures/grandma", GameConstants.PLAYER_1_START_POSITION_X,
                                          GameConstants.PLAYERS_START_POSITION_Y, GameConstants.Player.One);
                player2 = new GrandMother(Content, "Pictures/grandpa", GameConstants.PLAYER_2_START_POSITION_X,
                                          GameConstants.PLAYERS_START_POSITION_Y, GameConstants.Player.Two);
            }
            else if (this.player2.CollisionRactangle.Bottom < GameConstants.ROAD_OFFSET)
            {
                this.player_2_score  += GameConstants.SCORE_GET_FINISH;
                this.elapsedIntroTime = 0;
                player1 = new GrandMother(Content, "Pictures/grandma", GameConstants.PLAYER_1_START_POSITION_X,
                                          GameConstants.PLAYERS_START_POSITION_Y, GameConstants.Player.One);
                player2 = new GrandMother(Content, "Pictures/grandpa", GameConstants.PLAYER_2_START_POSITION_X,
                                          GameConstants.PLAYERS_START_POSITION_Y, GameConstants.Player.Two);
            }

            //check if someone was killed
            foreach (Car car in this.cars)
            {
                if (car.CollisionRactangle.Intersects(this.player1.CollisionRactangle))
                {
                    this.player_2_score += GameConstants.SCORE_COMPETITOR_DIES;
                    this.womanDieSound.Play(0.5f, 0.0f, 0.0f);
                    this.bloodInks.Add(new Blood(Content, this.player1.CollisionRactangle.Center.X,
                                                 this.player1.CollisionRactangle.Center.Y));
                    this.player1 = new GrandMother(Content, "Pictures/grandma", GameConstants.PLAYER_1_START_POSITION_X,
                                                   GameConstants.PLAYERS_START_POSITION_Y, GameConstants.Player.One);
                }
                else if (car.CollisionRactangle.Intersects(this.player2.CollisionRactangle))
                {
                    this.player_1_score += GameConstants.SCORE_COMPETITOR_DIES;
                    this.manDieSound.Play(0.5f, 0.0f, 0.0f);
                    this.bloodInks.Add(new Blood(Content, this.player2.CollisionRactangle.Center.X,
                                                 this.player2.CollisionRactangle.Center.Y));
                    this.player2 = new GrandMother(Content, "Pictures/grandpa", GameConstants.PLAYER_2_START_POSITION_X,
                                                   GameConstants.PLAYERS_START_POSITION_Y, GameConstants.Player.Two);
                }
            }

            //clean up inactive objects
            for (int i = this.cars.Count - 1; i >= 0; i--)
            {
                if (!this.cars[i].Active)
                {
                    this.cars.RemoveAt(i);
                }
            }
            for (int i = this.bloodInks.Count - 1; i >= 0; i--)
            {
                if (!this.bloodInks[i].Active)
                {
                    this.bloodInks.RemoveAt(i);
                }
            }

            base.Update(gameTime);
        }