zoukankan      html  css  js  c++  java
  • 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文件才能使用........

    支持个人观看使用,如商用或转载,请告知! -----萧朗(QQ:453929789 Email:xiaolang_xl@sina.com)
  • 相关阅读:
    0094-leetcode算法实现之二叉树中序遍历-binary-tree-inorder-traversal-python&golang实现
    0144-leetcode算法实现之二叉树的前序遍历-binary-tree-preorder-traversal-python&golang实现
    0347-leetcode算法实现之前K个高频元素-top-k-frequent-elements-python&golang实现
    0239-leetcode算法实现之滑动窗口最大值-sliding-window-maximum-python&golang实现
    0150-leetcode算法实现之逆波兰表达式-evaluate-reverse-polish-notation-python&golang实现
    1047-leetcode算法实现之删除字符串的所有相邻的重复字符-remove-all-adjacent-duplicates-in-string-python&golang实现
    pstack 追踪进程-转
    0225-leetcode算法实现之用队列实现栈-implement-stack-using-queues-python&golang实现
    0020-leetcode算法实现之有效括号-valid-parentheses-python&golang实现
    0232-leetcode算法实现-用栈实现队列-implement-queue-using-stacks-python&golang实现
  • 原文地址:https://www.cnblogs.com/XiaoLang0/p/10154736.html
Copyright © 2011-2022 走看看