|
使用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;
要删除吗?? |
|
|
|
|
|
|
|

楼主 |
发表于 22-9-2008 10:45 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
|
显示全部楼层
这是我目前手头上的最新版本(已停止开发)- using System;
- using System.Drawing;
- using System.Windows.Forms;
- using Microsoft.DirectX;
- using Microsoft.DirectX.Direct3D;
- using Microsoft.DirectX.DirectInput;
- using Microsoft.DirectX.DirectSound;
- namespace My2DGame
- {
- public class Actor
- {
- public Vector3 pos;
- Vector3 ctr;
- Texture texture;
- Matrix finalMatrix;
- uint alpha, tint, spcolor;
- Rectangle boundingRect;
- bool isRotate;
-
- public Actor(Microsoft.DirectX.Direct3D.Device device)
- {
- pos = new Vector3(device.Viewport.Width / 2, device.Viewport.Height / 1.2f, 0.0f);
- texture = null;
- ctr = Vector3.Empty;
- finalMatrix = new Matrix();
- boundingRect = new Rectangle(32, 32, (device.Viewport.Width - 64), (device.Viewport.Height - 64));
- alpha = 0xff000000;//opaq
- tint = 0xffffff;//white
- spcolor = alpha | tint;
- isRotate = false;
- }
- public void SetTexture(Microsoft.DirectX.Direct3D.Device device, String texturesrc)
- {
- texture = TextureLoader.FromFile(device, texturesrc);
- Surface surface = texture.GetSurfaceLevel(0);
- ctr = new Vector3(surface.Description.Width / 2.0f, surface.Description.Height / 2.0f, 0.0f);
- }
- public void Rotate(float angle)
- {
- finalMatrix = Matrix.Multiply(Matrix.RotationZ(angle), Matrix.Translation(pos));
- isRotate = true;
- }
- public void DrawSprite(Sprite sp)
- {
- if (isRotate)
- {
- sp.Transform = finalMatrix;
- sp.Draw(texture, ctr, Vector3.Empty, unchecked((int)spcolor));
- isRotate = false;
- }
- else
- {
- sp.Draw(texture, ctr, pos, unchecked((int)spcolor));
- }
- }
- public void setAlpha(uint newAlpha)
- {
- alpha = newAlpha;
- spcolor = alpha | tint;
- }
- public void setTint(uint newTint)
- {
- tint = newTint;
- spcolor = alpha | tint;
- }
- public void setPos(float xpos, float ypos)
- {
- pos.X = xpos;
- pos.Y = ypos;
- }
- }
- //------------------------------------------------------------------------------------------
-
- //------------------------------------------------------------------------------------------
- public class Game : Form
- {
- Microsoft.DirectX.Direct3D.Device d3dev = null;
- Microsoft.DirectX.DirectInput.Device kb = null;
- Actor player;
- Sprite sprite;
- int errorNo;
- public Game()
- {
- this.ClientSize = new Size(800, 600);
- this.Text = "My 2D Game with Managed DirectX";
- }
- public bool InitD3D()
- {
- try
- {
- PresentParameters pp = new PresentParameters();
- pp.Windowed = false;
- pp.BackBufferWidth = 800;
- pp.BackBufferHeight = 600;
- pp.BackBufferFormat = Format.A8R8G8B8;
- pp.SwapEffect = SwapEffect.Discard;
- d3dev = new Microsoft.DirectX.Direct3D.Device(0,
- Microsoft.DirectX.Direct3D.DeviceType.Hardware,
- this,
- CreateFlags.HardwareVertexProcessing,
- pp);
- return true;
- }
- catch (DirectXException)
- {
- errorNo = DirectXException.LastError;
- return false;
- }
- }
- public bool InitKeyboard()
- {
- try
- {
- kb = new Microsoft.DirectX.DirectInput.Device(SystemGuid.Keyboard);
- kb.SetCooperativeLevel(this, CooperativeLevelFlags.NonExclusive | CooperativeLevelFlags.Background);
- kb.Acquire();
- return true;
- }
- catch(DirectXException)
- {
- errorNo = DirectXException.LastError;
- return false;
- }
- }
- float rotAngle = 0;//for testing only
- public void UpdateGame()
- {
- KeyboardState ks = kb.GetCurrentKeyboardState();
- if (ks[Key.Escape])
- {
- this.Close();
- return;
- }
- if (ks[Key.Space])
- {
-
-
- }
- if (ks[Key.A])
- {
- if ((player.pos.X -= 5) < 0)
- player.pos.X = 0;
- }
- if (ks[Key.D])
- {
- if ((player.pos.X += 5) > d3dev.Viewport.Width)
- player.pos.X = d3dev.Viewport.Width;
- }
- if (ks[Key.W])
- {
- if ((player.pos.Y -= 5) < 0)
- player.pos.Y = 0;
- }
- if (ks[Key.S])
- {
- if ((player.pos.Y += 5) > d3dev.Viewport.Height)
- player.pos.Y = d3dev.Viewport.Height;
- }
- player.Rotate(rotAngle += 0.1f);
- RenderGame();
- }
-
- public void RenderGame()
- {
- d3dev.Clear(ClearFlags.Target, Color.Blue, 1.0f, 0);
-
- d3dev.BeginScene();
- sprite.Begin(SpriteFlags.AlphaBlend);
- player.DrawSprite(sprite);
- sprite.End();
- d3dev.EndScene();
- d3dev.Present();
- }
- public bool InitGame()
- {
- if (!InitD3D())
- {
- return false;
- }
- if (!InitKeyboard())
- {
- return false;
- }
- sprite = new Sprite(d3dev);
- player = new Actor(d3dev);
- player.SetTexture(d3dev, "plane.dds");
- player.setPos(400.0f, 300.0f);
- return true;
- }
- //-----------------------------------------------------------------
- static void Main()
- {
- using (Game MainForm = new Game())
- {
- if (!MainForm.InitGame())
- {
- MessageBox.Show("Failed to initialize D3D: " + DirectXException.GetDirectXErrorString(MainForm.errorNo));
- return;
- }
- MainForm.Show();
- while (MainForm.Created)
- {
- MainForm.UpdateGame();
- Application.DoEvents();
- }
- }
- }
- }
- }
复制代码 |
|
|
|
|
|
|
|
发表于 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 |
|
|
|
|
|
|
|

楼主 |
发表于 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是图片格式,没看过 |
|
|
|
|
|
|
|
发表于 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
|
显示全部楼层
|
|
|
|
|
|
|
发表于 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
|
显示全部楼层
|
|
|
|
|
|
|

楼主 |
发表于 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 时也是丈八金刚摸不着脑,当场就放弃了,过了很久后才又再度的鼓起勇气尝试下去,才有现在的成就。。。 |
|
|
|
|
|
|
|
发表于 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
|
显示全部楼层
|
|
|
|
|
|
| |
本周最热论坛帖子
|