بســم الله الـرحمــن الرحيــم الدرس الثاني <<== إذهب إلى الدرس السابق أهلا بكم في الدرس الثاني من سلسلة دروس تعلم ال Xna , في هذا الدرس سوف نقوم برسم خلفية اللعبة. الخطوة الأولى في عملية إنشاء لعبة ثنائية الأبعاد هي عملية تصيير “Render” صورة ثنائية الأبعاد. في ال Xna, من السهل جدا عمل ذلك. كمثال أول سوف نقوم بتصيير صورتين على كامل الشاشة. الأولى سوف تكون صورة الأرضية الخلفية و التي تحتوي على مشهد السماء و الجبال. وفوق هذه الصورة سوف نقوم بتصيير صورة الأرضية الأمامية التي تحتوي على التضاريس. بداية يجب علينا تنزيل الصور من خلال الروابط التالية هناو الرابط هنا(أو من خلال المرفقات). الآن إذهب إلى المشروع الذي قمنا بإنشائه في الدرس السابق, و إذهب إلى “Solution Explorer” مربع صغير في الركن العلوي الأيسر من الشاشة, يحتوي على كل ملفات المشروع. بعدها ابحث عن مجلد ال “Content” و بعدها إضغط بالزر اليمين على المجلد و إختر Add->Existing Item , كما هو موضح في الصورة التالية: من خلال الشاشة المفتوحة قم بالتصفح حتى تصل إلى موقع الصورتان اللتان قمت بتنزيلهما سابقا. إختر ملفات الصوره و بعدها إضغط إضافة "Add" . هكذا سوف يتم نسخ الملف إلى مجلد ال “Content” في مشروعك. يجب أن يظهر الملف اسفل مجلد ال “Content” كما في الصورة: بعد تحميل الصوره إلى مشروع ال Xna الخاص بك, يجب أن نقوم بإنشاء متغير في الكود, بحيث يتم ربط المتغير مع الصورة. وجود المتغير هو شرط أساسي, مما يمكننا من الوصول إلى الصورة من خلال الكود. قم بإضافة المتغير في أعلى الكود , فوق الدالة “Game1()” .
Texture2D backgroundTexture;
backgroundTexture = Content.Load<Texture2D> ("background");
private void DrawScenery() { }
int screenWidth; int screenHeight;
screenWidth = device.PresentationParameters.BackBufferWidth; screenHeight = device.PresentationParameters.BackBufferHeight;
Rectangle screenRectangle = new Rectangle(0, 0, screenWidth, screenHeight);spriteBatch.Draw(backgroundTexture, screenRectangle, Color.White);
protected override void Draw(GameTime gameTime) { graphics.GraphicsDevice.Clear(Color.CornflowerBlue); spriteBatch.Begin(); DrawScenery(); spriteBatch.End(); base.Draw(gameTime); }
Texture2D foregroundTexture;
foregroundTexture = Content.Load<Texture2D> ("foreground");
spriteBatch.Draw(foregroundTexture, screenRectangle, Color.White);
using System; using System.Collections.Generic; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Audio; using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Framework.GamerServices; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input; using Microsoft.Xna.Framework.Net; using Microsoft.Xna.Framework.Storage; namespace XNAtutorial { public class Game1 : Microsoft.Xna.Framework.Game { GraphicsDeviceManager graphics; SpriteBatch spriteBatch; GraphicsDevice device; Texture2D backgroundTexture; Texture2D foregroundTexture; int screenWidth; int screenHeight; public Game1() { graphics = new GraphicsDeviceManager(this); Content.RootDirectory = "Content"; } protected override void Initialize() { graphics.PreferredBackBufferWidth = 500; graphics.PreferredBackBufferHeight = 500; graphics.IsFullScreen = false; graphics.ApplyChanges(); Window.Title = "Riemer's 2D XNA Tutorial"; base.Initialize(); } protected override void LoadContent() { device = graphics.GraphicsDevice; spriteBatch = new SpriteBatch(device); backgroundTexture = Content.Load ("background"); foregroundTexture = Content.Load ("foreground"); screenWidth = device.PresentationParameters.BackBufferWidth; screenHeight = device.PresentationParameters.BackBufferHeight; } protected override void UnloadContent() { } protected override void Update(GameTime gameTime) { if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) this.Exit(); base.Update(gameTime); } protected override void Draw(GameTime gameTime) { graphics.GraphicsDevice.Clear(Color.CornflowerBlue); spriteBatch.Begin(); DrawScenery(); spriteBatch.End(); base.Draw(gameTime); } private void DrawScenery() { Rectangle screenRectangle = new Rectangle(0, 0, screenWidth, screenHeight); spriteBatch.Draw(backgroundTexture, screenRectangle, Color.White); spriteBatch.Draw(foregroundTexture, screenRectangle, Color.White); } } }