XNA项目基础

using System;

using System.Collections.Generic;

using System.Linq;

using Microsoft.Xna.Framework;

using Microsoft.Xna.Framework.Audio;

using Microsoft.Xna.Framework.Content;

using Microsoft.Xna.Framework.GamerServices;

using Microsoft.Xna.Framework.Graphics;

using Microsoft.Xna.Framework.Input;

using Microsoft.Xna.Framework.Media;

namespace WindowsGame1

{

    public class Game1 : Microsoft.Xna.Framework.Game

    {

        GraphicsDeviceManager graphics;

        SpriteBatch spriteBatch;

 

        SpriteFont FontofTimesNewRoman;

        Model model;

        Texture2D  texture;

 

        public Game1()

        {

            graphics = new GraphicsDeviceManager(this);

            //屏幕控制

            this.graphics.PreferredBackBufferWidth = 1024;

            this.graphics.PreferredBackBufferHeight = 768;

            this.graphics.IsFullScreen = true;

 

            Content.RootDirectory = "Content";

        }

        protected override void Initialize()

        {

            this.IsMouseVisible = true;//鼠标显示

            this.Window.AllowUserResizing = true;//允许窗口最大化

            base.Initialize();

        }

        protected override void LoadContent()

        {

            spriteBatch = new SpriteBatch(GraphicsDevice);

            //加载content中的资源

            model = Content.Load<Model>("model");

            FontofTimesNewRoman = Content.Load<SpriteFont>("FontofTimesNewRoman");

            texture = Content.Load<Texture2D>("texture");

        }

        protected override void UnloadContent()

        {

        }

        protected override void Update(GameTime gameTime)

        {

            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)

                this.Exit();

 

            //ESC退出

            if (Keyboard.GetState().IsKeyDown(Keys.Escape))

                this.Exit();

 

             //键盘控制 退出全屏

            KeyboardState keyb = Keyboard.GetState();

            if (keyb.IsKeyDown(Keys.A))

            {

                this.graphics.ToggleFullScreen();

            }

 

            //鼠标控制 退出全屏

            //Mouse.GetState().LeftButton==ButtonState.Pressed

            MouseState mouse = Mouse.GetState();

            if (mouse.LeftButton == ButtonState.Pressed)

            {

                this.graphics.ToggleFullScreen();

            }

 

            base.Update(gameTime);

        }

        protected override void Draw(GameTime gameTime)

        {

            GraphicsDevice.Clear(Color.CornflowerBlue);

 

           

            spriteBatch.Begin();

            //图片显示

            spriteBatch.Draw(texture, new Vector2(0, 0), Color.White);

 

            //字体显示

            string str = "times:" + gameTime.TotalGameTime.Seconds.ToString();

            Vector2 ms = FontofTimesNewRoman.MeasureString(str);

            Vector2 position;

            position.X = (this.Window.ClientBounds.Width - ms.X) / 2.0f;

            position.Y = (this.Window.ClientBounds.Height - ms.Y) / 2.0f;

            spriteBatch.DrawString(FontofTimesNewRoman, str, position, Color.BlueViolet);

 

            spriteBatch.End();

 

 

 

            //model显示

            Matrix world = Matrix.Identity;

            Matrix view = Matrix.CreateLookAt(new Vector3(500, 0, 500), Vector3.One, Vector3.Up);

            Matrix projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, GraphicsDevice.Viewport.AspectRatio, 0.1f, 500f);

 

            model.Draw(world, view, projection);

 

            base.Draw(gameTime);

        }

    }

}

 注:字体,图片,模型需放到content目录下编译成xnb文件才能使用........

posted on 2018-12-21 11:22  _萧朗  阅读(286)  评论(0编辑  收藏  举报