XNA项目基础

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文件才能使用........

转载于:https://www.cnblogs.com/XiaoLang0/p/10154736.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值