[2]有虫在爬……

首先在上一次的基础上进行动画贴图。

2D游戏中的帧动画来自这种东西:



可以很清楚地看到,在一张图中,每一行是一种动作,每一列是一帧。我们所要做的,就是选定某个动作(行),然后一帧一帧(从左到右)的把这些小方格轮流贴到屏幕上的同一位置上去。具体这样做:

sprite.Draw(texture, new Rectangle(x*48,y*48,48,48), Vector3.Empty, new Vector3(10,10,0), Color.White);          
//x是行数,y是列数。x和y表示当前需要画上屏幕的方格  
//这里每一个方格尺寸是48*48像素,第四个参数表示把
//图贴到屏幕(10,10)的位置。最后一个参数可以用来
//做透明色。因为我不认识图像原本的底色是什么,所以
//在我的程序中我把它改成了白色。
每次update屏幕的时候,改变x值就可以实现动画了。

然后,关于图像的移动。上面这段代码中,第四个参数决定了贴图的位置。也就是说改变它的值就可以让图像进行移动。同样是在每个update中把速度作为步长而增减坐标。

接下来,代码说明一切。

using System;
using System.Drawing;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;

namespace WindowsApplication1
{
    
public partial class Form1 : Form
    
{
        
public Form1()
        
{
            InitializeComponent();
            
this.Shown += new EventHandler(Form1_Shown);
        }


        
void Form1_Shown(object sender, EventArgs e)
        
{
            InitDevice(); 
            LoadTexture();

            
//重要改动:避免出错。
            while (!this.IsDisposed && !this.Disposing)
            
{
                Draw();
            }

        }


        
private Texture[] texture;
        
private Sprite sprite;
        
private Device device;

        
private void InitDevice()
        
{
            PresentParameters[] PP 
= new PresentParameters[1];
            PP[
0= new PresentParameters();
            
            PP[
0].Windowed = true;
            PP[
0].SwapEffect = SwapEffect.Discard;
                        
            device 
= new Device(0, DeviceType.Hardware, this, CreateFlags.HardwareVertexProcessing, PP);
            sprite 
= new Sprite(device);

            device.Clear(ClearFlags.Target, Color.Black, 
00);

        }


        
private void LoadTexture()
        
{
            texture 
= new Texture[2];
            
//改动:读取图片的时候将宽度和高度设为零,即读入整张图片
            texture[0= TextureLoader.FromFile(device, @"d:\md3dsp\beetle48.bmp",0,0,0,Usage.None,Format.Unknown,Pool.Managed,Filter.None,Filter.None,Color.White.ToArgb());
            texture[
1= TextureLoader.FromFile(device, @"d:\md3dsp\background.BMP"5122560, Usage.None, Format.Unknown, Pool.Managed, Filter.None, Filter.None, Color.White.ToArgb());
        }


        
private int i = 0;
        
private int speed = 2;//新字段:用来表示虫子的移动速度
        private int X = 50;
        
private int Y = 50;//新字段:用来表示虫子当前的位置

        
/// <summary>
        
/// 新代码:用于横向移动虫子
        
/// </summary>

        private Vector3 BugMove()
        
{
            
if (X == this.Width)
            
{
                X 
= -48;
            }

            
else
            
{
                X 
= X + speed;
            }
                       
            
return new Vector3(X, Y, 0);     
        }

                
        
private void Draw()
        
{
                device.BeginScene();
                sprite.Begin(SpriteFlags.AlphaBlend);
                sprite.Draw(texture[
1], Vector3.Empty, Vector3.Empty, Color.White.ToArgb());
                
//改动:第二个参数处,由于向右爬行的动画在第35行,所以矩形的Y位置应为34*48
                sprite.Draw(texture[0], new Rectangle(i * 4834*484848), Vector3.Empty, BugMove(), Color.White);
                i
++;
                
if (i > 9) i = 0;
                sprite.End();
                device.EndScene();
                device.Present();
                
//改动:下面这句的意图是让程序可以接受常规的windows操作,
                
//即我现在可以不写程序的退出方法。
                Application.DoEvents();
        }

    }

    
}
附带我所用的两张图。先说一句这两张是微软的东西,汝不可以拿去卖。
/Files/CloudAge/md3dsp.rar
posted @ 2005-11-28 21:40  Cloudage  阅读(303)  评论(0编辑  收藏  举报