XNA开发实用教程——图片

三峡大学土木水电学院肖泽云本教程的主要目的是让你看完后,真正体会一下什么是XNA?XNA中主要包括哪些部分?相信你自己,在看完整个教程后,你也能设计自己的三维场景!祝你成功!
二、图片 [TextureWindowsGame]
1)、添加图片
1、首先在全局中定义:
Texture2DmyTexture;
2、将图片tubiao.png添加在解决方案资源管理器下的Content目录中。如下图所示:
XNA开发实用教程——图片


3、在LoadContent中添加:
myTexture = Content.Load<Texture2D>("tubiao");
4、在Draw函数中添加:
spriteBatch.Begin(SpriteBlendMode.AlphaBlend);
spriteBatch.Draw(myTexture, new Vector2(100, 100), Color.White);
spriteBatch.End();
其中new Vector2(100, 100)表示图片所在的位置,SpriteBlendMode.AlphaBlend表示图片是以阿尔法混合显示,即透明显示。但是透明显示主要和图片有关,若需要制作透明图片,可在PhotoShop中设置其透明通道(层),然后以PNG格式保存即可,必须注意,该图片背景一定要是透明,如下图所示:
XNA开发实用教程——图片



整个程序的代码如下:
[Game1.cs]
#region Using Statements//引用
using System;
using System.Collections.Generic;
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.Net;
using Microsoft.Xna.Framework.Storage;
#endregion

namespace TextureWindowsGame
{
public class Game1 : Microsoft.Xna.Framework.Game //继承Game类
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Texture2D myTexture;

public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}

protected override void Initialize()//初始化
{
base.Initialize();
}

protected override void LoadContent()//导入目录,每次游戏启动时都会启动
{
// 创建一个精灵,用于绘制图片
spriteBatch = new SpriteBatch(GraphicsDevice);
myTexture = Content.Load<Texture2D>("tubiao");
}

protected override void UnloadContent()//卸载目录
{
// TODO: Unload any non ContentManager content here
}

protected override void Update(GameTime gameTime)/// 更新。用于检测碰撞、输入等
{
// 设置游戏结束事件
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();

//添加更新的对象代码

base.Update(gameTime);
}

protected override void Draw(GameTime gameTime)//当绘制时被调用
{
graphics.GraphicsDevice.Clear(Color.CornflowerBlue);

// 添加绘图代码
spriteBatch.Begin(SpriteBlendMode.AlphaBlend);
spriteBatch.Draw(myTexture, new Vector2(100, 100), Color.White);
spriteBatch.End();


base.Draw(gameTime);
}
}
}

其最终结果如下:
XNA开发实用教程——图片



2)、设置游戏程序的图标
打开程序的属性,设置图标。如下图所示。
XNA开发实用教程——图片



3)、添加鼠标移动事件
XNA中默认并没有光标事件,在此将结合前面的TextureWindowsGame例子在XNA中添加光标移动事件,其光标图案采用tubiao.png参考前面的例子。
1、首先在全局中定义光标的位置:
int m_MouseX, m_MouseY;
2、在Game1类中获取鼠标事件状态
Mouse.WindowHandle = Window.Handle;
3、定义鼠标移动更新函数UpdataMouse()
public void UpdataMouse()
{
MouseState m_MouseState = Mouse.GetState();
m_MouseX = m_MouseState.X;
m_MouseY = m_MouseState.Y;
}
4、在Draw函数中调用。
UpdataMouse();
spriteBatch.Begin(SpriteBlendMode.AlphaBlend);
spriteBatch.Draw(myTexture,new Vector2(m_MouseX, m_MouseY), Color.White);
spriteBatch.End();
注意,其中new Vector2(m_MouseX, m_MouseY)即当前图片的位置即光标获取的位置。
所以代码如下:
[Game1.cs]
#region Using Statements//引用
using System;
using System.Collections.Generic;
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.Net;
using Microsoft.Xna.Framework.Storage;
#endregion

namespace TextureWindowsGame
{
public class Game1 : Microsoft.Xna.Framework.Game //继承Game类
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Texture2D myTexture;
int m_MouseX, m_MouseY;

public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
Mouse.WindowHandle = Window.Handle;
}

protected override void Initialize()//初始化
{
base.Initialize();
}

protected override void LoadContent()//导入目录,每次游戏启动时都会启动
{
// 创建一个精灵,用于绘制图片
spriteBatch = new SpriteBatch(GraphicsDevice);
myTexture = Content.Load<Texture2D>("tubiao");
}

protected override void UnloadContent()//卸载目录
{
// TODO: Unload any non ContentManager content here
}

protected override void Update(GameTime gameTime)/// 更新。用于检测碰撞、输入等
{
// 设置游戏结束事件
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();

//添加更新的对象代码

base.Update(gameTime);
}

public void UpdataMouse()
{
MouseState m_MouseState = Mouse.GetState();
m_MouseX = m_MouseState.X;
m_MouseY = m_MouseState.Y;
}

protected override void Draw(GameTime gameTime)//当绘制时被调用
{
graphics.GraphicsDevice.Clear(Color.CornflowerBlue);

// 添加绘图代码
UpdataMouse();
spriteBatch.Begin(SpriteBlendMode.AlphaBlend);
spriteBatch.Draw(myTexture, new Vector2(m_MouseX, m_MouseY), Color.White);
spriteBatch.End();

base.Draw(gameTime);
}
}
}
其显示结果如下图所示:
XNA开发实用教程——图片



4)、设置图片的缩放
图片的缩放主要是在Draw函数中的spriteBatch.Draw(myTexture, new Vector2(m_MouseX, m_MouseY), Color.White);里面
注意:new Vector2(m_MouseX, m_MouseY)这个表示图片的插入点,默认图片的大小为原始大小;若用new Rectangle(20,40,100,200),这表示图片的插入点为(20,40),图片的宽是100,高是200,通过更改这四个值,就可以更改图片的大小缩放等。如用:
spriteBatch.Draw(myTexture, new Rectangle(m_MouseX, m_MouseY,30,30), Color.White);

5)、设置图片的透明度
设置图片的透明度同设置图片大小一样,在Draw函数中设置。具体来说是在调用的时候设置。在spriteBatch.Draw(myTexture, new Rectangle(m_MouseX, m_MouseY,30,30), Color.White);中,一般最后一个参数为Color.White,但是如果要设置它的透明度的话,更改其为new Color(255, 255, 255,100 )即可。值得注意的是,new Color中的最后一个参数代表的是其Alpha值,即透明度,而且它的范围为0~255的整数,若是其他类型数字,必须将它转化为Byte类型。
运用代码:
spriteBatch.Draw(myTexture, new Rectangle(m_MouseX, m_MouseY, 30, 30), new Color(255, 255, 255, 100));
其显示结果如下:
XNA开发实用教程——图片


整个程序的代码如下:
#region Using Statements//引用
using System;
using System.Collections.Generic;
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.Net;
using Microsoft.Xna.Framework.Storage;
#endregion

namespace TextureWindowsGame
{
public class Game1 : Microsoft.Xna.Framework.Game //继承Game类
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Texture2D myTexture;
int m_MouseX, m_MouseY;

public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
Mouse.WindowHandle = Window.Handle;
}

protected override void Initialize()//初始化
{
base.Initialize();
}

protected override void LoadContent()//导入目录,每次游戏启动时都会启动
{
// 创建一个精灵,用于绘制图片
spriteBatch = new SpriteBatch(GraphicsDevice);
myTexture = Content.Load<Texture2D>("tubiao");
}

protected override void UnloadContent()//卸载目录
{
// TODO: Unload any non ContentManager content here
}

protected override void Update(GameTime gameTime)/// 更新。用于检测碰撞、输入等
{
// 设置游戏结束事件
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();

//添加更新的对象代码

base.Update(gameTime);
}

public void UpdataMouse()
{
MouseState m_MouseState = Mouse.GetState();
m_MouseX = m_MouseState.X;
m_MouseY = m_MouseState.Y;
}

protected override void Draw(GameTime gameTime)//当绘制时被调用
{
graphics.GraphicsDevice.Clear(Color.CornflowerBlue);

// 添加绘图代码
UpdataMouse();
spriteBatch.Begin(SpriteBlendMode.AlphaBlend);
spriteBatch.Draw(myTexture, new Rectangle(m_MouseX, m_MouseY, 30, 30), new Color(255, 255, 255, 100));
spriteBatch.End();

base.Draw(gameTime);
}
}
} <!--v:3.2-->

XNA开发实用教程——图片

<!--E 文章--><!--S 翻页-->