コード例 #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()
        {
            graphics.PreferredBackBufferWidth  = Constants.WindowWidth;
            graphics.PreferredBackBufferHeight = Constants.WindowHeight;
            graphics.ApplyChanges();
            drawController = new DrawController();
            base.Initialize();

            string[] path = { "Content", "Levels", "Level_1.txt" };
            drawController.CreateMap(Path.Combine(path));
        }
コード例 #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)
        {
            if (Keyboard.GetState().IsKeyDown(Keys.Escape))
            {
                Exit();
            }

            if (Keyboard.GetState().IsKeyDown(Keys.R))
            {
                string[] path = { "Content", "Levels", "Level_1.txt" };
                drawController.CreateMap(Path.Combine(path));
            }

            Vector2 position = Vector2.Zero;

            var kstate = Keyboard.GetState();

            if (kstate.IsKeyDown(Keys.Up) && previousKeyState != kstate)
            {
                position.Y -= Constants.FieldCellHeight;
            }

            if (kstate.IsKeyDown(Keys.Down) && previousKeyState != kstate)
            {
                position.Y += Constants.FieldCellHeight;
            }

            if (kstate.IsKeyDown(Keys.Left) && previousKeyState != kstate)
            {
                position.X -= Constants.FieldCellWidth;
            }

            if (kstate.IsKeyDown(Keys.Right) && previousKeyState != kstate)
            {
                position.X += Constants.FieldCellWidth;
            }

            previousKeyState = Keyboard.GetState();

            if (drawController.Map != null)
            {
                foreach (var gameElement in drawController.Map)
                {
                    var player = gameElement as IDynamic;
                    if ((player as IGameElement)?.Texture == Constants.RobotTexture)
                    {
                        player?.Move(position, drawController);
                    }
                }
            }

            base.Update(gameTime);
        }