コード例 #1
0
 protected override void Initialize()
 {
     player = new Player();
         enemy1 = new Enemies();
         enemies = new List<Enemies>();
         lastEnemy = 0;
     bullet = new Projectiles();
     bullets = new List<Projectiles>();
     cooldown = 500;
     enemyCooldown = 250;
     lastShot = 0;
     lastHit = 0;
     lastReload = player.rsMachinegun*2;
     amountPerWave = 10;
     totalEnemiesCount = 0;
     wave = 1;
     currentWeapon = 1;
     healthColor = Color.LightGreen;
     bulletColor = Color.White;
     
     //player.profession = "Vampire";
     base.Initialize();
 }
コード例 #2
0
        protected override void Update(GameTime gameTime)
        {


            KeyboardState pressedKeys = Keyboard.GetState();
            if (pressedKeys.IsKeyDown(Keys.P))
            {
                paused = true;
            }
            if (pressedKeys.IsKeyDown(Keys.P) && pressedKeys.IsKeyDown(Keys.LeftShift))
            {
                paused = false;
            }
            if (!paused)
            {
                lastShot += (float)gameTime.ElapsedGameTime.TotalMilliseconds;
                lastEnemy += (float)gameTime.ElapsedGameTime.TotalMilliseconds;
                lastHit += (float)gameTime.ElapsedGameTime.TotalMilliseconds;
                lastReload += (float)gameTime.ElapsedGameTime.TotalMilliseconds;
                


                #region Waving
                for (int i = 0; i < amountPerWave; i++)
                {
                    if (lastEnemy > enemyCooldown && enemies.Count < amountPerWave && totalEnemiesCount < amountPerWave)
                    {
                        Random rnd = new Random();
                        Enemies newEnemy = new Enemies();
                        newEnemy.speed = rnd.Next(1,4);
                        newEnemy.sprite = Content.Load<Texture2D>("enemy");
                        enemies.Add(newEnemy);
                        lastEnemy = 0;
                        totalEnemiesCount++;
                        newEnemy.position = new Vector2(rnd.Next(-2500, 2500), rnd.Next(-2500, 2500));
                    }
                }
                if (totalEnemiesCount > 0 && enemies.Count == 0)
                {
                    totalEnemiesCount = 0;
                    amountPerWave += 5;
                    wave++;
                    player.health += 10;
                }
                #endregion

                #region Movement
                if (pressedKeys.IsKeyDown(Keys.W))
                {
                    player.velocity.Y = -player.speed;
                }
                else if (pressedKeys.IsKeyDown(Keys.S))
                {
                    player.velocity.Y = player.speed;
                }
                else
                    player.velocity.Y = 0;
                if (pressedKeys.IsKeyDown(Keys.D))
                {
                    player.velocity.X = player.speed;
                }
                else if (pressedKeys.IsKeyDown(Keys.A))
                {
                    player.velocity.X = -player.speed;
                }
                else player.velocity.X = 0;
                #endregion

                #region Bullet control
                if (currentWeapon == 1) //Gun
                {
                    if (lastShot > player.gunCooldown && player.gunAmmo > 0 && lastReload > player.rsGun)
                    {
                        if (pressedKeys.IsKeyDown(Keys.Left))
                        {

                            bullet = new Projectiles(player.position.X + 10, player.position.Y + 10, -bullet.speed, 0, currentWeaponDamage);
                            bullet.sprite = Content.Load<Texture2D>("revolverBullet_left");
                            bullets.Add(bullet);
                            player.gunAmmo--;
                            lastShot = 0;
                        }


                        else if (pressedKeys.IsKeyDown(Keys.Right))
                        {

                            bullet = new Projectiles(player.position.X + 10, player.position.Y + 10, bullet.speed, 0, currentWeaponDamage);
                            bullet.sprite = Content.Load<Texture2D>("revolverBullet_right");
                            bullets.Add(bullet);
                            player.gunAmmo--;
                            lastShot = 0;
                        }

                        else if (pressedKeys.IsKeyDown(Keys.Up))
                        {

                            bullet = new Projectiles(player.position.X + 10, player.position.Y + 10, 0, -bullet.speed, currentWeaponDamage);
                            bullet.sprite = Content.Load<Texture2D>("revolverBullet_up");
                            bullets.Add(bullet);
                            player.gunAmmo--;
                            lastShot = 0;


                        }

                        else if (pressedKeys.IsKeyDown(Keys.Down))
                        {

                            bullet = new Projectiles(player.position.X + 10, player.position.Y + 10, 0, bullet.speed, currentWeaponDamage);
                            bullet.sprite = Content.Load<Texture2D>("revolverBullet_down");
                            bullets.Add(bullet);
                            player.gunAmmo--;
                            lastShot = 0;
                        }
                    }
                }
                else if (currentWeapon == 2) //Shotgun
                {
                    Random rnd = new Random();
                    if (lastShot > player.shotgunCooldown && player.shotgunAmmo > 0 && lastReload > player.rsShotgun)
                    {
                        if (pressedKeys.IsKeyDown(Keys.Left))
                        {

                            bullet = new Projectiles(player.position.X + 10, player.position.Y + 10, -bullet.speed, 0, currentWeaponDamage);
                            bullet.sprite = Content.Load<Texture2D>("shotgunBullet");
                            bullets.Add(bullet);
                            bullet = new Projectiles(player.position.X + 10, player.position.Y + 10, -bullet.speed, 5, currentWeaponDamage);
                            bullet.sprite = Content.Load<Texture2D>("shotgunBullet");
                            bullets.Add(bullet);
                            bullet = new Projectiles(player.position.X + 10, player.position.Y + 10, -bullet.speed, -5, currentWeaponDamage);
                            bullet.sprite = Content.Load<Texture2D>("shotgunBullet");
                            bullets.Add(bullet);
                            bullet = new Projectiles(player.position.X + 10, player.position.Y + 10, -bullet.speed, 2, currentWeaponDamage);
                            bullet.sprite = Content.Load<Texture2D>("shotgunBullet");
                            bullets.Add(bullet);
                            bullet = new Projectiles(player.position.X + 10, player.position.Y + 10, -bullet.speed, -2, currentWeaponDamage);
                            bullet.sprite = Content.Load<Texture2D>("shotgunBullet");
                            bullets.Add(bullet);
                            player.shotgunAmmo--;
                            lastShot = 0;

                        }


                        else if (pressedKeys.IsKeyDown(Keys.Right))
                        {

                            bullet = new Projectiles(player.position.X + 10, player.position.Y + 10, bullet.speed, 0, currentWeaponDamage);
                            bullet.sprite = Content.Load<Texture2D>("shotgunBullet");
                            bullets.Add(bullet);
                            bullet = new Projectiles(player.position.X + 10, player.position.Y + 10, bullet.speed, bullet.speed / 2, currentWeaponDamage);
                            bullet.sprite = Content.Load<Texture2D>("shotgunBullet");
                            bullets.Add(bullet);
                            bullet = new Projectiles(player.position.X + 10, player.position.Y + 10, bullet.speed, -bullet.speed / 2, currentWeaponDamage);
                            bullet.sprite = Content.Load<Texture2D>("shotgunBullet");
                            bullets.Add(bullet);
                            bullet = new Projectiles(player.position.X + 10, player.position.Y + 10, bullet.speed, 2, currentWeaponDamage);
                            bullet.sprite = Content.Load<Texture2D>("shotgunBullet");
                            bullets.Add(bullet);
                            bullet = new Projectiles(player.position.X + 10, player.position.Y + 10, bullet.speed, -2, currentWeaponDamage);
                            bullet.sprite = Content.Load<Texture2D>("shotgunBullet");
                            bullets.Add(bullet);
                            player.shotgunAmmo--;
                            lastShot = 0;
                        }

                        else if (pressedKeys.IsKeyDown(Keys.Up))
                        {

                            bullet = new Projectiles(player.position.X + 10, player.position.Y + 10, 0, -bullet.speed, currentWeaponDamage);
                            bullet.sprite = Content.Load<Texture2D>("shotgunBullet");
                            bullets.Add(bullet);
                            bullet = new Projectiles(player.position.X + 10, player.position.Y + 10, bullet.speed / 2, -bullet.speed, currentWeaponDamage);
                            bullet.sprite = Content.Load<Texture2D>("shotgunBullet");
                            bullets.Add(bullet);
                            bullet = new Projectiles(player.position.X + 10, player.position.Y + 10, -bullet.speed / 2, -bullet.speed, currentWeaponDamage);
                            bullet.sprite = Content.Load<Texture2D>("shotgunBullet");
                            bullets.Add(bullet);
                            bullet = new Projectiles(player.position.X + 10, player.position.Y + 10, 2, -bullet.speed, currentWeaponDamage);
                            bullet.sprite = Content.Load<Texture2D>("shotgunBullet");
                            bullets.Add(bullet);
                            bullet = new Projectiles(player.position.X + 10, player.position.Y + 10, -2, -bullet.speed, currentWeaponDamage);
                            bullet.sprite = Content.Load<Texture2D>("shotgunBullet");
                            bullets.Add(bullet);
                            player.shotgunAmmo--;
                            lastShot = 0;


                        }

                        else if (pressedKeys.IsKeyDown(Keys.Down))
                        {

                            bullet = new Projectiles(player.position.X + 10, player.position.Y + 10, 0, bullet.speed, currentWeaponDamage);
                            bullet.sprite = Content.Load<Texture2D>("shotgunBullet");
                            bullets.Add(bullet);
                            bullet = new Projectiles(player.position.X + 10, player.position.Y + 10, bullet.speed / 2, bullet.speed, currentWeaponDamage);
                            bullet.sprite = Content.Load<Texture2D>("shotgunBullet");
                            bullets.Add(bullet);
                            bullet = new Projectiles(player.position.X + 10, player.position.Y + 10, -bullet.speed / 2, bullet.speed, currentWeaponDamage);
                            bullet.sprite = Content.Load<Texture2D>("shotgunBullet");
                            bullets.Add(bullet);
                            bullet = new Projectiles(player.position.X + 10, player.position.Y + 10, 2, bullet.speed, currentWeaponDamage);
                            bullet.sprite = Content.Load<Texture2D>("shotgunBullet");
                            bullets.Add(bullet);
                            bullet = new Projectiles(player.position.X + 10, player.position.Y + 10, -2, bullet.speed, currentWeaponDamage);
                            bullet.sprite = Content.Load<Texture2D>("shotgunBullet");
                            bullets.Add(bullet);
                            player.shotgunAmmo--;
                            lastShot = 0;
                        }
                    }
                }
                if (currentWeapon == 3) // Machinegun
                {
                    if (lastShot > player.machinegunCooldown && player.machinegunAmmo > 0 && lastReload > player.rsMachinegun)
                    {
                        Random rnd = new Random();
                        if (pressedKeys.IsKeyDown(Keys.Left))
                        {
                            bullet = new Projectiles(player.position.X + 10, player.position.Y + 10, -bullet.speed, rnd.Next(-1, 2), currentWeaponDamage);
                            bullet.sprite = Content.Load<Texture2D>("machinegunBullet_left");
                            bullets.Add(bullet);
                            player.machinegunAmmo--;
                            lastShot = 0;
                        }


                        else if (pressedKeys.IsKeyDown(Keys.Right))
                        {

                            bullet = new Projectiles(player.position.X + 10, player.position.Y + 10, bullet.speed, rnd.Next(-1, 2), currentWeaponDamage);
                            bullet.sprite = Content.Load<Texture2D>("machinegunBullet_right");
                            bullets.Add(bullet);
                            player.machinegunAmmo--;
                            lastShot = 0;
                        }

                        else if (pressedKeys.IsKeyDown(Keys.Up))
                        {

                            bullet = new Projectiles(player.position.X + 10, player.position.Y + 10, rnd.Next(-1, 2), -bullet.speed, currentWeaponDamage);
                            bullet.sprite = Content.Load<Texture2D>("machinegunBullet_up");
                            bullets.Add(bullet);
                            player.machinegunAmmo--;
                            lastShot = 0;


                        }

                        else if (pressedKeys.IsKeyDown(Keys.Down))
                        {

                            bullet = new Projectiles(player.position.X + 10, player.position.Y + 10, rnd.Next(-1, 2), bullet.speed, currentWeaponDamage);
                            bullet.sprite = Content.Load<Texture2D>("machinegunBullet_down");
                            bullets.Add(bullet);
                            player.machinegunAmmo--;
                            lastShot = 0;
                        }
                    }
                }
                if (currentWeapon == 1)
                {
                    //Måla ut rätt sprite
                    currentWeaponDamage = player.gunDamage;
                }
                if (currentWeapon == 2)
                {
                    //Måla ut rätt sprite
                    currentWeaponDamage = player.shotgunDamage;
                }
                if (currentWeapon == 3)
                {
                    //Måla ut rätt sprite
                    currentWeaponDamage = player.machinegunDamage;
                }
                #endregion

                #region Weapon control

                if (pressedKeys.IsKeyDown(Keys.D1))
                {

                    currentWeapon = 1;
                }
                if (pressedKeys.IsKeyDown(Keys.D2))
                {

                    currentWeapon = 2;
                }
                if (pressedKeys.IsKeyDown(Keys.D3))
                {

                    currentWeapon = 3;
                }
                if (pressedKeys.IsKeyDown(Keys.R))
                {
                    if(currentWeapon == 1 && player.gunAmmo < player.gunAmmoMax)
                    {
                        player.gunAmmo = player.gunAmmoMax;
                        lastReload = 0;
                    }
                    else if (currentWeapon == 2 && player.shotgunAmmo < player.shotgunAmmoMax)
                    {
                        player.shotgunAmmo = player.shotgunAmmoMax;
                        lastReload = 0;
                    }
                    else if (currentWeapon == 3 && player.gunAmmo < player.machinegunAmmoMax)
                    {
                        player.machinegunAmmo = player.machinegunAmmoMax;
                        lastReload = 0;
                    }

                }




                #endregion

                #region Progression!
                //if (player.experiencePoints == 10)
                //{

                //    if (player.gunCooldown > 410)
                //    {
                //        player.gunCooldown -= 10;
                //    }
                //    player.experiencePoints = 0;
                //    player.level++;
                //}
                #endregion

                #region Professions control

                //if(player.profession == "Vampire") 
                //{
                //    if (pressedKeys.IsKeyDown(Keys.Space) && player.health < 100 && player.manaPoints > 5)
                //    {
                //        player.health += 25;
                //        player.manaPoints -= 5;

                //    }
                //}
                #endregion


                player.Update();


                foreach (Enemies enemyUpdate in enemies)
                {
                    enemyUpdate.Update(player.position);
                }

                foreach (Projectiles bulletUpdate in bullets)
                {
                    bulletUpdate.Update();
                }

                for (int i = 0; i < bullets.Count; i++)
                {

                    for (int j = 0; j < enemies.Count; j++)
                    {


                        if (bullets[i].Hitbox.Intersects(enemies[j].Hitbox))
                        {

                            enemies[j].health -= bullets[i].damage;
                            bullets.RemoveAt(i);
                            player.experiencePoints++;
                            if (bullets.Count == 0)
                                goto outOfLoop;
                            else if (i != 0) {
                                i--;
                            }
                        }
                    }
                }
            outOfLoop:

                for (int i = 0; i < enemies.Count; i++)
                {
                    if (player.Hitbox.Intersects(enemies[i].Hitbox))
                    {
                        if (lastHit > cooldown)
                        {
                            player.health -= 10;

                            lastHit = 0;
                        }
                    }
                }

               
                    if (player.health == 0 || player.health < 0)
                {
                    Game newGame = new Game1();
                    newGame.Run();
                    Environment.Exit(0);
                }
                for (int i = 0; i < enemies.Count; i++)
                {
                    if (enemies[i].health == 0 || enemies[i].health < 0)
                    {
                        enemies.RemoveAt(i);
                    }
                }

                for(int i = 0; i < enemies.Count; i++)
                {
                    if(enemies[i].position.X < player.position.X)
                    {
                        enemies[i].sprite = Content.Load<Texture2D>("enemy");
                    }
                    else
                        enemies[i].sprite = Content.Load<Texture2D>("enemyLeft");
                }
                base.Update(gameTime);
            }
        }