XNA 学习笔记(1.基础 : 常用的一些属性)

1.改变 游戏窗口的大小

GraphicsDeviceManager graphics = new GraphicsDeviceManager(this);

graphics.PreferredBackBufferWidth = 800;

graphics.PreferredBackBufferHeight = 400;

2.得到键盘鼠标事件

KeyboardState keyboard = Keyboard.GetState();

MouseState mouse = Mouse.GetState();

可以在 Update 方法中.接收用户的输入事件:

if (keyboard.IsKeyDown(Keys.LeftControl)) {
  // Left Control Pressed

}
if (mouse.LeftButton == ButtonState.Pressed) {
  // Left Mouse Button Pressed

}

 3.显示鼠标

IsMouseVisible = true;

4.游戏组件:

1). GameComponent // 不需要Draw东西的时候.只重写其Update 方法

2).DrawableGameComponent // 可以重写 Draw 方法.

组件间的通信可以使用 GameService.

3).如何定义 GameService. 先创建一个提拱的接口 如:

Interface ICamera {

  Matrix transform {get;}

  ...

}

然后让 GameComponent去实现这个服务接口.

class CameraComponent : GameComponent,ICamera {

  public CameraComponent(Game game) {

    game.services.AddService(typeof(ICamera),this); // 将自身加到 Game 的 .GameServices 中.

  }

}

这样别的组件, 就可以访问到该组件提拱的服务了:

ICamera camera = (ICamera)Game.Services.GetService(typeof(ICamera));

camera.transform .... // 这样就可以访问这个服务接口里的数据了.

5. 分屏显示 Viewport

一般情况下 (全屏.只有一个Viewpot,一个相机), 我们在 Draw() 方法中,只需要绘制一次, 多屏的情况下. 就需要多个 相机的 Matrix 及多个 Viewport.

比如左右分屏:

Viewport LeftViewport;

Viewport RightViewport;

// 先将它们都初始化为默认的Viewport

LeftViewport = RightViewport = GraphicsDevice.Viewport;

// Half Size Viewport width

LeftViewport.Width = RightViewport.Width = GraphicsDevice.Viewport.Width / 2;

RightViewport.X = LeftViewport.Width;

// 在 Draw 方法中, 先画左屏

GraphicsDevice.Viewport = LeftViewport;

Draw().....

// 再画右屏

GraphicsDevice.Viewport = RightViewport;

Draw().....

*注:*  Draw的方法中, 要用到分另 2 个视图的相机的 Matrix.

================ 待 续 =================

原文地址:https://www.cnblogs.com/easyfrog/p/2829743.html