C#游戏开发教程--指引如何开发最好的游戏

王朝other·作者佚名  2008-05-19
窄屏简体版  字體: |||超大  

本文适合有一定编程基础的爱好者!本文不会涉及基本的语法等内容,本文

适合所有游戏开发初学者,本文从Microsoft DirectX 9.0 SDK (Summer 2004)中

的D3D下Tutorials文件夹下的例子开始!!

关键字:c#

游戏开发

3D

教程

C#(读作“C sharp”)是一种简单、现代、面向对象且类型安全的编程语言。C 和 C++ 程序员能很快熟悉它。C# 同时具备“应用程序快速开发”(RAD) 语言的高效率和 C++ 固有的强大能力。(c#语言标准参考如是说)

废话少说,进入主题,开始我们的c#游戏开发之旅!(翻译有误请多原谅)

第一章

组建我们的设备

1。建立一个DX程序,首先你需要下载Microsoft DirectX

SDK(最好事9.0一下简称DX),安装。然后事要保证你安装了Visual Studio .NET开发产品套件(一下简称vs.net),这是最小环境,然后你就可以进行游戏开发了。

建立一个DX设备。打开DX中的Tutorials文件夹下的Tutorials1例子并打开编译!下面是运行结果:

创建了一个DX窗口!

下面是代码:

//-----------------------------------------------------------------------------

// File: CreateDevice.cs

// 创建设备

// Desc: This is the first tutorial for using Direct3D. In this tutorial, all

//

we are doing is creating a Direct3D device and using it to clear the

//

window.

// 注释:这是第一个使用D3D的教学例子,在这个例子中,我们要作的仅仅是创建以个D3D“设备”和刷新窗口

// Copyright (c) Microsoft Corporation. All rights reserved.

//-----------------------------------------------------------------------------

using System;

using System.Drawing;

using System.Windows.Forms;

using Microsoft.DirectX;

using Microsoft.DirectX.Direct3D;

namespace DeviceTutorial

{

public class CreateDevice : Form

{

// Our global variables for this project

Device device = null; // Our rendering device

//我们的绘图设备

public CreateDevice()

{

// Set the initial size of our form

//设置窗体的初始值

this.ClientSize = new System.Drawing.Size(400,300);

// And it's caption

//设置窗体标题

this.Text = "D3D Tutorial 01: CreateDevice";

}

public bool InitializeGraphics()

{

try

{

// Now let's setup our D3D stuff

//现在我们设置D3D的一些选项

PresentParameters presentParams = new PresentParameters();

presentParams.Windowed=true;//标志着程序运行时窗口模式

presentParams.SwapEffect = SwapEffect.Discard;//返回或设置交换区选项????

device = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentParams);

//?,设备的类型(这里选择了硬件),创建图形设备的窗体,创建类型,创建实体);

//创建设备实例

return true;

}

catch (DirectXException)//捕捉DX异常

{

return false;

}

}

private void Render()//刷新模块

{

if (device == null)

return;

//Clear the backbuffer to a blue color

//将设备窗口刷成绿色

device.Clear(ClearFlags.Target, System.Drawing.Color.Blue, 1.0f, 0);

//clear(刷屏的参数这里选的是目标,使用的颜色,深度(可能用于模板),模板(0)因为没有使用模板)

//Begin the scene

//开始渲染场景,(因为没有场景所以一下句是空的就直接结束了场景的渲染)

device.BeginScene();

// Rendering of scene objects can happen here

//可以在这里渲染场景

//End the scene

//结束场景的渲染

device.EndScene();

device.Present();

}

protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)

//重写OnPaint方法

{

//this.Render(); // Render on painting

//循环的刷新窗口

}

protected override void OnKeyPress(System.Windows.Forms.KeyPressEventArgs e)//重写OnKeyPress方法

{

if ((int)(byte)e.KeyChar == (int)System.Windows.Forms.Keys.Escape)

this.Close(); // Esc was pressed

//如果按下了ESC则退出程序

}

///

/// The main entry point for the application.

/// 程序的主函数,入口点

///

static void Main()

{

//使用USING语句创建对象保证对象的销毁

using (CreateDevice frm = new CreateDevice())

{

if (!frm.InitializeGraphics()) // Initialize Direct3D

{

MessageBox.Show("Could not initialize Direct3D.

This tutorial will exit.");

return;

}

frm.Show();

// While the form is still valid, render and process messages

//消息循环

while(frm.Created)

{

frm.Render();

Application.DoEvents();

}

}

}

}

}

代码中有加入的注释!

首先是:

using System;

using System.Drawing;

using System.Windows.Forms;

using Microsoft.DirectX;

using Microsoft.DirectX.Direct3D;

使用命3名空间!注意的是,在程序的Main主程序中使也用了USING,注意这是c#中的一条语句,using 语句定义一个范围,在此范围的末尾将处理对象。

接着Device device = null;这句是申请了Device类的对象device但并未创建实例对象,实例对象的创建必须使用new语句创建。public bool InitializeGraphics() 函数 的作用是初始化DX,private void Render() 函数是渲染函数,其中的device.BeginScene(); 是开始渲染,device.EndScene();

device.Present(); 结束渲染,就如同翻页!可以在BeginScene();和EndScene();函数之中添加图像的显示或文字的显示等其它工作!程序最后的

while(frm.Created)

{

frm.Render();

Application.DoEvents();

}

是检测程序是否在执行,是则使用frm实例对象的方法Render();来渲染屏幕,Application.DoEvents();是执行消息循环!

这样!一个简单的DX窗口就建立好了

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航