zoukankan      html  css  js  c++  java
  • XNA Game的基础

    Windows Game

    XNA基础:

    添加Camera:

    点击鼠标右键,新建项,选择GameComponent,重命名为Camera.cs。

    在Camera类中添加两个类级变量(自动实现属性)来表示摄像机的视图和投影矩阵。

    22         public Matrix view { get; protected set; }
    23         public Matrix projection { get; protected set; }
    然后修改构造器,接收3个Vector3变量,它们代表初始的摄像机的位置、目标和up向量。
    完整代码:
     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using Microsoft.Xna.Framework;
     5 using Microsoft.Xna.Framework.Audio;
     6 using Microsoft.Xna.Framework.Content;
     7 using Microsoft.Xna.Framework.GamerServices;
     8 using Microsoft.Xna.Framework.Graphics;
     9 using Microsoft.Xna.Framework.Input;
    10 using Microsoft.Xna.Framework.Media;
    11 
    12 
    13 namespace start
    14 {
    15     //摄像机的视图和投影矩阵
    16     /// <summary>
    17     /// This is a game component that implements IUpdateable.
    18     /// </summary>
    19     public class Camera : Microsoft.Xna.Framework.GameComponent
    20     {
    21         //摄像机的视图和投影矩阵
    22         public Matrix view { get; protected set; }
    23         public Matrix projection { get; protected set; }
    24         public Camera(Game game,Vector3 pos,Vector3 target,Vector3 up)
    25             : base(game)
    26         {
    27             view = Matrix.CreateLookAt(pos,target,up);
    28             projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4,(float)Game.Window.ClientBounds.Width/(float)Game.Window.ClientBounds.Height,1,100);
    29             // TODO: Construct any child components here
    30         }
    31 
    32         /// <summary>
    33         /// Allows the game component to perform any initialization it needs to before starting
    34         /// to run.  This is where it can query for any required services and load content.
    35         /// </summary>
    36         public override void Initialize()
    37         {
    38             // TODO: Add your initialization code here
    39 
    40             base.Initialize();
    41         }
    42 
    43         /// <summary>
    44         /// Allows the game component to update itself.
    45         /// </summary>
    46         /// <param name="gameTime">Provides a snapshot of timing values.</param>
    47         public override void Update(GameTime gameTime)
    48         {
    49             // TODO: Add your update code here
    50 
    51             base.Update(gameTime);
    52         }
    53     }
    54 }

    绘制基元,在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 start
    {
        /// <summary>
        /// This is the main type for your game
        /// </summary>
        public class Game1 : Microsoft.Xna.Framework.Game
        {
            GraphicsDeviceManager graphics;
            SpriteBatch spriteBatch;
            Camera camera;
            VertexBuffer vertexBuffer;
            VertexPositionColor[] verts;
            BasicEffect effect;
            Matrix world = Matrix.Identity;
    
            public Game1()
            {
                graphics = new GraphicsDeviceManager(this);
            }
    
            /// <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()
            {
                camera = new Camera(this,new Vector3(0,0,5),Vector3.Zero,Vector3.Up);
                Components.Add(camera);
                // TODO: Add your initialization logic here
    
                base.Initialize();
            }
    
            /// <summary>
            /// LoadContent will be called once per game and is the place to load
            /// all of your content.
            /// </summary>
            protected override void LoadContent()
            {
                verts = new VertexPositionColor[3];
                verts[0] = new VertexPositionColor(new Vector3(0, 1, 0), Color.Blue);
                verts[1] = new VertexPositionColor(new Vector3(1, -1, 0), Color.Red);
                verts[2] = new VertexPositionColor(new Vector3(-1, -1, 0), Color.Green);
                vertexBuffer = new VertexBuffer(GraphicsDevice, typeof(VertexPositionColor), verts.Length, BufferUsage.None);
                vertexBuffer.SetData(verts);
                // Create a new SpriteBatch, which can be used to draw textures.
                spriteBatch = new SpriteBatch(GraphicsDevice);
                //初始化BasicBuffer
                effect = new BasicEffect(GraphicsDevice);
                            Content.RootDirectory = "Content";
    
                // TODO: use this.Content to load your game content here
            }
    
            /// <summary>
            /// UnloadContent will be called once per game and is the place to unload
            /// all content.
            /// </summary>
            protected override void UnloadContent()
            {
                // TODO: Unload any non ContentManager content here
            }
    
            /// <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)
            {
                // Allows the game to exit
                if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                    this.Exit();
    
                // TODO: Add your update logic here
                //平移(Tracelotion)
                KeyboardState keyboardstate = Keyboard.GetState();
                if (keyboardstate.IsKeyDown(Keys.Left))
                {
                    world *= Matrix.CreateTranslation(-.01f, 0,0);
                    world *= Matrix.CreateRotationY(MathHelper.PiOver4 / 60);
                }
                if (keyboardstate.IsKeyDown(Keys.Right))
                {
                    world *= Matrix.CreateTranslation(.01f, 0, 0);
                    world *= Matrix.CreateRotationX(MathHelper.PiOver4 / 60);
                }
    
    
                base.Update(gameTime);
            }
    
            /// <summary>
            /// This is called when the game should draw itself.
            /// </summary>
            /// <param name="gameTime">Provides a snapshot of timing values.</param>
            protected override void Draw(GameTime gameTime)
            {
                GraphicsDevice.Clear(Color.CornflowerBlue);
                GraphicsDevice.SetVertexBuffer(vertexBuffer);
                // TODO: Add your drawing code here
                //设置物体和摄像机的信息
                effect.World = world;
                effect.View = camera.view;
                effect.Projection = camera.projection;
                effect.VertexColorEnabled = true;
                //开始效果,并绘制每个Pass
                foreach (EffectPass pass in effect.CurrentTechnique.Passes)
                {
                    pass.Apply();
                    GraphicsDevice.DrawUserPrimitives<VertexPositionColor>(PrimitiveType.TriangleStrip, verts, 0, 1);
                }
    
                base.Draw(gameTime);
                
            }
        }
    }

    keydown,读者自行改变。

  • 相关阅读:
    PHP mysqli_fetch_assoc() 函数
    PHP mysqli_fetch_array() 函数
    PHP mysqli_fetch_all() 函数
    systemctl 列出系统所有服务
    HDU 1068 Girls And Boys 二分图题解
    Hadoop自学笔记(三)MapReduce简单介绍
    经典语录
    设计模式之九 单例模式
    NYOJ 587 blockhouses 【DFS】
    【从0開始Tornado建站】显示全部注冊用户
  • 原文地址:https://www.cnblogs.com/yuanshaoqian/p/2979387.html
Copyright © 2011-2022 走看看