跟我一起学XNA(1)让物体动起来①(附源码)
程序环境:VS2010和XNA Game Studio 4.0插件,具体可以下载
http://xbox.create.msdn.com/zh-CN/resources/downloads 这个是笔者的下载地址
1.新建一个项目
选中Windows Game(4.0),建立
会有两个工程,上面一个工程负责代码实现,下面的content则包含了项目所有资源,图片声音模型
首先,先看一下One(move)这个工程,它下面有个Game1,program这些文件,打开Game1
/// <summary> /// 所有的game都继承自Microsoft.Xna.Framework.Game /// </summary> public class Game1 : Microsoft.Xna.Framework.Game { //它提供了访问PC、Xbox360以及wp7图形设备的途径 GraphicsDeviceManager graphics; //绘制精灵,可以理解为一个2D,3D图像 SpriteBatch spriteBatch; public Game1() { graphics = new GraphicsDeviceManager(this); Content.RootDirectory = "Content"; } /// <summary> /// 初始化各种数据和方法,和.net的Initialize()差不多的意义,这里是程序执行的第一步 /// </summary> protected override void Initialize() { // TODO: Add your initialization logic here base.Initialize(); } /// <summary> /// 这里加载各种资源 /// </summary> protected override void LoadContent() { // Create a new SpriteBatch, which can be used to draw textures. spriteBatch = new SpriteBatch(GraphicsDevice); // TODO: use this.Content to load your game content here } /// <summary> /// 释放资源 /// </summary> protected override void UnloadContent() { // TODO: Unload any non ContentManager content here } /// <summary> /// 这里会进行一个刷新动作,默认每秒钟60次,这样就相当于界面在动态 /// </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 base.Update(gameTime); } /// <summary> /// 这里是和Update相同的,动态刷新,但是尽量在这里少做处理,所有的处理,逻辑,最好放在Update中 /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.CornflowerBlue); // TODO: Add your drawing code here base.Draw(gameTime); } }
一个XNA的生命周期是:Initialize()->LoadContent()->Update()和Draw()循环->UnLoadContet()
接着我加入一张图片,你可以右键添加,也可以直接复制,然后包含在项目中
加载图片
protected override void LoadContent() { // Create a new SpriteBatch, which can be used to draw textures. spriteBatch = new SpriteBatch(GraphicsDevice); dog = Content.Load<Texture2D>(@"Image/dog"); // TODO: use this.Content to load your game content here }
在Draw函数中,让加载后的图片画出来
protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.CornflowerBlue); // TODO: Add your drawing code here spriteBatch.Begin(); spriteBatch.Draw(dog, Vector2.Zero, null, Color.White); spriteBatch.End(); base.Draw(gameTime); }
运行,就能看到图片了
然后,我们要让图片动起来,我们在程序里定义一个图片初始位置和一个图片的运行速度
Vector2 beginPosition = Vector2.Zero; float speed = 3f;
在Update中加入判断
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 beginPosition.X += speed; if (beginPosition.X > Window.ClientBounds.Width - dog.Width || beginPosition.X < 0) speed *= -1; base.Update(gameTime); }
Draw函数中
protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.CornflowerBlue); // TODO: Add your drawing code here spriteBatch.Begin(); //spriteBatch.Draw(dog, Vector2.Zero, null, Color.White); spriteBatch.Draw(dog, beginPosition, null, Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, 0); spriteBatch.End(); base.Draw(gameTime); }
这就是一个简单的XNA图片运动的程序
在wpf中,可以通过Animation来指定开始和结束进行动画,和XNA能做到相同的效果。
只是笔者个人做3D的时候遇到一些瓶颈,wpf有些满足不了想法,所以试着学一下XNA看看能不能解决,以后会继续看下去,仅仅是个人爱好
若有相似想法,可以讨论
源代码:files.cnblogs.com/fish124423/XNA.rar