游戲開發教程c#

發表于:2007-04-28來源:作者:點擊數: 標簽:開發適合本文一定教程
本文適合有一定編程基礎的愛好者!本文不會涉及基本的語法等內容,本文 適合所有 游戲 開發 初學者,本文從MicrosoftDirectX9.0SDK(Summer2004)中 的D3D下Tutorials文件夾下的例子開始??! 關鍵字:c#游戲開發3D教程 C#(讀作“Csharp”)是一種簡單、現代、
本文適合有一定編程基礎的愛好者!本文不會涉及基本的語法等內容,本文

適合所有游戲開發初學者,本文從Microsoft DirectX 9.0 SDK (Summer 2004)中

的D3D下Tutorials文件夾下的例子開始??!
    關鍵字:c#  游戲開發  3D  教程
    C#(讀作“C sharp”)是一種簡單、現代、面向對象且類型安全的編程語言。C 和 C++ 程序員能很快熟悉它。C# 同時具備“應用程序快速開發”(RAD) 語言的高效率和 C++ 固有的強大能力。(c#語言標準參考如是說)
廢話少說,進入主題,開始我們的c#游戲開發之旅?。ǚg有誤請多原諒)


第一章 組建我們的設備


    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則退出程序
}

/// <summary>
/// The main entry point for the application.
/// 程序的主函數,入口點
/// </summary>
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窗口就建立好了
!

附件



原文轉自:http://www.anti-gravitydesign.com

国产97人人超碰caoprom_尤物国产在线一区手机播放_精品国产一区二区三_色天使久久综合给合久久97