โปรแกรมแรก โหลดภาพใส่XNA

หลังจากติดตั้งแล้ว ก็มาลองเลย.. เนื่องจากผมก็ไม่รู้เหมือนกันว่าเริ่มไง 555+ ผมเลยหาอ่านๆเอาจากลิ้งเหล่านี้

Object-Oriented Programming for XNA

XNA in a Day… Almost

XNA – Simple Sample Game

MSDN Your First Game: Microsoft XNA Game Studio in 2D

ละจะมาขอเล่าต่อให้ฟังละกัน
เย้ๆ goๆ
- เปิด visual c# express
- กดเมนู File > New Project
- เลือก XNA studio เมนูทางซ้าย ละWindows Game 3.0 เมนูทางขวา
- ตั้งชื่อ Project อยากชื่ออะไรตั้งเลยครับ
- ละก็กด OK

เอาล่ะ อยากเห็นภาพซักภาพก่อนละ ภาพไรก็ได้เอาเบสิคๆ 2dก่อนละกัน.. ว่าแล้วก็พิมพ์กูเกิ้ลไปว่า car sprite ละได้รูปรถมาคันนึง (sprite คือภาพที่สำหรับทำเกม 2d ถ้าอยากทำเกม 2d ก็ต้องใช้พวกนี้ละครับ หาได้จาก google หรือ กระทู้gamedev.netนี้) ส่วนผม google ได้รูปนี้มา (เวบตัวอย่างมันยานบิน ผมเอารถละกัน ฮ่าๆ)

car1

car1.png

add ใส่โปรเจคก่อนเลย มองทางขวาของโปรแกรม ที่ Solution explorer(ถ้าไม่มีไปหาจากเมนู view) ตรง content คลิกขวา add exiting item เลือกรูปที่จะใช้ในที่นี้คือรูปรถแดงนั่นเอง จากนั้นจะเห็นมันเพิ่มมาใน content ลองคลิกขวาดู propertie จะเห็นชื่อของมัน(asset name) คือเราสามารถเรียกใช้ด้วยชื่อนี้ได้เลย

car-asset

อยากจะบอกว่าขั้นตอนต่อไปง่ายมากก

เพิ่ม

Texture2D carTexture;
Vector2 carPosition;

ไปหลัง GraphicsDeviceManager graphics; SpriteBatch spriteBatch;

ละก็มาดู เมธอดInitialize()ซึ่งตามชื่อก็คือเริ่มเกมมาจะทำงานตรงนี้หนึ่งครั้งเท่านั้น

protected override void Initialize()
{
Window.Title = “First XNA”;

graphics.PreferredBackBufferWidth = 300;
graphics.PreferredBackBufferHeight = 500;

graphics.ApplyChanges();

base.Initialize();
}

เลข 300 500 ก็ขนาดหน้าจอนั่นล่ะ แล้วที่ต้องเพิ่มต่อไปก็..

protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
carTexture = Content.Load<Texture2D>(“car1″);
}

ข้างบนก็คือการ load ไฟล์ต่างๆมั้ง (เช่นเดียวกับ initial จะถูกเรียกครั้งแรกครั้งเดียวตอนโหลดเกม)จะเห็นว่ามี car1 ที่ใส่ไปตอนแรก

สุดท้ายแล้ว

protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
spriteBatch.Draw(carTexture, carPosition, Color.White);
spriteBatch.End();
base.Draw(gameTime);

}

กด F5 รันนน ได้ละ ง่ายจังแหะ..

first-xna

~ โดย iZhT บน มีนาคม 18, 2009.

ใส่ความเห็น

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / เปลี่ยนแปลง )

Twitter picture

You are commenting using your Twitter account. Log Out / เปลี่ยนแปลง )

Facebook photo

You are commenting using your Facebook account. Log Out / เปลี่ยนแปลง )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.