مرحبا مبرمجي XNA الأكارم :
اقدم لكم جزء مبسط من تصميم اللعبة من البداية وهي أضافة القائمة الرئيسية للألعاب المصممة بـ XNA
وسوف أقدم كيفية برمجة القوائم الرئيسية للتصميم اية لعبة كانت
وهذه نبذة مختصرة للقائمة الرئيسية عند الدخول للعبة من البداية
وهذا مثال يوضح ذلك :
http://rapidshare.com/files/28885845...NA.ccgame.html
قبل تشغيل المثال يجب عليك تنزيل النسخة الأحدث من XNA 3.1 لتشغيلها بالشكل الصحيح ....
او تحميل البرنامج : XNA Framework Redistributable 3.1
http://www.microsoft.com/downloads/d...1-98eb3e799ef2
والبقية الشرح على مراحل في هذا الموضوع هنا
ملاحظة هامة :
اذا كان الشرح غير واضح الرجاء وضع ملاحظاته في هذا الموضوع لأعادة صياغة الشرح بطريقة أخرى وبالتفصيل الكامل
واليكم الكتاب الكامل لتعليم #C باللغة العربية
http://rapidshare.com/files/28890320..._____.rar.html
والكتاب الكامل لتعليم XNA 3.0 باللغة الأنجليزية
http://rapidshare.com/files/28890439...A_3.0.rar.html
اقدم لكم جزء مبسط من تصميم اللعبة من البداية وهي أضافة القائمة الرئيسية للألعاب المصممة بـ XNA
وسوف أقدم كيفية برمجة القوائم الرئيسية للتصميم اية لعبة كانت
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using System;
using System.Collections.Generic;
using XnaGraphicEngine.Game;
using XnaGraphicEngine.Graphics;
using Texture = XnaGraphicEngine.Graphics.Texture;
using Model = XnaGraphicEngine.Graphics.Model;
using XnaGraphicEngine.Helpers;
using XnaGraphicEngine.GameScreens;
using XnaGraphicEngine.Sounds;
using XnaGraphicEngine.Properties;
//ادخال مكتبات xna للأستدعائها عند البرمجة وهي مهمة جداً للتنفيذ بشكل سلس بدون أية أخطاء
namespace XnaGraphicEngine
//تحديد اسم المشروع في بداية البرمجة
{
public class YourGame : BaseGame
// تحديد اسم الكلس الأول للبرمجة مكونات المشروع المراد عمله
{
#region Variables
/// <summary>
/// من هنا اضافة تعريف المكونات ظهور الشاشة الأولى
/// وهذا هو منطق اللعبة من البداية
/// </summary>
private Stack<IGameScreen> gameScreens = new Stack<IGameScreen>();
/// <summary>
///طريقة تحميل القائمة الرئيسية للعبة
/// </summary>
public Texture mainMenuTexture = null,
mouseCursorTexture = null;
// انشاء متغير للظهور الماوس على الشاشة
public Texture hudTopTexture = null,
// انشاء متغير عند الضغط على الماوس
hudBottomTexture = null;
#region Constructor
/// <summary>
/// كود انشاء اللعبة
/// </summary>
public YourGame()
{
// برمجة عدم تفعيل ظهور الماوس عند ظهور القائمة الرئيسية للعبة
this.IsMouseVisible = false;
}
#region Initialize
/// <summary>
/// اضافة مكونات اللعبة وتحميل التحكمات الرئيسية للعبة
/// </summary>
protected override void Initialize()
{
base.Initialize();
//اظهار مؤشر الماوس في الوسط الشاشة
Input.Update();
Input.MousePos = new Point(
Window.ClientBounds.X + width / 2,
Window.ClientBounds.Y + height / 2);
Input.Update();
// تحميل صورة القائمة مع صورة الماوس
mainMenuTexture = new Texture("XNA2");
mouseCursorTexture = new Texture("MouseCursor");
hudTopTexture = new Texture("HudTop");
hudBottomTexture = new Texture("HudBottom");
//هنا اضافة اظهار القائمة الرئيسية
gameScreens.Push(new MainMenu());
// اضافة بداية اللعبة
//gameScreens.Push(new Mission());
//inGame = true;
// وهنا اضافة الصوت للعبة وهو الموسيقى
Sound.StartMusic();
} // Initialize()
#endregion
/// <summary>
/// تفعيل الموسيقى تشغيل او اغلاق
/// </summary>
public void ToggleMusicOnOff()
{
if (GameSettings.Default.MusicOn)
Sound.StopMusic();
else
Sound.StartMusic();
}
//اضافة شاشة المقدمة للعبة
static bool inGame = false;
public static bool InGame
{
get
{
return inGame;
}
}
/// <summary>
/// حجز مكان للذاكرة للظهور اول شاشة
/// </summary>
/// <param name="newGameScreen">New game screen</param>
public void AddGameScreen(IGameScreen newGameScreen)
{
gameScreens.Push(newGameScreen);
inGame = newGameScreen.GetType() == typeof(Mission);
}
/// <summary>
/// عملية ازاحة اومسح الشاشة الحالية للأفراغ الذاكرة
/// </summary>
public void RemoveCurrentGameScreen()
{
gameScreens.Pop();
inGame = gameScreens.Count > 0 &&
gameScreens.Peek().GetType() == typeof(Mission);
}
/// <summary>
/// هنا يتم اضافة رسم القائمة الخلفية اللعبة
/// </summary>
public void RenderMenuBackground()
{
// ادخال ظهور صورة القائمة الرئيسية شفافة امام النص .
BaseGame.EnableAlphaBlending();
// تحديد وضع امداد الصورة على كامل الشاشة
mainMenuTexture.RenderOnScreen(
BaseGame.ResolutionRect,
new Rectangle(0, 0, 1024, 768));
}
public static void RenderGameBackground()
{
//TODO
}
// هنا يتم اضافة ازرار او كبسات للقائمة
public bool RenderMenuButton(
MenuButton buttonType, Point pos)
{
//امكنة ظهور الضغط الزر على الشاشة
Rectangle rect = new Rectangle(
pos.X * BaseGame.Width / 1024,
pos.Y * BaseGame.Height / 768,
200 * BaseGame.Width / 1024,
77 * BaseGame.Height / 768);
Rectangle innerRect = new Rectangle(
rect.X, rect.Y + rect.Height / 5,
rect.Width, rect.Height * 3 / 5);
bool highlight = Input.MouseInBox(
innerRect);
// هنا يتم أضافة اصوات النقرات للقائمة
if (highlight &&
Input.MouseWasNotInRectLastFrame(innerRect))
Sound.Play(Sound.Sounds.Highlight);
int buttonNum = (int)buttonType;
Rectangle pixelRect = new Rectangle(3 + 204 * buttonNum,
900 + 80 * (highlight ? 1 : 0), 200, 77);
//هنا يتم الرسم للقائمة وهي الصورة القائمة
mainMenuTexture.RenderOnScreen(rect, pixelRect);
bool ret =
(Input.MouseLeftButtonJustPressed ||
Input.GamePadAJustPressed) &&
this.IsActive &&
highlight;
if (buttonType == MenuButton.Back &&
Input.GamePadBackJustPressed)
ret = true;
if (buttonType == MenuButton.Missions &&
Input.GamePadStartPressed)
ret = true;
if (ret == true)
Sound.Play(Sound.Sounds.Click);
return ret;
}
/// <summary>
/// هنا يتم رسم الماوس على الشاشة
/// </summary>
public void RenderMouseCursor()
{
int mouseAnimationStep = (int)(BaseGame.TotalTimeMs / 100) % 4;
mouseCursorTexture.RenderOnScreen(
new Rectangle(Input.MousePos.X, Input.MousePos.Y, 60 * 2 / 3, 64 * 2 / 3),
new Rectangle(64 * mouseAnimationStep, 0, 60, 64));
SpriteHelper.DrawSprites(width, height);
}
// أستدعاء الدالة update لحركة الأطارات frame 60 مرة لكل ثانية تقريباً
protected override void Update(GameTime gameTime)
{
if (gameScreens.Count > 0 &&
gameScreens.Peek().Quit)
RemoveCurrentGameScreen();
// ازالة مكان للذاكرة عند الخروج من القائمة الحالية
if (gameScreens.Count == 0)
{
if (this.GetType() != typeof(TestGame))
Exit();
}
base.Update(gameTime);
} // للتحديث الرسم
protected override void Draw(GameTime gameTime)
{
ClearBackground();
// لمسح الشاشة لأضافة رسم المكونات والصور والتأثيرات effect
// هنا يتم رسم التأثيرات الشيدر على الشاشة الرئيسية
BaseGame.GlowShader.Start();
try
{
if (gameScreens.Count > 0)
gameScreens.Peek().Run(this);
}
catch (Exception ex)
{
Log.Write("Failed to execute " + gameScreens.Peek().Name +
"\nError: " + ex.ToString());
}
base.Draw(gameTime);
// يتم أظهار الماوس
if (inGame == false &&
gameScreens.Count > 0)
RenderMouseCursor();
else
{
// في بداية اي لعبة يجب اظهار الماوس في الوسط
Input.CenterMouse();
}
// وهنا يتم اظهار التاثير الثنائي والثلاثي البعد للشيدر على الشاشة
glowShader.Show();
}
/// <summary>
/// وهنا يتم اضافة بداية تحميل اللعبة
/// </summary>
public static void StartGame()
{
using (YourGame game = new YourGame())
{
game.Run();
}
GameSettings.Save();
{
{
{
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using System;
using System.Collections.Generic;
using XnaGraphicEngine.Game;
using XnaGraphicEngine.Graphics;
using Texture = XnaGraphicEngine.Graphics.Texture;
using Model = XnaGraphicEngine.Graphics.Model;
using XnaGraphicEngine.Helpers;
using XnaGraphicEngine.GameScreens;
using XnaGraphicEngine.Sounds;
using XnaGraphicEngine.Properties;
//ادخال مكتبات xna للأستدعائها عند البرمجة وهي مهمة جداً للتنفيذ بشكل سلس بدون أية أخطاء
namespace XnaGraphicEngine
//تحديد اسم المشروع في بداية البرمجة
{
public class YourGame : BaseGame
// تحديد اسم الكلس الأول للبرمجة مكونات المشروع المراد عمله
{
#region Variables
/// <summary>
/// من هنا اضافة تعريف المكونات ظهور الشاشة الأولى
/// وهذا هو منطق اللعبة من البداية
/// </summary>
private Stack<IGameScreen> gameScreens = new Stack<IGameScreen>();
/// <summary>
///طريقة تحميل القائمة الرئيسية للعبة
/// </summary>
public Texture mainMenuTexture = null,
mouseCursorTexture = null;
// انشاء متغير للظهور الماوس على الشاشة
public Texture hudTopTexture = null,
// انشاء متغير عند الضغط على الماوس
hudBottomTexture = null;
#region Constructor
/// <summary>
/// كود انشاء اللعبة
/// </summary>
public YourGame()
{
// برمجة عدم تفعيل ظهور الماوس عند ظهور القائمة الرئيسية للعبة
this.IsMouseVisible = false;
}
#region Initialize
/// <summary>
/// اضافة مكونات اللعبة وتحميل التحكمات الرئيسية للعبة
/// </summary>
protected override void Initialize()
{
base.Initialize();
//اظهار مؤشر الماوس في الوسط الشاشة
Input.Update();
Input.MousePos = new Point(
Window.ClientBounds.X + width / 2,
Window.ClientBounds.Y + height / 2);
Input.Update();
// تحميل صورة القائمة مع صورة الماوس
mainMenuTexture = new Texture("XNA2");
mouseCursorTexture = new Texture("MouseCursor");
hudTopTexture = new Texture("HudTop");
hudBottomTexture = new Texture("HudBottom");
//هنا اضافة اظهار القائمة الرئيسية
gameScreens.Push(new MainMenu());
// اضافة بداية اللعبة
//gameScreens.Push(new Mission());
//inGame = true;
// وهنا اضافة الصوت للعبة وهو الموسيقى
Sound.StartMusic();
} // Initialize()
#endregion
/// <summary>
/// تفعيل الموسيقى تشغيل او اغلاق
/// </summary>
public void ToggleMusicOnOff()
{
if (GameSettings.Default.MusicOn)
Sound.StopMusic();
else
Sound.StartMusic();
}
//اضافة شاشة المقدمة للعبة
static bool inGame = false;
public static bool InGame
{
get
{
return inGame;
}
}
/// <summary>
/// حجز مكان للذاكرة للظهور اول شاشة
/// </summary>
/// <param name="newGameScreen">New game screen</param>
public void AddGameScreen(IGameScreen newGameScreen)
{
gameScreens.Push(newGameScreen);
inGame = newGameScreen.GetType() == typeof(Mission);
}
/// <summary>
/// عملية ازاحة اومسح الشاشة الحالية للأفراغ الذاكرة
/// </summary>
public void RemoveCurrentGameScreen()
{
gameScreens.Pop();
inGame = gameScreens.Count > 0 &&
gameScreens.Peek().GetType() == typeof(Mission);
}
/// <summary>
/// هنا يتم اضافة رسم القائمة الخلفية اللعبة
/// </summary>
public void RenderMenuBackground()
{
// ادخال ظهور صورة القائمة الرئيسية شفافة امام النص .
BaseGame.EnableAlphaBlending();
// تحديد وضع امداد الصورة على كامل الشاشة
mainMenuTexture.RenderOnScreen(
BaseGame.ResolutionRect,
new Rectangle(0, 0, 1024, 768));
}
public static void RenderGameBackground()
{
//TODO
}
// هنا يتم اضافة ازرار او كبسات للقائمة
public bool RenderMenuButton(
MenuButton buttonType, Point pos)
{
//امكنة ظهور الضغط الزر على الشاشة
Rectangle rect = new Rectangle(
pos.X * BaseGame.Width / 1024,
pos.Y * BaseGame.Height / 768,
200 * BaseGame.Width / 1024,
77 * BaseGame.Height / 768);
Rectangle innerRect = new Rectangle(
rect.X, rect.Y + rect.Height / 5,
rect.Width, rect.Height * 3 / 5);
bool highlight = Input.MouseInBox(
innerRect);
// هنا يتم أضافة اصوات النقرات للقائمة
if (highlight &&
Input.MouseWasNotInRectLastFrame(innerRect))
Sound.Play(Sound.Sounds.Highlight);
int buttonNum = (int)buttonType;
Rectangle pixelRect = new Rectangle(3 + 204 * buttonNum,
900 + 80 * (highlight ? 1 : 0), 200, 77);
//هنا يتم الرسم للقائمة وهي الصورة القائمة
mainMenuTexture.RenderOnScreen(rect, pixelRect);
bool ret =
(Input.MouseLeftButtonJustPressed ||
Input.GamePadAJustPressed) &&
this.IsActive &&
highlight;
if (buttonType == MenuButton.Back &&
Input.GamePadBackJustPressed)
ret = true;
if (buttonType == MenuButton.Missions &&
Input.GamePadStartPressed)
ret = true;
if (ret == true)
Sound.Play(Sound.Sounds.Click);
return ret;
}
/// <summary>
/// هنا يتم رسم الماوس على الشاشة
/// </summary>
public void RenderMouseCursor()
{
int mouseAnimationStep = (int)(BaseGame.TotalTimeMs / 100) % 4;
mouseCursorTexture.RenderOnScreen(
new Rectangle(Input.MousePos.X, Input.MousePos.Y, 60 * 2 / 3, 64 * 2 / 3),
new Rectangle(64 * mouseAnimationStep, 0, 60, 64));
SpriteHelper.DrawSprites(width, height);
}
// أستدعاء الدالة update لحركة الأطارات frame 60 مرة لكل ثانية تقريباً
protected override void Update(GameTime gameTime)
{
if (gameScreens.Count > 0 &&
gameScreens.Peek().Quit)
RemoveCurrentGameScreen();
// ازالة مكان للذاكرة عند الخروج من القائمة الحالية
if (gameScreens.Count == 0)
{
if (this.GetType() != typeof(TestGame))
Exit();
}
base.Update(gameTime);
} // للتحديث الرسم
protected override void Draw(GameTime gameTime)
{
ClearBackground();
// لمسح الشاشة لأضافة رسم المكونات والصور والتأثيرات effect
// هنا يتم رسم التأثيرات الشيدر على الشاشة الرئيسية
BaseGame.GlowShader.Start();
try
{
if (gameScreens.Count > 0)
gameScreens.Peek().Run(this);
}
catch (Exception ex)
{
Log.Write("Failed to execute " + gameScreens.Peek().Name +
"\nError: " + ex.ToString());
}
base.Draw(gameTime);
// يتم أظهار الماوس
if (inGame == false &&
gameScreens.Count > 0)
RenderMouseCursor();
else
{
// في بداية اي لعبة يجب اظهار الماوس في الوسط
Input.CenterMouse();
}
// وهنا يتم اظهار التاثير الثنائي والثلاثي البعد للشيدر على الشاشة
glowShader.Show();
}
/// <summary>
/// وهنا يتم اضافة بداية تحميل اللعبة
/// </summary>
public static void StartGame()
{
using (YourGame game = new YourGame())
{
game.Run();
}
GameSettings.Save();
{
{
{
وهذه نبذة مختصرة للقائمة الرئيسية عند الدخول للعبة من البداية
وهذا مثال يوضح ذلك :
http://rapidshare.com/files/28885845...NA.ccgame.html
قبل تشغيل المثال يجب عليك تنزيل النسخة الأحدث من XNA 3.1 لتشغيلها بالشكل الصحيح ....
او تحميل البرنامج : XNA Framework Redistributable 3.1
http://www.microsoft.com/downloads/d...1-98eb3e799ef2
والبقية الشرح على مراحل في هذا الموضوع هنا
ملاحظة هامة :
اذا كان الشرح غير واضح الرجاء وضع ملاحظاته في هذا الموضوع لأعادة صياغة الشرح بطريقة أخرى وبالتفصيل الكامل
تعلم أساسيات لغة #C أولاً
اذا لم تكن تعرف عن اوامر #C اساساً اليك السلسلة التعليمية وباللغة العربية لـ #C فقط واليكم الكتاب الكامل لتعليم #C باللغة العربية
http://rapidshare.com/files/28890320..._____.rar.html
والكتاب الكامل لتعليم XNA 3.0 باللغة الأنجليزية
http://rapidshare.com/files/28890439...A_3.0.rar.html
ترقبونا دائماً كل ما هو جديد من دروس
XNA
تعليق