佳礼资讯网

 找回密码
 注册

ADVERTISEMENT

楼主: geekman

使用C# + Managed DIrectX 制作 2D 游戏

[复制链接]
发表于 22-9-2008 12:34 AM | 显示全部楼层
My2DGame.cs 只放
using System;
using System.Drawing;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
using Microsoft.DirectX.DirectInput;
using Microsoft.DirectX.DirectSound;


开启一个新的Windows Form 最上面的
using System;
using System.Drawing;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
using Microsoft.DirectX.DirectInput;
using Microsoft.DirectX.DirectSound;
要删除吗??
回复

使用道具 举报


ADVERTISEMENT

 楼主| 发表于 22-9-2008 10:45 AM | 显示全部楼层
原帖由 cskoom 于 22-9-2008 12:23 AM 发表
现在在学习这个,C# + Managed DIrectX 制作 2D 游戏

一步步跟着做括弧有点乱,copy & paste 又跑不起。。。



在 class Game:Form{在这里吗??}

还是在这里??
在 class Game:Form{ ...

namespace My2DGame
{
    在这里.
    class Game:Form
    {
   
    }
}
#2)不需要,没用到的using statement并不会被compile在.exe 档案里面,顶多只是当你写code的时候,Auto Complete 需要多一点时间搜寻正确的建议罢了,只要你的电脑够快,根本不会有影响。

[ 本帖最后由 geekman 于 22-9-2008 10:47 AM 编辑 ]
回复

使用道具 举报

发表于 22-9-2008 11:26 AM | 显示全部楼层
开启一个新的Windows Form:

public class Game : Form
    {
        public Game()
        {
}

抱歉!!我新建的Windows Form。。

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
    }


把public partial class Form1 : Form改成public class Game : Form是吗??出错哦。。请指教
回复

使用道具 举报

 楼主| 发表于 22-9-2008 11:40 AM | 显示全部楼层
这个 project 是以 Empty Project 来开始的,而不是 Windows Application。因为在这个project 里面,我们使用的 Game class 本身就是继承了 Windows Form 的属性,而且 这个 project 会自行的创建自己的 Form,所以不需要使用 Windows Application 作为起始范本。
回复

使用道具 举报

 楼主| 发表于 22-9-2008 11:42 AM | 显示全部楼层
这是我目前手头上的最新版本(已停止开发)
  1. using System;
  2. using System.Drawing;
  3. using System.Windows.Forms;
  4. using Microsoft.DirectX;
  5. using Microsoft.DirectX.Direct3D;
  6. using Microsoft.DirectX.DirectInput;
  7. using Microsoft.DirectX.DirectSound;

  8. namespace My2DGame
  9. {
  10.     public class Actor
  11.     {
  12.         public Vector3 pos;
  13.         Vector3 ctr;
  14.         Texture texture;
  15.         Matrix finalMatrix;
  16.         uint alpha, tint, spcolor;
  17.         Rectangle boundingRect;
  18.         bool isRotate;
  19.                
  20.         public Actor(Microsoft.DirectX.Direct3D.Device device)
  21.         {
  22.             pos = new Vector3(device.Viewport.Width / 2, device.Viewport.Height / 1.2f, 0.0f);
  23.             texture = null;
  24.             ctr =  Vector3.Empty;
  25.             finalMatrix = new Matrix();
  26.             boundingRect = new Rectangle(32, 32, (device.Viewport.Width - 64), (device.Viewport.Height - 64));
  27.             alpha = 0xff000000;//opaq
  28.             tint = 0xffffff;//white
  29.             spcolor = alpha | tint;
  30.             isRotate = false;            
  31.         }

  32.         public void SetTexture(Microsoft.DirectX.Direct3D.Device device, String texturesrc)
  33.         {
  34.             texture = TextureLoader.FromFile(device, texturesrc);
  35.             Surface surface = texture.GetSurfaceLevel(0);
  36.             ctr = new Vector3(surface.Description.Width / 2.0f, surface.Description.Height / 2.0f, 0.0f);
  37.         }

  38.         public void Rotate(float angle)
  39.         {        
  40.             finalMatrix = Matrix.Multiply(Matrix.RotationZ(angle), Matrix.Translation(pos));
  41.             isRotate = true;
  42.         }

  43.         public void DrawSprite(Sprite sp)
  44.         {
  45.             if (isRotate)
  46.             {
  47.                 sp.Transform = finalMatrix;
  48.                 sp.Draw(texture, ctr, Vector3.Empty, unchecked((int)spcolor));               
  49.                 isRotate = false;
  50.             }
  51.             else
  52.             {
  53.                 sp.Draw(texture, ctr, pos, unchecked((int)spcolor));
  54.             }
  55.         }

  56.         public void setAlpha(uint newAlpha)
  57.         {
  58.             alpha = newAlpha;
  59.             spcolor = alpha | tint;
  60.         }

  61.         public void setTint(uint newTint)
  62.         {
  63.             tint = newTint;
  64.             spcolor = alpha | tint;
  65.         }

  66.         public void setPos(float xpos, float ypos)
  67.         {
  68.             pos.X = xpos;
  69.             pos.Y = ypos;
  70.         }

  71.     }
  72.     //------------------------------------------------------------------------------------------
  73.    
  74.     //------------------------------------------------------------------------------------------
  75.     public class Game : Form
  76.     {
  77.         Microsoft.DirectX.Direct3D.Device d3dev = null;
  78.         Microsoft.DirectX.DirectInput.Device kb = null;

  79.         Actor player;
  80.         Sprite sprite;
  81.         int errorNo;      

  82.         public Game()
  83.         {
  84.             this.ClientSize = new Size(800, 600);
  85.             this.Text = "My 2D Game with Managed DirectX";         
  86.         }

  87.         public bool InitD3D()
  88.         {
  89.             try
  90.             {
  91.                 PresentParameters pp = new PresentParameters();
  92.                 pp.Windowed = false;
  93.                 pp.BackBufferWidth = 800;
  94.                 pp.BackBufferHeight = 600;
  95.                 pp.BackBufferFormat = Format.A8R8G8B8;
  96.                 pp.SwapEffect = SwapEffect.Discard;

  97.                 d3dev = new Microsoft.DirectX.Direct3D.Device(0,
  98.                             Microsoft.DirectX.Direct3D.DeviceType.Hardware,
  99.                             this,
  100.                             CreateFlags.HardwareVertexProcessing,
  101.                             pp);

  102.                 return true;
  103.             }
  104.             catch (DirectXException)
  105.             {
  106.                 errorNo = DirectXException.LastError;
  107.                 return false;
  108.             }
  109.         }

  110.         public bool InitKeyboard()
  111.         {
  112.             try
  113.             {
  114.                 kb = new Microsoft.DirectX.DirectInput.Device(SystemGuid.Keyboard);
  115.                 kb.SetCooperativeLevel(this, CooperativeLevelFlags.NonExclusive | CooperativeLevelFlags.Background);

  116.                 kb.Acquire();
  117.                 return true;
  118.             }
  119.             catch(DirectXException)
  120.             {
  121.                 errorNo = DirectXException.LastError;
  122.                 return false;
  123.             }
  124.         }
  125.         float rotAngle = 0;//for testing only
  126.         public void UpdateGame()
  127.         {
  128.             KeyboardState ks = kb.GetCurrentKeyboardState();

  129.             if (ks[Key.Escape])
  130.             {
  131.                 this.Close();
  132.                 return;
  133.             }
  134.             if (ks[Key.Space])
  135.             {
  136.                
  137.                
  138.             }
  139.             if (ks[Key.A])
  140.             {
  141.                 if ((player.pos.X -= 5) < 0)
  142.                     player.pos.X = 0;
  143.             }
  144.             if (ks[Key.D])
  145.             {
  146.                 if ((player.pos.X += 5) > d3dev.Viewport.Width)
  147.                     player.pos.X = d3dev.Viewport.Width;   
  148.             }
  149.             if (ks[Key.W])
  150.             {
  151.                 if ((player.pos.Y -= 5) < 0)
  152.                     player.pos.Y = 0;
  153.             }
  154.             if (ks[Key.S])
  155.             {
  156.                 if ((player.pos.Y += 5) > d3dev.Viewport.Height)
  157.                     player.pos.Y = d3dev.Viewport.Height;   
  158.             }
  159.             player.Rotate(rotAngle += 0.1f);
  160.             RenderGame();
  161.         }
  162.         
  163.         public void RenderGame()
  164.         {
  165.             d3dev.Clear(ClearFlags.Target, Color.Blue, 1.0f, 0);
  166.    
  167.             d3dev.BeginScene();
  168.                 sprite.Begin(SpriteFlags.AlphaBlend);
  169.                     player.DrawSprite(sprite);
  170.                 sprite.End();
  171.             d3dev.EndScene();
  172.             d3dev.Present();

  173.         }

  174.         public bool InitGame()
  175.         {
  176.             if (!InitD3D())
  177.             {
  178.                 return false;
  179.             }

  180.             if (!InitKeyboard())
  181.             {
  182.                 return false;
  183.             }

  184.             sprite = new Sprite(d3dev);
  185.             player = new Actor(d3dev);
  186.             player.SetTexture(d3dev, "plane.dds");
  187.             player.setPos(400.0f, 300.0f);
  188.             return true;
  189.         }
  190.         //-----------------------------------------------------------------
  191.         static void Main()
  192.         {
  193.             using (Game MainForm = new Game())
  194.             {
  195.                 if (!MainForm.InitGame())
  196.                 {

  197.                     MessageBox.Show("Failed to initialize D3D: " + DirectXException.GetDirectXErrorString(MainForm.errorNo));
  198.                     return;
  199.                 }
  200.                 MainForm.Show();

  201.                 while (MainForm.Created)
  202.                 {
  203.                     MainForm.UpdateGame();
  204.                     Application.DoEvents();
  205.                 }
  206.             }
  207.         }
  208.     }
  209. }
复制代码
回复

使用道具 举报

发表于 22-9-2008 10:11 PM | 显示全部楼层
哦!!全部代码都在My2DGames.cs里是吗??

“”接下来,我们要开启一个新的Windows Form:“”“
我以为开个Windows Form=Form1

我把完整的代码直接贴在My2DGames.cs,debug没问题可以跑,全屏黑色一直loading是什么问题是显示卡的问题吗?

我的显示卡GeForce FX52000 [好像是64mb]

全都安装了
Visual C# 2005 Express Edition + sp1
XNA
Microsoft DirectX 9.0 SDK  March 2007

直接copy&paste可以吗??源码可以寄给我测试吗?? 1
回复

使用道具 举报

Follow Us
 楼主| 发表于 22-9-2008 11:08 PM | 显示全部楼层
画面全黑,可能是你的 project 里面没有相对应的图片。记得我的code 里面有一个:
player.SetTexture(d3dev, "plane.dds");

plane.dds 是一张代表玩家控制的飞机的图片。你可以用任何一种图片代替它(记得别太大张,理想是128x128,或者 64x64),可以采用任何windows支援的格式(gif, ,jpg, png, bmp, dds 等等),根据我所形容的步骤把它加入到project 里面就行了。

这个project cut & paste应该没问题,大多数的code都不必通过manager的登录。
回复

使用道具 举报

发表于 22-9-2008 11:21 PM | 显示全部楼层
哦!!试试。。

之前看过你的教学“跨入三次元:C# + MDX 3D 射击游戏基本架构”有一图片驱动它的,就是找不到图片格式,原来dds是图片格式,没看过
回复

使用道具 举报


ADVERTISEMENT

发表于 22-9-2008 11:54 PM | 显示全部楼层
Add->Existing Item 图片在My2DGame,bin,obj都不能哦,平时是放在那里的?96X96 BMP 可以吗?
回复

使用道具 举报

 楼主| 发表于 23-9-2008 12:12 AM | 显示全部楼层
在solution explorer里面,在你的project name (My2DGame)上right click,选择Add->Existing Item.
你要加入的是Image File,不是bin或则obj。

source code 在我的办公室的电脑里面,明天发给你吧。
回复

使用道具 举报

发表于 23-9-2008 11:31 AM | 显示全部楼层
网上找到的C#源码,不是public partial class 开头的,都是用你这方法的嘛??
用Empty Project 来开始——>Reference的components-->Add->New Item... 选择 Code File。。。paste ,是这样吗?
回复

使用道具 举报

 楼主| 发表于 23-9-2008 12:28 PM | 显示全部楼层
source code 已经发给你了.
回复

使用道具 举报

发表于 23-9-2008 03:00 PM | 显示全部楼层
不错哦!!可以把飞机不旋转嘛?如果设计到可以打飞机要很多时间吗??

不知怎么我跑你的源码没问题,如果开自己开的,在View designer如下面的图,[是图片的问题吗?]


[ 本帖最后由 cskoom 于 23-9-2008 03:09 PM 编辑 ]
回复

使用道具 举报

 楼主| 发表于 23-9-2008 03:54 PM | 显示全部楼层
不是,这个error的原因是,Game class并不是project 里的第一个class(我把Actor class放在前面了),这就造成了designer无法正确的判断哪一个才是project的main class。再重申一次,这个project 的 windows form 是在 runtime 的时候以 program code 来开启的,所以并无法使用form designer,也不需要使用,因为这个 project 除了使用那个 created form 的 handle 提供给 D3D Device之外,就根本没使用到任何和form有关的东西了。

要停止飞机的旋转,只要把 UpdateGame() 里面的 player.Rotate(rotAngle += 0.1f); 这一行去掉就可以了。
回复

使用道具 举报

发表于 23-9-2008 08:17 PM | 显示全部楼层
Game class并不是project里的第一个class,那么copy & paste不成立了,要一步步来吗?

My2DGame是Code File, 命名为 My2DGame.cs的,我没开新的Windows Form。
你的意思是My2DGame.cs无法开启view designer是吗?在Solution Explorer上面哦!!!

有待消化


抱歉!!我对于C#和Visual C# Express Edition 不是很熟悉,只会HTML,一点编程基础都没,(我住这里很山买不到书来学习,在谷歌找到点C#基础的还没搞清楚)

还有我的英文很烂,Visual C# Express Edition根本是查字典来了解。。。

我有一颗强壮的心,钢铁般的意志,对游戏开发和学习编程有强烈的兴趣。哦,但是。。。我没超强的吸收力。。。
回复

使用道具 举报

 楼主| 发表于 23-9-2008 09:22 PM | 显示全部楼层
没有超强的吸收力那就慢慢的吸。。。

对,这个 project 根本就没有使用 form designer 的必要,因为它并没有由 .net manager 所开设的 form,而是在执行程式初始化时,由 program 即时产生自己的 form。

这个 project 你还是可以使用 copy & paste ,第一个class 并不是 Game class 这个包含 main() 的 class 只不过代表 form designer 无法启动罢了。

在没有任何实际编程经验(HTML与其说是编程,倒不如说是设计版位排列)之下,你能学到这里已经很不错了,你缺乏的只是实际操作 VC# 的 IDE 的经验罢了。继续努力吧。想当初我初次接触 C++ Builder 时也是丈八金刚摸不着脑,当场就放弃了,过了很久后才又再度的鼓起勇气尝试下去,才有现在的成就。。。
回复

使用道具 举报


ADVERTISEMENT

发表于 23-9-2008 10:56 PM | 显示全部楼层
吖!!我成功了!! 经过多次的教学,再回到第一页,捉到一点路了,一步一步的走,终于明白Windows Form是什么了。。

经过这次教学,我知网上的分享的代码怎呈现了。。
回复

使用道具 举报

 楼主| 发表于 23-9-2008 11:21 PM | 显示全部楼层
很好,你入门了!
回复

使用道具 举报

发表于 24-9-2008 12:47 PM | 显示全部楼层
要停止飞机的旋转,只要把 UpdateGame() 里面的 player.Rotate(rotAngle += 0.1f); 这一行去掉就可以了。

你给我的源码强化了吗??在第一页里的教学没player.Rotate(rotAngle += 0.1f); 这一行哦!!我学习的是第一页里的教学源码。。
回复

使用道具 举报

 楼主| 发表于 24-9-2008 01:39 PM | 显示全部楼层
我寄给你的是后来我改进过的。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

 

ADVERTISEMENT



ADVERTISEMENT



ADVERTISEMENT

ADVERTISEMENT


版权所有 © 1996-2023 Cari Internet Sdn Bhd (483575-W)|IPSERVERONE 提供云主机|广告刊登|关于我们|私隐权|免控|投诉|联络|脸书|佳礼资讯网

GMT+8, 11-8-2025 11:47 AM , Processed in 0.117606 second(s), 22 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表