Exemple #1
0
 private void Game_Load(object sender, EventArgs e) //This all occurs when the game is loaded. This creates and adds objects to the game.
 {
     Player     = new Character();
     Spikes     = new List <Spike>();
     Tokens     = new List <Token>();
     HealthPack = new HealthPack();
     FuelTank   = new FuelTank();
     Rocket     = new Rocket(Height, Width);
     Controls.Add(Player);
     Controls.Add(Rocket);
     Controls.Add(HealthPack);
     Controls.Add(FuelTank);
     Player.Fuel   = 100 + fuelChange;
     Player.Health = 100 + healthChange;
 }
Exemple #2
0
        private void GameTimer_Tick(object sender, EventArgs e) //All of this happens every tick (10ms)
        {
            //Rocket
            Rocket.Left -= 10;                         //Moves the Rocket every tick.
            if (Rocket.HitTest(Player.Bounds) == true) //If the rocket hits the Player:
            {
                Player.DamageByRocket();
                Rocket.Hide();
                Rocket.Shoot();
            }
            if (Rocket.Left <= 1) //If the rocket reaches the side of the form:
            {
                Rocket.Hide();
                Rocket.Shoot();
            }
            //Spikes
            int spikeRand = randomGenerator.Next(1, 100); //Should generate a spike approx twice a second.

            if (spikeRand == 1)
            {
                ActivateTopSpike();
            }
            if (spikeRand == 2)
            {
                ActivateBottomSpike();
            }
            for (int i = 0; i < Spikes.Count; i++) //Moves the spikes constantly to the left.
            {
                Spikes[i].Left -= 5 + speedChange;
            }
            for (int i = 0; i < Spikes.Count; i++)
            {
                if (Spikes[i].HitTest(Player.Bounds) == true) //If the spike hits the Player:
                {
                    Spikes[i].Hide();
                    Controls.Remove(Spikes[i]);
                    Spikes.Remove(Spikes[i]);
                    Player.DamageBySpike();
                }
                else if (Spikes[i].Left <= 1) //If the spike reaches the side of the form:
                {
                    Spikes[i].Hide();
                    Controls.Remove(Spikes[i]);
                    Spikes.Remove(Spikes[i]);
                }
            }
            //Player
            if (!isFlying) //Makes the player fall if it is not f;ying.
            {
                Player.Fall();
            }
            isFlying         = false;
            FuelLabel.Text   = Player.Fuel.ToString();   //Makes the FuelLabel work.
            HealthLabel.Text = Player.Health.ToString(); //Makes the HealthLabel work.
            if (Player.Health == 0)                      //Ends the game if Health reaches zero.
            {
                GameTimer.Enabled = false;
                MessageBox.Show("Game Over!");
                Application.Exit();
            }
            //HealthPack
            HealthPack.MoveHealthPack();                                 //Moves the healthpack every tick.
            if (HealthPack.HitTest(Player.Bounds) && HealthPack.Visible) //If the healthpack hits the player:
            {
                Player.IncreaseHealth(healthChange);
                HealthPack.ResetHealth();
            }
            else if (HealthPack.Left <= 1) //If the healthpack reaches the side of the form:
            {
                HealthPack.ResetHealth();
            }
            //FuelTank
            FuelTank.MoveFuelTank();                                 //Moves the fueltank every tick.
            if (FuelTank.HitTest(Player.Bounds) && FuelTank.Visible) //If the fueltank hits the player:
            {
                Player.IncreaseFuel(fuelChange);
                FuelTank.ResetFuel();
            }
            else if (FuelTank.Left <= 1) //If the fueltank reaches the side of the form:
            {
                FuelTank.ResetFuel();
            }
            //Token
            int tokenRand = randomGenerator.Next(1, 100 - tokenChange);

            if (tokenRand == 1) //Should generate one token approx every second.
            {
                ActivateToken();
            }
            for (int i = 0; i < Tokens.Count; i++)
            {
                Tokens[i].MoveToken();                        //Moves each token every tick.
                if (Tokens[i].HitTest(Player.Bounds) == true) //If a token hits the player:
                {
                    Tokens[i].Hide();
                    Controls.Remove(Tokens[i]);
                    Tokens.Remove(Tokens[i]);
                    score += 500;
                }
                else if (Tokens[i].Left <= 1) //If the token reaches the left side of the screen:
                {
                    Tokens[i].Hide();
                    Controls.Remove(Tokens[i]);
                    Tokens.Remove(Tokens[i]);
                }
            }
            //Misc
            ScoreLabel.Text = score.ToString(); //Makes the score label work.
            score++;                            //Increases score every tick.
        }