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 herebase.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);//初始化BasicBuffereffect = 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 exitif (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;//开始效果,并绘制每个Passforeach (EffectPass pass in effect.CurrentTechnique.Passes){pass.Apply();GraphicsDevice.DrawUserPrimitives<VertexPositionColor>(PrimitiveType.TriangleStrip, verts, 0, 1);}base.Draw(gameTime);}}
}

keydown,读者自行改变。

转载于:https://www.cnblogs.com/yuanshaoqian/archive/2013/03/24/2979387.html

XNA Game的基础相关推荐

  1. XNA框架基础——XNA介绍

    第1章:XNA介绍 欢迎来到XNA的世界.作为一个游戏程序员,你或许知道 DirectX ,甚至也许具有XNA 框架的基础知识. 这一个章节解释了如何安装 XNA Game Studio Expres ...

  2. 《XNA高级编程:Xbox 360和Windows》1-1

    第一部分 XNA Framework基础 第一章 XNA简介 欢迎进入XNA的世界.作为一个游戏程序员,你大概了解一些DirectX的知识以及XNA Framework的基础.这一章主要介绍怎样安装X ...

  3. 分享Silverlight/WPF/Windows Phone一周学习导读(1月17日-1月23日)

    上周微软Silverlight团队发布"微软发布Silverlight Native Extensions 1.0 - 扩展OOB应用功能",对于Silverlight开发人员而言 ...

  4. 游戏开发和设计推荐书籍

    游戏开发书籍推荐(1/3) 1.<Windows游戏编程大师技巧(第二版)> 原名:Tricks of the Windows Game Programming Gurus, 2nd 作者 ...

  5. 第一部分:基础知识(第一章)一个XNA 手机程序

    接下来我们编写一个在在屏幕中心显示一些问候的XNA程序.而文本往往在Silverlight应用非常普遍,通常作为文字描或如何游戏或展示分数.这是一个"Hello,world"不能作 ...

  6. Direct和XNA基础

    #### 1. DirectX的定义和作用 DirectX(Direct eXtension,简称DX)是由微软公司创建的多媒体编程接口,是一种应用程序接口(API).DirectX可让以window ...

  7. XNA之RPG游戏开发教程之三

    本节在上一节基础上继续完善该游戏引擎,主要完成以下任务: (1)完善StartMenuScreen,同时添加一个GamePlayScreen页面 (2)创建一个新的控件,picture box (3) ...

  8. 创建XNA Shooter游戏——挑战:用引擎编写自己的游戏

    挑战:用引擎编写自己的游戏 本书之前已经讨论了很多游戏,在你开始最后一个游戏之前我建议你使用游戏引擎创建自己的小游戏.本章你看到了所有的重要步骤,从在Rocket Commander引擎的基础上创建X ...

  9. Farseer:一个用于Silverlight和XNA的开源物理引擎

    当前在演示和越来越多交互界面的推动下,实时动画在很多情况下已经成为不可或缺的要求.当动画涉及到物体在屏幕上互相反弹或者被重力影响的时候,一个物理引擎就是必要的了. Farseer Physics En ...

最新文章

  1. SOFAMosn配置模型
  2. 阿里云助力合作伙伴帮助政府、企业体验云计算大数据魅力!
  3. 【同124】LeetCode 543. Diameter of Binary Tree
  4. 实验报告四 201521430002 张实
  5. 主键和外键举例_数据库-主键和外键及其约束
  6. 如何把html转为excel,怎么把网页转化为excel
  7. Ubuntu常用C语言IDE,Ubuntu下常用IDE的安装
  8. 《数学之美》读书笔记和知识点总结
  9. 搞懂特征值与特征向量
  10. Neo4j学习笔记-Embedded嵌入模式简单示例
  11. index.php.bak 颓废_CVE-2018-12613-phpmyadmin4.8.1远程文件包含漏洞复现
  12. 给WPS文档加密码的多种方法
  13. geoJson——地理数据编码格式
  14. 大学生社交网络问卷调查,社交情况问卷调查报告
  15. amc 美国数学竞赛能用计算机吗,美国数学竞赛AMC的三种级别
  16. 积分换元法中换元单调性问题的讨论
  17. Android必知必会-Stetho调试工具
  18. Ubuntu10下载安装Android 2.2 froyo 源码
  19. 有向无环图——AOE网(关键路径)
  20. 1603: 海岛争霸

热门文章

  1. SQL中 group by 1, order by 1 语句是什么意思
  2. 0110闭区间上连续函数的性质-函数与极限-高等数学
  3. 多益网络校招前端面经
  4. 单片机 怎调用显示屏字库_单片机巧用Windows矢量字库
  5. 最优化理论——(二)凸性1 凸集
  6. 什么是蓝牙的核心协议层和产品类型?一文读懂BQB认证中两者的关联
  7. 三国志战略版:S9血刃开荒实录五_血刃实战
  8. 语音识别 从入门到进阶 一 文末附项目/源码
  9. Flink 清理过期 Checkpoint 目录的正确姿势
  10. (三)千隆问屈术 让你成为别人的神 让人觉得你是权威专家的沟通模式