示例#1
0
        public void LoadAlienShooterState()
        {
            using (IsolatedStorageFile gameStorage = IsolatedStorageFile.GetUserStoreForApplication())
            {
                //Initialize all objects as before
                enemies = new List <AlienGameObject>();
                for (int i = 0; i < maxEnemies; i++)
                {
                    enemies.Add(new AlienGameObject(alienShooterSpriteSheet, "spaceship", screenRect));
                }

                //Initialize Player Object
                heroShip          = new UserGameObject(alienShooterSpriteSheet, "heroship", screenRect, maxMissiles);
                heroShip.Position = new Vector2(screenRect.Width / 2, 720);
                heroShip.LoadContent(content);

                //Initialize Status Board
                statusBoard = new GameStatusBoard(gameFont);
                statusBoard.LoadContent(content);

                //Set saved state on objects
                if (gameStorage.FileExists(AlienShooterStateFileName))
                {
                    using (IsolatedStorageFileStream fs =
                               gameStorage.OpenFile(AlienShooterStateFileName, System.IO.FileMode.Open))
                    {
                        using (StreamReader streamReader = new StreamReader(fs))
                        {
                            heroShip.Position = new Vector2(
                                (float)Convert.ToDouble(streamReader.ReadLine()),
                                (float)Convert.ToDouble(streamReader.ReadLine()));
                            heroShip.Velocity = new Vector2(
                                (float)Convert.ToDouble(streamReader.ReadLine()),
                                (float)Convert.ToDouble(streamReader.ReadLine()));
                            statusBoard.Score = Convert.ToInt32(streamReader.ReadLine());
                            statusBoard.Lives = Convert.ToInt32(streamReader.ReadLine());
                            for (int i = 0; i < maxEnemies; i++)
                            {
                                enemies[i].Alive    = Convert.ToBoolean(streamReader.ReadLine());
                                enemies[i].Position = new Vector2(
                                    (float)Convert.ToDouble(streamReader.ReadLine()),
                                    (float)Convert.ToDouble(streamReader.ReadLine()));
                                enemies[i].Velocity = new Vector2(
                                    (float)Convert.ToDouble(streamReader.ReadLine()),
                                    (float)Convert.ToDouble(streamReader.ReadLine()));
                            }
                            streamReader.Close();
                        }
                    }
                }
            }
        }
        /// <summary>
        /// Load graphics content for the game.
        /// </summary>
        public override void LoadContent()
        {
            if (content == null)
            {
                content = new ContentManager(ScreenManager.Game.Services, "Content");
            }

            gameFont = content.Load <SpriteFont>("gamefont");

            alienShooterSpriteSheet = content.Load <SpriteSheet>("Sprites/AlienShooterSpriteSheet");

            backgroundTexture  = content.Load <Texture2D>("Textures/background");
            backgroundPosition = new Vector2(0, 34);

            //Get a pointer to the entire screen Rectangle
            screenRect = ScreenManager.GraphicsDevice.Viewport.Bounds;

            //Initialize Enemies collection
            enemies = new List <AlienGameObject>();
            for (int i = 0; i < maxEnemies; i++)
            {
                enemies.Add(new AlienGameObject(alienShooterSpriteSheet, "spaceship", screenRect));
            }

            //Initialize Player Object
            heroShip          = new UserGameObject(alienShooterSpriteSheet, "heroship", screenRect, maxMissiles);
            heroShip.Position = new Vector2(screenRect.Width / 2, 720);
            heroShip.LoadContent(content);

            //Initialize Status Board
            statusBoard = new GameStatusBoard(gameFont);
            statusBoard.LoadContent(content);

            // A real game would probably have more content than this sample, so
            // it would take longer to load. We simulate that by delaying for a
            // while, giving you a chance to admire the beautiful loading screen.
            Thread.Sleep(1000);

            // once the load has finished, we use ResetElapsedTime to tell the game's
            // timing mechanism that we have just finished a very long frame, and that
            // it should not try to catch up.
            ScreenManager.Game.ResetElapsedTime();
        }