本文將引導你使用XNA Game Studio Express一步一步地創建一個簡單的游戲

本文將引導你使用XNA Game Studio Express一步一步地創建一個簡單的游戲

第1步: 安裝軟件
第2步: 創建新項目
第3步: 查看代碼
第4步: 加入一個精靈
第5步: 使精靈可以移動和彈跳
第6步: 繼續嘗試!
完整的實例
第1步: 安裝軟件
在動手之前,先確定你已經安裝了所需的軟件,其中包括August 2006 DirectX SDK.參見所需軟件 中的軟件列表.

第2步:創建新項目
從開始菜單中啟動Microsoft Visual C# 2005 Express Edition.
在Microsoft Visual C# 2005 Express Edition 起始頁面出現后,點擊文件(File)菜單, 然后選擇新項目( New Project).
當出現對話框的時候, 選擇Windows Game (XNA) 類型的項目,輸入工程名稱,點擊OK.
創建新項目以后,你會看到設計器視圖和它的組件.至此,你的游戲中只有一個被標記為graphics的GraphicsComponent組件.

點擊文件菜單(File )-保存全部(Save All).
輸入保存工程的路徑,這個路徑也是你向游戲中加入圖象及其他資源的時候的路徑.保存完畢將返回設計視圖.
第3步:查看代碼
一些復雜的工作已經為你做好.如果你現在編譯并運行你的游戲, GraphicsComponent組件將為你處理設置屏幕大小,渲染出一個空白屏幕的這些工作. 你的游戲會自動的運行和更新. 你得加入自己的代碼來使游戲更有趣一些. 要查看代碼,只要在灰色的設計面板中的任意地方雙擊就可以了,或者在解決方案瀏覽器中右鍵點擊"Game1.cs",選擇查看代碼(View Code)

你將會看到已經為你寫好的代碼: 一個簡單的Update循環和一個簡單的 Draw循環; 你可以在任一個循環中加入你的代碼.

Update 循環是你對游戲邏輯進行更新的最佳場所:移動對象,接收玩家輸入,決定對象之間碰撞的結果,等等...
Draw 循環是你渲染對象和屏幕背景的一個最佳的場所.
第4步:加入一個精靈
下一步就是給屏幕上加入一個可見的圖象,使用一個小的圖象文件,比如.bmp或者.jpg,你也可以自己制作圖形,這樣就更具有創造性了.

你甚至可以制作不規則圖象-比如讓邊緣和拐角的地方不要顯示,這樣看起來效果會更好,可以參見例子:怎樣制作帶有遮掩的紋理圖..

當你有了圖形文件以后,你可以按照下面的步驟把它拷貝到你的項目的文件夾里面:

右鍵單擊解決方案資源管理器, 點擊添加(Add), 再點擊已有項(Existing Item).查找你的圖象文件并且選中它.解決方案資源管理器中將為這個圖象文件增加一項.
點擊該項.在解決方案資源管理器下面的屬性(Properties)面板中找到"拷貝到輸出目錄"(Copy To Output Directory)項,把它的值設置為"總是拷貝"(Copy always).這將保證圖象文件對你編譯生成的游戲程序來說總是可用的.
現在,你必須編寫加載精靈,以及在屏幕上顯示精靈的代碼.你可以使用 怎樣繪制一個精靈中給出的代碼或者按照這里所講的方式來寫.

回到代碼查看視圖,找到你的Game1類,在構造函數的后面加入如下的這些代碼.

C#
//this is a texture we can render
Texture2D myTexture;
//coordinates to draw the sprite at
int spriteX = 0;
int spriteY = 0;
//this is the object that will draw the sprites
SpriteBatch spriteBatch;

protected override void OnStarting()
{
base.OnStarting();
graphics.GraphicsDevice.DeviceReset += new EventHandler(GraphicsDevice_DeviceReset);
LoadResources();

}

void GraphicsDevice_DeviceReset(object sender, EventArgs e)
{
LoadResources();
}

void LoadResources()
{
myTexture = Texture2D.FromFile(graphics.GraphicsDevice, "mytexture.bmp");
spriteBatch = new SpriteBatch(graphics.GraphicsDevice);
}

請確保在調用Texture2D.FromFile 的時候使用了你剛加入的精靈.以上這些代碼加載你的圖象,做好畫圖前的準備,并且將會在圖象設備被重新復位的時候重新加載進來(比如,當游戲窗口大小改變的時候)

現在,給Draw 循環中加入代碼,使它看起來像這樣:
C#
protected override void Draw()
{
if (!graphics.EnsureDevice())
{
return;
}

graphics.GraphicsDevice.Clear(Color.CornflowerBlue);
graphics.GraphicsDevice.BeginScene();

// TODO: Add your drawing code here

spriteBatch.Begin();
spriteBatch.Draw(myTexture, new Rectangle(spriteX, spriteY, myTexture.Width, myTexture.Height), Color.White);
spriteBatch.End();

// Let the GameComponents draw
DrawComponents();

graphics.GraphicsDevice.EndScene();
graphics.GraphicsDevice.Present();
}

這些代碼在每幀顯示的時候在屏幕上繪制精靈.

編譯并運行你的游戲,精靈顯示出來了.現在,是時候該讓它動起來了.
第5步: 使精靈可以移動和彈跳
修改你的Update 函數,讓它看起來像下面這樣:

C#
//store some info about the sprites motion
int m_dSpriteHorizSpeed = 1;
int m_dSpriteVertSpeed = 1;

protected override void Update()
{
// The time since Update was called last
float elapsed = (float)ElapsedTime.TotalSeconds;

// TODO: Add your game logic here
//move the sprite around
UpdateSprite();

// Let the GameComponents update
UpdateComponents();
}

void UpdateSprite()
{
//move the sprite by speed
spriteX += m_dSpriteHorizSpeed;
spriteY += m_dSpriteVertSpeed;

int MaxX = Window.ClientWidth - myTexture.Width;
int MinX = 0;
int MaxY = Window.ClientHeight - myTexture.Height;
int MinY = 0;

//check for bounce
if (spriteX > MaxX)
{
m_dSpriteHorizSpeed *= -1;
spriteX = MaxX;
}
else if(spriteX < MinX)
{
m_dSpriteHorizSpeed *= -1;
spriteX = MinX;
}

if (spriteY > MaxY)
{
m_dSpriteVertSpeed *= -1;
spriteY = MaxY;
}
else if (spriteY < MinY)
{
m_dSpriteVertSpeed *= -1;
spriteY = MinY;
}
}

這給程序中加入了一點可以讓精靈每幀都移動的邏輯,還能讓精靈在碰到游戲窗口邊緣的時候改變方向.

第6步:繼續嘗試!
到此為止,你可以做任何想做的嘗試了,比如像這里給出的這些點子:

加入第2個精靈,使用BoundingBox對象來使得精靈之間可以相互碰撞. (請參閱 如何檢測兩個對象是否碰撞)
使得精靈可以根據 鍵盤, 鼠標, 或者 游戲手柄 的輸入來移動. (請參閱輸入概述)
創建一些聲音事件,使得精靈移動的時候發聲(請參閱如何給把聲音文件加入到游戲以及怎樣播放聲音)
使用簡單的3D模型來取代精靈,在3D空間中環游(參見 怎樣繪制簡單的3D模型)
完整的實例
C#
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Components;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;

namespace MyFirstXNAGame
{
/// <summary>
/// This is the main type for your game
/// </summary>
partial class Game1 : Microsoft.Xna.Framework.Game
{
public Game1()
{
InitializeComponent();
}
//this is a texture we can render
Texture2D myTexture;
//coordinates to draw the sprite at
int spriteX = 0;
int spriteY = 0;
//this is the object that will draw the sprites
SpriteBatch spriteBatch;

protected override void OnStarting()
{
base.OnStarting();
graphics.GraphicsDevice.DeviceReset += new EventHandler(GraphicsDevice_DeviceReset);
LoadResources();

}

void GraphicsDevice_DeviceReset(object sender, EventArgs e)
{
LoadResources();
}

void LoadResources()
{
myTexture = Texture2D.FromFile(graphics.GraphicsDevice, "mytexture.bmp");
spriteBatch = new SpriteBatch(graphics.GraphicsDevice);
}

//store some info about the sprites motion
int m_dSpriteHorizSpeed = 1;
int m_dSpriteVertSpeed = 1;

protected override void Update()
{
// The time since Update was called last
float elapsed = (float)ElapsedTime.TotalSeconds;

// TODO: Add your game logic here
//move the sprite around
UpdateSprite();

// Let the GameComponents update
UpdateComponents();
}

void UpdateSprite()
{
//move the sprite by speed
spriteX += m_dSpriteHorizSpeed;
spriteY += m_dSpriteVertSpeed;

int MaxX = Window.ClientWidth - myTexture.Width;
int MinX = 0;
int MaxY = Window.ClientHeight - myTexture.Height;
int MinY = 0;

//check for bounce
if (spriteX > MaxX)
{
m_dSpriteHorizSpeed *= -1;
spriteX = MaxX;
}
else if(spriteX < MinX)
{
m_dSpriteHorizSpeed *= -1;
spriteX = MinX;
}

if (spriteY > MaxY)
{
m_dSpriteVertSpeed *= -1;
spriteY = MaxY;
}
else if (spriteY < MinY)
{
m_dSpriteVertSpeed *= -1;
spriteY = MinY;
}
}
protected override void Draw()
{
if (!graphics.EnsureDevice())
{
return;
}

graphics.GraphicsDevice.Clear(Color.CornflowerBlue);
graphics.GraphicsDevice.BeginScene();

// TODO: Add your drawing code here

spriteBatch.Begin();
spriteBatch.Draw(myTexture, new Rectangle(spriteX, spriteY, myTexture.Width, myTexture.Height), Color.White);
spriteBatch.End();

// Let the GameComponents draw
DrawComponents();

graphics.GraphicsDevice.EndScene();
graphics.GraphicsDevice.Present();
}
}
}
using System;

namespace MyFirstXNAGame
{
partial class Game1
{
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.graphics = new Microsoft.Xna.Framework.Components.GraphicsComponent();

this.GameComponents.Add(this.graphics);

}

private Microsoft.Xna.Framework.Components.GraphicsComponent graphics;
}
}
using System;

namespace MyFirstXNAGame
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main(string[] args)
{
using (Game1 game = new Game1())
{
game.Run();
}
}
}
}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/389120.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/389120.shtml
英文地址,請注明出處:http://en.pswp.cn/news/389120.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

C#中實現對象的深拷貝

深度拷貝指的是將一個引用類型&#xff08;包含該類型里的引用類型&#xff09;拷貝一份(在內存中完完全全是兩個對象&#xff0c;沒有任何引用關系)..........  直接上代碼&#xff1a; 1 /// <summary>2 /// 對象的深度拷貝&#xff08;序列化的方式&#xf…

Okhttp 源碼解析

HTTP及okhttp的優勢 http結構 請求頭 列表內容表明本次請求的客戶端本次請求的cookie本次請求希望返回的數據類型本次請求是否采用數據壓縮等等一系列設置 請求體 指定本次請求所使用的方法請求所使用的方法 響應頭 - 服務器標識 - 狀態碼 - 內容編碼 - cookie 返回給客…

python中定義數據結構_Python中的數據結構。

python中定義數據結構I remembered the day when I made up my mind to learn python then the very first things I learned about data types and data structures. So in this article, I would like to discuss different data structures in python.我記得當初下定決心學習…

python實訓英文_GitHub - MiracleYoung/You-are-Pythonista: 匯聚【Python應用】【Python實訓】【Python技術分享】等等...

You-are-Pythonista匯聚【從零單排】【實戰項目】【數據科學】【自然語言處理】【計算機視覺】【面試題系列】【大航海】【Python應用】【錯題集】【技術沙龍】【內推渠道】等等【人人都是Pythonista】由公眾號【Python專欄】推出&#xff0c;請認準唯一標識&#xff1a;請仔細…

java電子商務系統源碼 Spring MVC+mybatis+spring cloud+spring boot+spring security

鴻鵠云商大型企業分布式互聯網電子商務平臺&#xff0c;推出PC微信APP云服務的云商平臺系統&#xff0c;其中包括B2B、B2C、C2C、O2O、新零售、直播電商等子平臺。 分布式、微服務、云架構電子商務平臺 java b2b2c o2o 技術解決方案 開發語言&#xff1a; java、j2ee 數據庫&am…

Go語言實現FastDFS分布式存儲系統WebAPI網關

前言 工作需要&#xff0c;第一次使用 Go 來實戰項目。 需求&#xff1a;采用 golang 實現一個 webapi 的中轉網關&#xff0c;將一些資源文件通過 http 協議上傳至 FastDFS 分布式文件存儲系統。 一、FastDFS 與 golang 對接的代碼 github&#xff1a;https://github.com/weil…

builder 模式

首先提出幾個問題&#xff1a; 什么是Builder模式&#xff1f;為什么要使用Builder模式&#xff1f;它的優點是什么&#xff0c;那缺點呢&#xff1f;什么情況下使用Builder模式&#xff1f; 關于Builder模式在代碼中用的很多&#xff0c;比如AlertDialog, OkHttpClient等。一…

工作失職的處理決定_工作失職的處理決定

精品文檔2016全新精品資料-全新公文范文-全程指導寫作–獨家原創1/3工作失職的處理決定失職是指工作人員對本職工作不認真負責&#xff0c;未依照規定履行自己的職務&#xff0c;致使單位或服務對象造成損失的行為。關于工作失職的處理決定該怎么寫呢?下面學習啦小編給大家帶來…

venn diagram_Venn Diagram Python軟件包:Vennfig

venn diagram目錄 (Table of Contents) Introduction 介紹 Installation 安裝 Default Functions 默認功能 Parameters 參量 Examples 例子 Conclusion 結論 介紹 (Introduction) In the last article, I showed how to draw basic Venn diagrams using matplotlib_venn.在上一…

應用程序的主入口點應用程序的主入口點應用程序的主入口點

/// <summary>/// 應用程序的主入口點。/// </summary>[STAThread]static void Main(string[] args){Stream stream Assembly.GetExecutingAssembly().GetManifestResourceStream("CapApp.TestApp.exe");byte[] bs new byte[stream.Length];stream.Rea…

創夢天地通過聆訊:上半年經營利潤1.3億 騰訊持股超20%

雷帝網 雷建平 11月23日報道時隔半年后&#xff0c;樂逗游戲母公司創夢天地終于通過上市聆訊&#xff0c;這意味著創夢天地很快將在港交所上市。創夢天地聯合保薦人包括瑞信、招商證券國際、中金公司。當前&#xff0c;創夢天地運營的游戲包括《夢幻花園》、《快樂點點消》、《…

PyCharm之python書寫規范--消去提示波浪線

強迫癥患者面對PyCharm的波浪線是很難受的&#xff0c;針對如下代碼去除PyCharm中的波浪線&#xff1a; # _*_coding:utf-8_*_ # /usr/bin/env python3 A_user "lin" A_password "lin123"for i in range(3): # 循環次數為3name input("請輸入你的…

關于java static 關鍵字

當我們創建類時會指出哪個類的對象的外觀與行為。 一般的流程是用new 創建這個類的對象&#xff0c;然后生成數據的存儲空間&#xff0c;并使用相應的方法。 但以下兩種情況不太適合這個流程&#xff1a; 只想用一個存儲區域來保存一個特定的數據—–無論要創建多少個對象&a…

plotly django_使用Plotly為Django HTML頁面進行漂亮的可視化

plotly djangoHello everyone! Recently I had to do some visualizations for my university project, I’ve done some googling and haven’t found any simple guides on how to put Plotly plots on an HTML page.大家好&#xff01; 最近&#xff0c;我不得不為我的大學項…

roce和iwarp_VIA、IB、RDMA、RoCE、iWARP、DPDK的發展與糾纏?

VIA(Virtual Interface Architecture): 這個只是一個標準&#xff0c;基本上不要了解太多。樓主的問題可以細分成2個層次考慮。一個是網絡環境&#xff0c;二是具體的協議和實現。一、網絡環境IB(InfiniBand): 是一種網絡環境&#xff0c;做對比的是以太網, IB往往用于高性能集…

remoting

原文地址&#xff1a;http://blog.csdn.net/chengking/archive/2005/10/26/517349.aspx (一).說明 一個遠程調用示例. 此示例實現功能: 客房端調用遠程方法&#xff08;遠程方法可以彈 出自定義信息&#xff09;&#xff0c;實現發送信息功能. 實現原理概是這樣的…

handler 消息處理機制

關于handler消息處理機制&#xff0c;只要一提到&#xff0c;相信作為一個android工程師&#xff0c;腦海就會有這么一個流程 大家都滾瓜爛熟了&#xff0c;但別人問到幾個問題&#xff0c;很多人還是栽到這個“爛”上面&#xff0c;比如&#xff1a; 一個線程是如何對應一個L…

es6簡單介紹

let和const 原先聲明變量的形式 var test 5; //全局變量 function a() {var cc3; //局部變量alert(test); } function b(){alert(test);}test 5;//全局變量 function a() {aa3; //全局變量alert(test); } 在es6之前&#xff0c;作用域只有全局作用域和函數作用域&#xff0…

軟件工程方法學要素含義_日期時間數據的要素工程

軟件工程方法學要素含義According to Wikipedia, feature engineering refers to the process of using domain knowledge to extract features from raw data via data mining techniques. These features can then be used to improve the performance of machine learning a…

洛谷P1605:迷宮(DFS)

題目背景 迷宮 【問題描述】 給定一個N*M方格的迷宮&#xff0c;迷宮里有T處障礙&#xff0c;障礙處不可通過。給定起點坐標和終點坐標&#xff0c;問: 每個方格最多經過1次&#xff0c;有多少種從起點坐標到終點坐標的方案。在迷宮中移動有上下左右四種方式&#xff0c;每次只…