Example #1
0
 //overloaded method for scenery
 public void Collision(Scenery obj)
 {
     if (IsActive == true)
     {
         // Subtract the health from a general GameObject value without specifying because all GameObjects will have a health
         if (this.Rect.Intersects(obj.ObjPos)) // Property for game object rectangle
         {
             // Use property and subtract 10 health from that
             obj.Health = obj.Health - 10;
             this.IsActive = false;
         }
     }
 }
Example #2
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()
        {
            // TODO: Add your initialization logic here

            // create player
            //user = new Player(playerImg, 0, 0, 50, 100, 100);
            user = new Player(playerImg, 0, 0, HERO_WIDTH, HERO_HEIGHT, 100);
            bulletExist = false;
            rgen = new Random();

            objCount = 0;

            // initialize the lists for the enemies
            enemyObj = new List<Enemy>();
            SceneryColl = new List<Scenery>();
            SceneryConColl = new List<Array>();
            EnemyBullets = new List<Bullet>();

            // initialize the arrays for highscores
            highScores = new List<int>();

            //fill the first five spots in the list to prevent compiler error when showing scores
            for (int i = 0; i < 5; i++)
            {
                highScores.Add(0);
            }

            // read from file
            if (File.Exists("map.dat"))
            {
                FileStream str = new FileStream("map.dat", FileMode.Open);
                long lineCount = str.Length;
                str.Close();

                using (BinaryReader reader = new BinaryReader(File.Open("map.dat", FileMode.Open)))
                {
                    for (int j = 0; j < (lineCount / 28); j++)
                    {
                        int[] n = new int[7];

                        for (int k = 0; k < n.Length; k++)
                        {
                            n[k] = reader.ReadInt32();
                        }

                        SceneryConColl.Add(n);
                    }
                }
            }

            // get background from file
            foreach (var objct in SceneryConColl)
            {
                objCount++;
                int[] obj = (int[])objct;
                Scenery newObj = new Scenery(obj[0], obj[1], obj[2], obj[3], obj[4], obj[5], obj[6]);
                SceneryColl.Add(newObj);
            }

            // intial state of the game
            gameState = GameState.Menu;
            base.Initialize();

        }