SlideShare uma empresa Scribd logo
1 de 24
XNA Game Development.


Exercise 1:

Hello World in XNA.



To write a hello world game, first you need to load font to the content folder.

Right click on the content folder and Add->new item
And select Spite font and rename that as Arial.spritefont

Arial is the name of the font we are going to use.

You can use any font that install in the Windows font folder.




Go to Arial.spritefont file



if you like you can change the size and style by modifing Size and Style tags.



<Size>14</Size>

<Style>Regular</Style>
Now in the Game 1.cs you need to write code to draw a string in the screen.



First create a object form SpriteFont to handle our font.

SpriteFont myfont;




Then in the LoadContent() method you can load the font.



myfont = Content.Load<SpriteFont>("Arial");



then in the Draw() metord you can draw the font in the sceran.




spriteBatch.Begin();          / /start the sprite batch process to draw font




spriteBatch.DrawString(myfont, "Hello world", new Vector2(10.0f, 10.0f),
Color.Black);

  //draw the font in the screan



spriteBatch.End();          //end the sprite batch process




Now you can run the project by pressing F5 or by clicking the run button.
Exercise 2:

Draw 2d image.



First you need to load an image to the content folder.

Right click on the content folder and Add->existing items




And select an image form the hard drive.

It could be .bmp,.png,.jpg ,.dds,.tga
Then create a object form Texture2D to handle the image.

Texture2D mytx;




Then in the LoadContent() method you can load the image.



mytx = Content.Load<Texture2D>("img");



then in the Draw() metord you can draw the image in the sceran.




spriteBatch.Begin();          / /start the sprite batch process to draw font




spriteBatch.Draw(mytx,new Rectangle(100,100,600,400), Color.White);




spriteBatch.End();          //end the sprite batch process




Now you can run the project by pressing F5 or by clicking the run button.
Modify the code for 2d animation.



Also you can create 2d animation to this example using these lines of codes.



First initialize a float variable to the animation.



 float val = 0.0f;



Then in Update() metord increment the value to change the position.



val = val + 0.3f;




Finally in the Draw() metord increment the x y positions of the image.



spriteBatch.Draw(mytx,new Rectangle(100+(int)val,100+(int)val,600,400),
Color.White);




Then after press F5 you are ready to play the animation.
Exercise 3:

Using Keyboard and Mouse in a PC game.



First you need to have an image to represent mouse cursor and another image to show the keyboard
usage.



You can add those files in to the content folder.




In the Game1.cs file initialize these variables.



KeyboardState mykeyboardstate;

//create a keyboardstate                 object to get the state of the keyboard
MouseState mymousesatate;

//get the state of the mouse



 SpriteFont myfont;

//add a sprite font to draw String in the screan




Texture2D mytexture, mytexture2;//creating texture object



float Position = 0.0f;//initializing up down position



float Position2 = 0.0f;//initializing left right position



float mousex = 0.0f;//initilizing mousex position



float mousey = 0.0f;//initializing mousey position




Now in the LoadContent() metord write code for load the assest.

//change the window title
Window.Title = "Using the keyboard+mouse-demo";


myfont = Content.Load<SpriteFont>("Arial");

mytexture = Content.Load<Texture2D>("pic");
mytexture2 = Content.Load<Texture2D>("cursor");




Then in the Update() methord you can chack for keyboard and mouse
inputs.



mykeyboardstate = Keyboard.GetState();//capturing the keybard state



mymousesatate = Mouse.GetState();//capturing mouse state



mousex = (float)mymousesatate.X;//getting the x position of the mouse



mousey = (float)mymousesatate.Y;//getting the y position of the mouse




// Move 400 pixels each second

  float moveFactorPerSecond = 80 *
(float)gameTime.ElapsedRealTime.TotalMilliseconds / 1000.0f;




//chage the position according to the key pressed up,down,left,right



 if (mykeyboardstate.IsKeyDown(Keys.Up))

        Position -= moveFactorPerSecond;



  if (mykeyboardstate.IsKeyDown(Keys.Down))

         Position += moveFactorPerSecond;
if (mykeyboardstate.IsKeyDown(Keys.Left))

            Position2 -= moveFactorPerSecond;




 if (mykeyboardstate.IsKeyDown(Keys.Right))

              Position2 += moveFactorPerSecond;




In the Draw () method you can see the Changes of the inputs by drawing the images.




myspitebatch.Begin();//start process



Vector2 position = new Vector2(200.0f + (int)Position2, 200.0f +
((int)Position));

  //setting position with variables (Position1) and (Position2)

  //those variables change the position of the image according to the
key pressing



 myspitebatch.Draw(mytexture, position, Color.White);//drawing the
image



 myspitebatch.DrawString(myfont, "Use Arrowkeys to move the image &
use mouse to point", new Vector2(10.0f, 10.0f), Color.Gold);//drawing
text on the screan
myspitebatch.Draw(mytexture2, new Vector2(mousex, mousey),
Color.White);

          //drawing the cursor image, acording to the mouse position



 myspitebatch.End();//end process




Now you can run the project by pressing F5 or by clicking the run button




Exercise 4:

Crating a Menu system in a PC game.
For the Game state you need to add some images to show the user inputs



You can add those files in to the content folder.




Then you are able to write a code for create a menu system in a XNA game.



In the Game1.cs file initialize these variables.



KeyboardState mykeyboardstate;

//create a keyboardstate                 object to get the state of the keyboard
MouseState mymousesatate;//get the state of the mouse



SpriteFont myfont;//add spite batch and spite font as explained in
tutorial 01-hello world



Texture2D mytexture, mytexture2;//creating texture object



float Position = 0.0f;//initializing up down position



float Position2 = 0.0f;//initializing left right position



float mousex = 0.0f;//initilizing mousex position



float mousey = 0.0f;//initializing mousey position




Using enum we can define the different game states.Then we set the initial stage as start state.




enum Mygamemode

           {

                 start,

                 game,

                 exit



          }    //define game modes start,game,exit
Mygamemode mygame = Mygamemode.start;//assign start mode as initial
mode




Now in the LoadContent() metord write code for load the assest



Window.Title = "Using menu system";



myspitebatch = new SpriteBatch(graphics.GraphicsDevice);//setting
spite batch for graphic device




 myfont = Content.Load<SpriteFont>("Arial"); //loading font



mytexture = Content.Load<Texture2D>("pic");//loding image



 mytexture2 = Content.Load<Texture2D>("cursor");//loding cursor image




Then in the Update() methord you can chack for keyboard and mouse
inputs.



mykeyboardstate = Keyboard.GetState();//capturing the state



 mymousesatate = Mouse.GetState();//capturing mouse state
mousex = (float)mymousesatate.X;//getting the x position of the mouse



 mousey = (float)mymousesatate.Y;//getting the y position of the mouse




// Move 400 pixels each second



 float moveFactorPerSecond = 80 *

               (float)gameTime.ElapsedRealTime.TotalMilliseconds /
1000.0f;




//chage the position according to the key pressed up,down,left,right



if (mykeyboardstate.IsKeyDown(Keys.Up))

               Position -= moveFactorPerSecond;




if (mykeyboardstate.IsKeyDown(Keys.Down))

               Position += moveFactorPerSecond;



if (mykeyboardstate.IsKeyDown(Keys.Left))

               Position2 -= moveFactorPerSecond;
if (mykeyboardstate.IsKeyDown(Keys.Right))

                      Position2 += moveFactorPerSecond;




In the Draw () method you can see the Changes by drawing the images.




if (mygame == Mygamemode.start) //chack whether you are in the start
mode



  {



 myspitebatch.Begin();




 myspitebatch.DrawString(myfont, "Welcome to the demo ", new
Vector2(30.0f, 30.0f), Color.YellowGreen);



myspitebatch.DrawString(myfont, "Press Enter to continue..... ", new
Vector2(30.0f, 80.0f), Color.YellowGreen);

                      myspitebatch.End();
if (mykeyboardstate.IsKeyDown(Keys.Enter))//chack whather user press
enter key while he/she in the start mode




mygame = Mygamemode.game;   //if(true) assign game mode




 }




if (mygame == Mygamemode.game)//chack whether you are in the game mode



{

              myspitebatch.Begin();//start process




 Vector2 position = new Vector2(200.0f + (int)Position2, 200.0f +
((int)Position));



//setting position with variables (Position1) and (Position2)




  //those variables change the position of the image according to the
key pressing
myspitebatch.Draw(mytexture, position, Color.White);//drawing the
image




  myspitebatch.DrawString(myfont, "Use Arrowkeys to move the image &
use mouse to point", new Vector2(10.0f, 10.0f), Color.Gold);//drawing
text on the screan




myspitebatch.DrawString(myfont, "Use Esc to exit demo", new
Vector2(10.0f, 30.0f), Color.Gold);//drawing text on the screan




 myspitebatch.Draw(mytexture2, new Vector2(mousex, mousey),
Color.White);




//drawing the cursor image, acording to the mouse position

               myspitebatch.End();//end process




 if (mykeyboardstate.IsKeyDown(Keys.Escape))//chack whather user press
escape key while he/she in the start mode
mygame = Mygamemode.exit;      //if(true) assign exit mode




}




 if (mygame == Mygamemode.exit) //chack whether you are in the exit
mode




{   myspitebatch.Begin();




   myspitebatch.DrawString(myfont, "Goodbuy from the demo ", new
Vector2(30.0f, 30.0f), Color.YellowGreen);




 myspitebatch.DrawString(myfont, "Press Q to exit..... ", new
Vector2(30.0f, 80.0f), Color.YellowGreen);




myspitebatch.End();




  if (mykeyboardstate.IsKeyDown(Keys.Q))//chack whather user press
enter key while he/she in the start mode
this.Exit();                     //if(true) assign game mode

}




Now you can run the project by pressing F5 or by clicking the run button.




Exercise 5:
First you need to add Music files (.mp3, .wav) to the content folder in a new XNA project.




In the Game1.cs file initialize these variables.



Song mysong;//initialize the song



SpriteFont myfont;//initialize font



 String val;



 Texture2D mytx;//initialize texture
Now in the LoadContent() metord write code for load the assest



    Window.AllowUserResizing = true;




    //first add new mp3 to a content folder



mysong = Content.Load<Song>("sum41");//load the mp3



myfont = Content.Load<SpriteFont>("Arial");//load the font



mytx = Content.Load<Texture2D>("B");//load the image




    MediaPlayer.Play(mysong);//play the song




Then in the Update() methord you can write this code segment.

//simple text effect



if (gameTime.TotalGameTime.Seconds % 2 == 0)



{

    val = "Playing -sum 41"

 }
else



 {



  val = "";




 }




In the Draw () method you can see the Changes by drawing the images.




spriteBatch.Begin();           //begin spite batch prosess




 spriteBatch.Draw(mytx, new Rectangle(0, 0, Window.ClientBounds.Width,
Window.ClientBounds.Height),

                      Color.White);

//draw image




spriteBatch.DrawString(myfont, val,

                     new Vector2(30.0f, 30.0f), Color.Orange);

//draw the font
spriteBatch.End();//end spite batch process


Now you can run the project by pressing F5 or by clicking the run button.

Mais conteúdo relacionado

Mais procurados

Introduction to Game Programming: Using C# and Unity 3D - Chapter 7 (Preview)
Introduction to Game Programming: Using C# and Unity 3D - Chapter 7 (Preview)Introduction to Game Programming: Using C# and Unity 3D - Chapter 7 (Preview)
Introduction to Game Programming: Using C# and Unity 3D - Chapter 7 (Preview)noorcon
 
The Ring programming language version 1.7 book - Part 53 of 196
The Ring programming language version 1.7 book - Part 53 of 196The Ring programming language version 1.7 book - Part 53 of 196
The Ring programming language version 1.7 book - Part 53 of 196Mahmoud Samir Fayed
 
Introduction to Game Programming: Using C# and Unity 3D - Chapter 2 (Preview)
Introduction to Game Programming: Using C# and Unity 3D - Chapter 2 (Preview)Introduction to Game Programming: Using C# and Unity 3D - Chapter 2 (Preview)
Introduction to Game Programming: Using C# and Unity 3D - Chapter 2 (Preview)noorcon
 
Introduction to Game Programming: Using C# and Unity 3D - Chapter 3 (Preview)
Introduction to Game Programming: Using C# and Unity 3D - Chapter 3 (Preview)Introduction to Game Programming: Using C# and Unity 3D - Chapter 3 (Preview)
Introduction to Game Programming: Using C# and Unity 3D - Chapter 3 (Preview)noorcon
 
The Ring programming language version 1.2 book - Part 50 of 84
The Ring programming language version 1.2 book - Part 50 of 84The Ring programming language version 1.2 book - Part 50 of 84
The Ring programming language version 1.2 book - Part 50 of 84Mahmoud Samir Fayed
 
Creating Games for Asha - platform
Creating Games for Asha - platformCreating Games for Asha - platform
Creating Games for Asha - platformJussi Pohjolainen
 
The Ring programming language version 1.5.4 book - Part 48 of 185
The Ring programming language version 1.5.4 book - Part 48 of 185The Ring programming language version 1.5.4 book - Part 48 of 185
The Ring programming language version 1.5.4 book - Part 48 of 185Mahmoud Samir Fayed
 
The Ring programming language version 1.5.2 book - Part 49 of 181
The Ring programming language version 1.5.2 book - Part 49 of 181The Ring programming language version 1.5.2 book - Part 49 of 181
The Ring programming language version 1.5.2 book - Part 49 of 181Mahmoud Samir Fayed
 
The Ring programming language version 1.5.1 book - Part 48 of 180
The Ring programming language version 1.5.1 book - Part 48 of 180The Ring programming language version 1.5.1 book - Part 48 of 180
The Ring programming language version 1.5.1 book - Part 48 of 180Mahmoud Samir Fayed
 
libGDX: Simple Frame Animation
libGDX: Simple Frame AnimationlibGDX: Simple Frame Animation
libGDX: Simple Frame AnimationJussi Pohjolainen
 
The Ring programming language version 1.10 book - Part 70 of 212
The Ring programming language version 1.10 book - Part 70 of 212The Ring programming language version 1.10 book - Part 70 of 212
The Ring programming language version 1.10 book - Part 70 of 212Mahmoud Samir Fayed
 
libGDX: User Input and Frame by Frame Animation
libGDX: User Input and Frame by Frame AnimationlibGDX: User Input and Frame by Frame Animation
libGDX: User Input and Frame by Frame AnimationJussi Pohjolainen
 
TDC2017 | São Paulo - Trilha Programação Funcional How we figured out we had ...
TDC2017 | São Paulo - Trilha Programação Funcional How we figured out we had ...TDC2017 | São Paulo - Trilha Programação Funcional How we figured out we had ...
TDC2017 | São Paulo - Trilha Programação Funcional How we figured out we had ...tdc-globalcode
 
The Ring programming language version 1.9 book - Part 80 of 210
The Ring programming language version 1.9 book - Part 80 of 210The Ring programming language version 1.9 book - Part 80 of 210
The Ring programming language version 1.9 book - Part 80 of 210Mahmoud Samir Fayed
 
The Ring programming language version 1.4 book - Part 14 of 30
The Ring programming language version 1.4 book - Part 14 of 30The Ring programming language version 1.4 book - Part 14 of 30
The Ring programming language version 1.4 book - Part 14 of 30Mahmoud Samir Fayed
 
The Ring programming language version 1.5.3 book - Part 48 of 184
The Ring programming language version 1.5.3 book - Part 48 of 184The Ring programming language version 1.5.3 book - Part 48 of 184
The Ring programming language version 1.5.3 book - Part 48 of 184Mahmoud Samir Fayed
 
HoloLens Programming Tutorial: AirTap & Spatial Mapping
HoloLens Programming Tutorial: AirTap & Spatial MappingHoloLens Programming Tutorial: AirTap & Spatial Mapping
HoloLens Programming Tutorial: AirTap & Spatial MappingTakashi Yoshinaga
 
The Ring programming language version 1.2 book - Part 36 of 84
The Ring programming language version 1.2 book - Part 36 of 84The Ring programming language version 1.2 book - Part 36 of 84
The Ring programming language version 1.2 book - Part 36 of 84Mahmoud Samir Fayed
 
Test Driven Cocos2d
Test Driven Cocos2dTest Driven Cocos2d
Test Driven Cocos2dEric Smith
 

Mais procurados (19)

Introduction to Game Programming: Using C# and Unity 3D - Chapter 7 (Preview)
Introduction to Game Programming: Using C# and Unity 3D - Chapter 7 (Preview)Introduction to Game Programming: Using C# and Unity 3D - Chapter 7 (Preview)
Introduction to Game Programming: Using C# and Unity 3D - Chapter 7 (Preview)
 
The Ring programming language version 1.7 book - Part 53 of 196
The Ring programming language version 1.7 book - Part 53 of 196The Ring programming language version 1.7 book - Part 53 of 196
The Ring programming language version 1.7 book - Part 53 of 196
 
Introduction to Game Programming: Using C# and Unity 3D - Chapter 2 (Preview)
Introduction to Game Programming: Using C# and Unity 3D - Chapter 2 (Preview)Introduction to Game Programming: Using C# and Unity 3D - Chapter 2 (Preview)
Introduction to Game Programming: Using C# and Unity 3D - Chapter 2 (Preview)
 
Introduction to Game Programming: Using C# and Unity 3D - Chapter 3 (Preview)
Introduction to Game Programming: Using C# and Unity 3D - Chapter 3 (Preview)Introduction to Game Programming: Using C# and Unity 3D - Chapter 3 (Preview)
Introduction to Game Programming: Using C# and Unity 3D - Chapter 3 (Preview)
 
The Ring programming language version 1.2 book - Part 50 of 84
The Ring programming language version 1.2 book - Part 50 of 84The Ring programming language version 1.2 book - Part 50 of 84
The Ring programming language version 1.2 book - Part 50 of 84
 
Creating Games for Asha - platform
Creating Games for Asha - platformCreating Games for Asha - platform
Creating Games for Asha - platform
 
The Ring programming language version 1.5.4 book - Part 48 of 185
The Ring programming language version 1.5.4 book - Part 48 of 185The Ring programming language version 1.5.4 book - Part 48 of 185
The Ring programming language version 1.5.4 book - Part 48 of 185
 
The Ring programming language version 1.5.2 book - Part 49 of 181
The Ring programming language version 1.5.2 book - Part 49 of 181The Ring programming language version 1.5.2 book - Part 49 of 181
The Ring programming language version 1.5.2 book - Part 49 of 181
 
The Ring programming language version 1.5.1 book - Part 48 of 180
The Ring programming language version 1.5.1 book - Part 48 of 180The Ring programming language version 1.5.1 book - Part 48 of 180
The Ring programming language version 1.5.1 book - Part 48 of 180
 
libGDX: Simple Frame Animation
libGDX: Simple Frame AnimationlibGDX: Simple Frame Animation
libGDX: Simple Frame Animation
 
The Ring programming language version 1.10 book - Part 70 of 212
The Ring programming language version 1.10 book - Part 70 of 212The Ring programming language version 1.10 book - Part 70 of 212
The Ring programming language version 1.10 book - Part 70 of 212
 
libGDX: User Input and Frame by Frame Animation
libGDX: User Input and Frame by Frame AnimationlibGDX: User Input and Frame by Frame Animation
libGDX: User Input and Frame by Frame Animation
 
TDC2017 | São Paulo - Trilha Programação Funcional How we figured out we had ...
TDC2017 | São Paulo - Trilha Programação Funcional How we figured out we had ...TDC2017 | São Paulo - Trilha Programação Funcional How we figured out we had ...
TDC2017 | São Paulo - Trilha Programação Funcional How we figured out we had ...
 
The Ring programming language version 1.9 book - Part 80 of 210
The Ring programming language version 1.9 book - Part 80 of 210The Ring programming language version 1.9 book - Part 80 of 210
The Ring programming language version 1.9 book - Part 80 of 210
 
The Ring programming language version 1.4 book - Part 14 of 30
The Ring programming language version 1.4 book - Part 14 of 30The Ring programming language version 1.4 book - Part 14 of 30
The Ring programming language version 1.4 book - Part 14 of 30
 
The Ring programming language version 1.5.3 book - Part 48 of 184
The Ring programming language version 1.5.3 book - Part 48 of 184The Ring programming language version 1.5.3 book - Part 48 of 184
The Ring programming language version 1.5.3 book - Part 48 of 184
 
HoloLens Programming Tutorial: AirTap & Spatial Mapping
HoloLens Programming Tutorial: AirTap & Spatial MappingHoloLens Programming Tutorial: AirTap & Spatial Mapping
HoloLens Programming Tutorial: AirTap & Spatial Mapping
 
The Ring programming language version 1.2 book - Part 36 of 84
The Ring programming language version 1.2 book - Part 36 of 84The Ring programming language version 1.2 book - Part 36 of 84
The Ring programming language version 1.2 book - Part 36 of 84
 
Test Driven Cocos2d
Test Driven Cocos2dTest Driven Cocos2d
Test Driven Cocos2d
 

Destaque

Personal Learning Network
Personal Learning NetworkPersonal Learning Network
Personal Learning Networksamspector93
 
Buyuk Taslar
Buyuk  TaslarBuyuk  Taslar
Buyuk Taslarmrelmas
 
N Ivanov Market Failure
N Ivanov Market FailureN Ivanov Market Failure
N Ivanov Market FailureNataliaivanov
 
Zhang Eye Movement As An Interaction Mechanism For Relevance Feedback In A Co...
Zhang Eye Movement As An Interaction Mechanism For Relevance Feedback In A Co...Zhang Eye Movement As An Interaction Mechanism For Relevance Feedback In A Co...
Zhang Eye Movement As An Interaction Mechanism For Relevance Feedback In A Co...Kalle
 
Liang Content Based Image Retrieval Using A Combination Of Visual Features An...
Liang Content Based Image Retrieval Using A Combination Of Visual Features An...Liang Content Based Image Retrieval Using A Combination Of Visual Features An...
Liang Content Based Image Retrieval Using A Combination Of Visual Features An...Kalle
 
TDR u Srbiji - pregled poslovanja - press konferencija 13.03.2012
TDR u Srbiji - pregled poslovanja - press konferencija 13.03.2012TDR u Srbiji - pregled poslovanja - press konferencija 13.03.2012
TDR u Srbiji - pregled poslovanja - press konferencija 13.03.2012TDR d.o.o Rovinj
 
Meridian Terms and Conditions
Meridian Terms and ConditionsMeridian Terms and Conditions
Meridian Terms and ConditionsAeroSvit Airlines
 
Hassinger Chiropractic Clinic
Hassinger Chiropractic ClinicHassinger Chiropractic Clinic
Hassinger Chiropractic ClinicKeith Hassinger
 
Hussain Learning Relevant Eye Movement Feature Spaces Across Users
Hussain Learning Relevant Eye Movement Feature Spaces Across UsersHussain Learning Relevant Eye Movement Feature Spaces Across Users
Hussain Learning Relevant Eye Movement Feature Spaces Across UsersKalle
 
Proyecto Cactus 1°3vesp
Proyecto Cactus 1°3vespProyecto Cactus 1°3vesp
Proyecto Cactus 1°3vespguest32ff1ff
 
ממלחמה זעירה למלחמה סדירה
ממלחמה זעירה למלחמה סדירהממלחמה זעירה למלחמה סדירה
ממלחמה זעירה למלחמה סדירהhaimkarel
 
Proposal 0323 (Stan)
Proposal 0323 (Stan)Proposal 0323 (Stan)
Proposal 0323 (Stan)guest7fe64c
 
Hyves Cbw Mitex Harry Van Wouter
Hyves Cbw Mitex Harry Van WouterHyves Cbw Mitex Harry Van Wouter
Hyves Cbw Mitex Harry Van Wouterguest2f17d3
 
C:\Documents And Settings\Freddy Kruger\Pulpit\Niedziela Palmowa
C:\Documents And Settings\Freddy Kruger\Pulpit\Niedziela PalmowaC:\Documents And Settings\Freddy Kruger\Pulpit\Niedziela Palmowa
C:\Documents And Settings\Freddy Kruger\Pulpit\Niedziela PalmowaEwa Kurzawska
 
Daugherty Measuring Vergence Over Stereoscopic Video With A Remote Eye Tracker
Daugherty Measuring Vergence Over Stereoscopic Video With A Remote Eye TrackerDaugherty Measuring Vergence Over Stereoscopic Video With A Remote Eye Tracker
Daugherty Measuring Vergence Over Stereoscopic Video With A Remote Eye TrackerKalle
 

Destaque (20)

NII
NIINII
NII
 
Personal Learning Network
Personal Learning NetworkPersonal Learning Network
Personal Learning Network
 
Buyuk Taslar
Buyuk  TaslarBuyuk  Taslar
Buyuk Taslar
 
Html
HtmlHtml
Html
 
N Ivanov Market Failure
N Ivanov Market FailureN Ivanov Market Failure
N Ivanov Market Failure
 
Zhang Eye Movement As An Interaction Mechanism For Relevance Feedback In A Co...
Zhang Eye Movement As An Interaction Mechanism For Relevance Feedback In A Co...Zhang Eye Movement As An Interaction Mechanism For Relevance Feedback In A Co...
Zhang Eye Movement As An Interaction Mechanism For Relevance Feedback In A Co...
 
Liang Content Based Image Retrieval Using A Combination Of Visual Features An...
Liang Content Based Image Retrieval Using A Combination Of Visual Features An...Liang Content Based Image Retrieval Using A Combination Of Visual Features An...
Liang Content Based Image Retrieval Using A Combination Of Visual Features An...
 
TDR u Srbiji - pregled poslovanja - press konferencija 13.03.2012
TDR u Srbiji - pregled poslovanja - press konferencija 13.03.2012TDR u Srbiji - pregled poslovanja - press konferencija 13.03.2012
TDR u Srbiji - pregled poslovanja - press konferencija 13.03.2012
 
Meridian Terms and Conditions
Meridian Terms and ConditionsMeridian Terms and Conditions
Meridian Terms and Conditions
 
Madness
MadnessMadness
Madness
 
Hassinger Chiropractic Clinic
Hassinger Chiropractic ClinicHassinger Chiropractic Clinic
Hassinger Chiropractic Clinic
 
Keele Interview
Keele InterviewKeele Interview
Keele Interview
 
Hussain Learning Relevant Eye Movement Feature Spaces Across Users
Hussain Learning Relevant Eye Movement Feature Spaces Across UsersHussain Learning Relevant Eye Movement Feature Spaces Across Users
Hussain Learning Relevant Eye Movement Feature Spaces Across Users
 
NCNG Social Media Webinar 3.24.10
NCNG Social Media Webinar 3.24.10NCNG Social Media Webinar 3.24.10
NCNG Social Media Webinar 3.24.10
 
Proyecto Cactus 1°3vesp
Proyecto Cactus 1°3vespProyecto Cactus 1°3vesp
Proyecto Cactus 1°3vesp
 
ממלחמה זעירה למלחמה סדירה
ממלחמה זעירה למלחמה סדירהממלחמה זעירה למלחמה סדירה
ממלחמה זעירה למלחמה סדירה
 
Proposal 0323 (Stan)
Proposal 0323 (Stan)Proposal 0323 (Stan)
Proposal 0323 (Stan)
 
Hyves Cbw Mitex Harry Van Wouter
Hyves Cbw Mitex Harry Van WouterHyves Cbw Mitex Harry Van Wouter
Hyves Cbw Mitex Harry Van Wouter
 
C:\Documents And Settings\Freddy Kruger\Pulpit\Niedziela Palmowa
C:\Documents And Settings\Freddy Kruger\Pulpit\Niedziela PalmowaC:\Documents And Settings\Freddy Kruger\Pulpit\Niedziela Palmowa
C:\Documents And Settings\Freddy Kruger\Pulpit\Niedziela Palmowa
 
Daugherty Measuring Vergence Over Stereoscopic Video With A Remote Eye Tracker
Daugherty Measuring Vergence Over Stereoscopic Video With A Remote Eye TrackerDaugherty Measuring Vergence Over Stereoscopic Video With A Remote Eye Tracker
Daugherty Measuring Vergence Over Stereoscopic Video With A Remote Eye Tracker
 

Semelhante a XNA coding series

Useful Tools for Making Video Games - XNA (2008)
Useful Tools for Making Video Games - XNA (2008)Useful Tools for Making Video Games - XNA (2008)
Useful Tools for Making Video Games - XNA (2008)Korhan Bircan
 
Flash auto play image gallery
Flash auto play image galleryFlash auto play image gallery
Flash auto play image galleryBoy Jeorge
 
School For Games 2015 - Unity Engine Basics
School For Games 2015 - Unity Engine BasicsSchool For Games 2015 - Unity Engine Basics
School For Games 2015 - Unity Engine BasicsNick Pruehs
 
Making Games in JavaScript
Making Games in JavaScriptMaking Games in JavaScript
Making Games in JavaScriptSam Cartwright
 
Keynote + Next Gen UIs.pptx
Keynote + Next Gen UIs.pptxKeynote + Next Gen UIs.pptx
Keynote + Next Gen UIs.pptxEqraKhattak
 
How to make a video game
How to make a video gameHow to make a video game
How to make a video gamedandylion13
 
Introduction to Unity3D and Building your First Game
Introduction to Unity3D and Building your First GameIntroduction to Unity3D and Building your First Game
Introduction to Unity3D and Building your First GameSarah Sexton
 
Building your first game in Unity 3d by Sarah Sexton
Building your first game in Unity 3d  by Sarah SextonBuilding your first game in Unity 3d  by Sarah Sexton
Building your first game in Unity 3d by Sarah SextonBeMyApp
 
98 374 Lesson 05-slides
98 374 Lesson 05-slides98 374 Lesson 05-slides
98 374 Lesson 05-slidesTracie King
 
import java.awt.Color;import java.awt.Insets;import java.awt.Con.pdf
import java.awt.Color;import java.awt.Insets;import java.awt.Con.pdfimport java.awt.Color;import java.awt.Insets;import java.awt.Con.pdf
import java.awt.Color;import java.awt.Insets;import java.awt.Con.pdfvenkt12345
 
Creating a Facebook Clone - Part XV - Transcript.pdf
Creating a Facebook Clone - Part XV - Transcript.pdfCreating a Facebook Clone - Part XV - Transcript.pdf
Creating a Facebook Clone - Part XV - Transcript.pdfShaiAlmog1
 
The following GUI is displayed once the application startsThe sug.pdf
The following GUI is displayed once the application startsThe sug.pdfThe following GUI is displayed once the application startsThe sug.pdf
The following GUI is displayed once the application startsThe sug.pdfarihantsherwani
 
Writing videogames with titanium appcelerator
Writing videogames with titanium appceleratorWriting videogames with titanium appcelerator
Writing videogames with titanium appceleratorAlessio Ricco
 
Chapter 02 sprite and texture
Chapter 02 sprite and textureChapter 02 sprite and texture
Chapter 02 sprite and textureboybuon205
 
Modify the following source code so that when the mouse is clicked w.pdf
Modify the following source code so that when the mouse is clicked w.pdfModify the following source code so that when the mouse is clicked w.pdf
Modify the following source code so that when the mouse is clicked w.pdfarorastores
 
How to Create Custom Shaders in Flutter?
How to Create Custom Shaders in Flutter?How to Create Custom Shaders in Flutter?
How to Create Custom Shaders in Flutter?Flutter Agency
 
Creating a Facebook Clone - Part XLI - Transcript.pdf
Creating a Facebook Clone - Part XLI - Transcript.pdfCreating a Facebook Clone - Part XLI - Transcript.pdf
Creating a Facebook Clone - Part XLI - Transcript.pdfShaiAlmog1
 

Semelhante a XNA coding series (20)

Useful Tools for Making Video Games - XNA (2008)
Useful Tools for Making Video Games - XNA (2008)Useful Tools for Making Video Games - XNA (2008)
Useful Tools for Making Video Games - XNA (2008)
 
Flash auto play image gallery
Flash auto play image galleryFlash auto play image gallery
Flash auto play image gallery
 
School For Games 2015 - Unity Engine Basics
School For Games 2015 - Unity Engine BasicsSchool For Games 2015 - Unity Engine Basics
School For Games 2015 - Unity Engine Basics
 
Making Games in JavaScript
Making Games in JavaScriptMaking Games in JavaScript
Making Games in JavaScript
 
Keynote + Next Gen UIs.pptx
Keynote + Next Gen UIs.pptxKeynote + Next Gen UIs.pptx
Keynote + Next Gen UIs.pptx
 
How to make a video game
How to make a video gameHow to make a video game
How to make a video game
 
Introduction to Unity3D and Building your First Game
Introduction to Unity3D and Building your First GameIntroduction to Unity3D and Building your First Game
Introduction to Unity3D and Building your First Game
 
Building your first game in Unity 3d by Sarah Sexton
Building your first game in Unity 3d  by Sarah SextonBuilding your first game in Unity 3d  by Sarah Sexton
Building your first game in Unity 3d by Sarah Sexton
 
98 374 Lesson 05-slides
98 374 Lesson 05-slides98 374 Lesson 05-slides
98 374 Lesson 05-slides
 
import java.awt.Color;import java.awt.Insets;import java.awt.Con.pdf
import java.awt.Color;import java.awt.Insets;import java.awt.Con.pdfimport java.awt.Color;import java.awt.Insets;import java.awt.Con.pdf
import java.awt.Color;import java.awt.Insets;import java.awt.Con.pdf
 
Creating a Facebook Clone - Part XV - Transcript.pdf
Creating a Facebook Clone - Part XV - Transcript.pdfCreating a Facebook Clone - Part XV - Transcript.pdf
Creating a Facebook Clone - Part XV - Transcript.pdf
 
Unity3 d devfest-2014
Unity3 d devfest-2014Unity3 d devfest-2014
Unity3 d devfest-2014
 
Graphical User Components Part 2
Graphical User Components Part 2Graphical User Components Part 2
Graphical User Components Part 2
 
The following GUI is displayed once the application startsThe sug.pdf
The following GUI is displayed once the application startsThe sug.pdfThe following GUI is displayed once the application startsThe sug.pdf
The following GUI is displayed once the application startsThe sug.pdf
 
Writing videogames with titanium appcelerator
Writing videogames with titanium appceleratorWriting videogames with titanium appcelerator
Writing videogames with titanium appcelerator
 
Game age ppt
Game age pptGame age ppt
Game age ppt
 
Chapter 02 sprite and texture
Chapter 02 sprite and textureChapter 02 sprite and texture
Chapter 02 sprite and texture
 
Modify the following source code so that when the mouse is clicked w.pdf
Modify the following source code so that when the mouse is clicked w.pdfModify the following source code so that when the mouse is clicked w.pdf
Modify the following source code so that when the mouse is clicked w.pdf
 
How to Create Custom Shaders in Flutter?
How to Create Custom Shaders in Flutter?How to Create Custom Shaders in Flutter?
How to Create Custom Shaders in Flutter?
 
Creating a Facebook Clone - Part XLI - Transcript.pdf
Creating a Facebook Clone - Part XLI - Transcript.pdfCreating a Facebook Clone - Part XLI - Transcript.pdf
Creating a Facebook Clone - Part XLI - Transcript.pdf
 

Último

Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...Pooja Nehwal
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 

Último (20)

Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 

XNA coding series

  • 1. XNA Game Development. Exercise 1: Hello World in XNA. To write a hello world game, first you need to load font to the content folder. Right click on the content folder and Add->new item
  • 2. And select Spite font and rename that as Arial.spritefont Arial is the name of the font we are going to use. You can use any font that install in the Windows font folder. Go to Arial.spritefont file if you like you can change the size and style by modifing Size and Style tags. <Size>14</Size> <Style>Regular</Style>
  • 3. Now in the Game 1.cs you need to write code to draw a string in the screen. First create a object form SpriteFont to handle our font. SpriteFont myfont; Then in the LoadContent() method you can load the font. myfont = Content.Load<SpriteFont>("Arial"); then in the Draw() metord you can draw the font in the sceran. spriteBatch.Begin(); / /start the sprite batch process to draw font spriteBatch.DrawString(myfont, "Hello world", new Vector2(10.0f, 10.0f), Color.Black); //draw the font in the screan spriteBatch.End(); //end the sprite batch process Now you can run the project by pressing F5 or by clicking the run button.
  • 4. Exercise 2: Draw 2d image. First you need to load an image to the content folder. Right click on the content folder and Add->existing items And select an image form the hard drive. It could be .bmp,.png,.jpg ,.dds,.tga
  • 5. Then create a object form Texture2D to handle the image. Texture2D mytx; Then in the LoadContent() method you can load the image. mytx = Content.Load<Texture2D>("img"); then in the Draw() metord you can draw the image in the sceran. spriteBatch.Begin(); / /start the sprite batch process to draw font spriteBatch.Draw(mytx,new Rectangle(100,100,600,400), Color.White); spriteBatch.End(); //end the sprite batch process Now you can run the project by pressing F5 or by clicking the run button.
  • 6. Modify the code for 2d animation. Also you can create 2d animation to this example using these lines of codes. First initialize a float variable to the animation. float val = 0.0f; Then in Update() metord increment the value to change the position. val = val + 0.3f; Finally in the Draw() metord increment the x y positions of the image. spriteBatch.Draw(mytx,new Rectangle(100+(int)val,100+(int)val,600,400), Color.White); Then after press F5 you are ready to play the animation.
  • 7. Exercise 3: Using Keyboard and Mouse in a PC game. First you need to have an image to represent mouse cursor and another image to show the keyboard usage. You can add those files in to the content folder. In the Game1.cs file initialize these variables. KeyboardState mykeyboardstate; //create a keyboardstate object to get the state of the keyboard
  • 8. MouseState mymousesatate; //get the state of the mouse SpriteFont myfont; //add a sprite font to draw String in the screan Texture2D mytexture, mytexture2;//creating texture object float Position = 0.0f;//initializing up down position float Position2 = 0.0f;//initializing left right position float mousex = 0.0f;//initilizing mousex position float mousey = 0.0f;//initializing mousey position Now in the LoadContent() metord write code for load the assest. //change the window title Window.Title = "Using the keyboard+mouse-demo"; myfont = Content.Load<SpriteFont>("Arial"); mytexture = Content.Load<Texture2D>("pic");
  • 9. mytexture2 = Content.Load<Texture2D>("cursor"); Then in the Update() methord you can chack for keyboard and mouse inputs. mykeyboardstate = Keyboard.GetState();//capturing the keybard state mymousesatate = Mouse.GetState();//capturing mouse state mousex = (float)mymousesatate.X;//getting the x position of the mouse mousey = (float)mymousesatate.Y;//getting the y position of the mouse // Move 400 pixels each second float moveFactorPerSecond = 80 * (float)gameTime.ElapsedRealTime.TotalMilliseconds / 1000.0f; //chage the position according to the key pressed up,down,left,right if (mykeyboardstate.IsKeyDown(Keys.Up)) Position -= moveFactorPerSecond; if (mykeyboardstate.IsKeyDown(Keys.Down)) Position += moveFactorPerSecond;
  • 10. if (mykeyboardstate.IsKeyDown(Keys.Left)) Position2 -= moveFactorPerSecond; if (mykeyboardstate.IsKeyDown(Keys.Right)) Position2 += moveFactorPerSecond; In the Draw () method you can see the Changes of the inputs by drawing the images. myspitebatch.Begin();//start process Vector2 position = new Vector2(200.0f + (int)Position2, 200.0f + ((int)Position)); //setting position with variables (Position1) and (Position2) //those variables change the position of the image according to the key pressing myspitebatch.Draw(mytexture, position, Color.White);//drawing the image myspitebatch.DrawString(myfont, "Use Arrowkeys to move the image & use mouse to point", new Vector2(10.0f, 10.0f), Color.Gold);//drawing text on the screan
  • 11. myspitebatch.Draw(mytexture2, new Vector2(mousex, mousey), Color.White); //drawing the cursor image, acording to the mouse position myspitebatch.End();//end process Now you can run the project by pressing F5 or by clicking the run button Exercise 4: Crating a Menu system in a PC game.
  • 12. For the Game state you need to add some images to show the user inputs You can add those files in to the content folder. Then you are able to write a code for create a menu system in a XNA game. In the Game1.cs file initialize these variables. KeyboardState mykeyboardstate; //create a keyboardstate object to get the state of the keyboard
  • 13. MouseState mymousesatate;//get the state of the mouse SpriteFont myfont;//add spite batch and spite font as explained in tutorial 01-hello world Texture2D mytexture, mytexture2;//creating texture object float Position = 0.0f;//initializing up down position float Position2 = 0.0f;//initializing left right position float mousex = 0.0f;//initilizing mousex position float mousey = 0.0f;//initializing mousey position Using enum we can define the different game states.Then we set the initial stage as start state. enum Mygamemode { start, game, exit } //define game modes start,game,exit
  • 14. Mygamemode mygame = Mygamemode.start;//assign start mode as initial mode Now in the LoadContent() metord write code for load the assest Window.Title = "Using menu system"; myspitebatch = new SpriteBatch(graphics.GraphicsDevice);//setting spite batch for graphic device myfont = Content.Load<SpriteFont>("Arial"); //loading font mytexture = Content.Load<Texture2D>("pic");//loding image mytexture2 = Content.Load<Texture2D>("cursor");//loding cursor image Then in the Update() methord you can chack for keyboard and mouse inputs. mykeyboardstate = Keyboard.GetState();//capturing the state mymousesatate = Mouse.GetState();//capturing mouse state
  • 15. mousex = (float)mymousesatate.X;//getting the x position of the mouse mousey = (float)mymousesatate.Y;//getting the y position of the mouse // Move 400 pixels each second float moveFactorPerSecond = 80 * (float)gameTime.ElapsedRealTime.TotalMilliseconds / 1000.0f; //chage the position according to the key pressed up,down,left,right if (mykeyboardstate.IsKeyDown(Keys.Up)) Position -= moveFactorPerSecond; if (mykeyboardstate.IsKeyDown(Keys.Down)) Position += moveFactorPerSecond; if (mykeyboardstate.IsKeyDown(Keys.Left)) Position2 -= moveFactorPerSecond;
  • 16. if (mykeyboardstate.IsKeyDown(Keys.Right)) Position2 += moveFactorPerSecond; In the Draw () method you can see the Changes by drawing the images. if (mygame == Mygamemode.start) //chack whether you are in the start mode { myspitebatch.Begin(); myspitebatch.DrawString(myfont, "Welcome to the demo ", new Vector2(30.0f, 30.0f), Color.YellowGreen); myspitebatch.DrawString(myfont, "Press Enter to continue..... ", new Vector2(30.0f, 80.0f), Color.YellowGreen); myspitebatch.End();
  • 17. if (mykeyboardstate.IsKeyDown(Keys.Enter))//chack whather user press enter key while he/she in the start mode mygame = Mygamemode.game; //if(true) assign game mode } if (mygame == Mygamemode.game)//chack whether you are in the game mode { myspitebatch.Begin();//start process Vector2 position = new Vector2(200.0f + (int)Position2, 200.0f + ((int)Position)); //setting position with variables (Position1) and (Position2) //those variables change the position of the image according to the key pressing
  • 18. myspitebatch.Draw(mytexture, position, Color.White);//drawing the image myspitebatch.DrawString(myfont, "Use Arrowkeys to move the image & use mouse to point", new Vector2(10.0f, 10.0f), Color.Gold);//drawing text on the screan myspitebatch.DrawString(myfont, "Use Esc to exit demo", new Vector2(10.0f, 30.0f), Color.Gold);//drawing text on the screan myspitebatch.Draw(mytexture2, new Vector2(mousex, mousey), Color.White); //drawing the cursor image, acording to the mouse position myspitebatch.End();//end process if (mykeyboardstate.IsKeyDown(Keys.Escape))//chack whather user press escape key while he/she in the start mode
  • 19. mygame = Mygamemode.exit; //if(true) assign exit mode } if (mygame == Mygamemode.exit) //chack whether you are in the exit mode { myspitebatch.Begin(); myspitebatch.DrawString(myfont, "Goodbuy from the demo ", new Vector2(30.0f, 30.0f), Color.YellowGreen); myspitebatch.DrawString(myfont, "Press Q to exit..... ", new Vector2(30.0f, 80.0f), Color.YellowGreen); myspitebatch.End(); if (mykeyboardstate.IsKeyDown(Keys.Q))//chack whather user press enter key while he/she in the start mode
  • 20. this.Exit(); //if(true) assign game mode } Now you can run the project by pressing F5 or by clicking the run button. Exercise 5:
  • 21. First you need to add Music files (.mp3, .wav) to the content folder in a new XNA project. In the Game1.cs file initialize these variables. Song mysong;//initialize the song SpriteFont myfont;//initialize font String val; Texture2D mytx;//initialize texture
  • 22. Now in the LoadContent() metord write code for load the assest Window.AllowUserResizing = true; //first add new mp3 to a content folder mysong = Content.Load<Song>("sum41");//load the mp3 myfont = Content.Load<SpriteFont>("Arial");//load the font mytx = Content.Load<Texture2D>("B");//load the image MediaPlayer.Play(mysong);//play the song Then in the Update() methord you can write this code segment. //simple text effect if (gameTime.TotalGameTime.Seconds % 2 == 0) { val = "Playing -sum 41" }
  • 23. else { val = ""; } In the Draw () method you can see the Changes by drawing the images. spriteBatch.Begin(); //begin spite batch prosess spriteBatch.Draw(mytx, new Rectangle(0, 0, Window.ClientBounds.Width, Window.ClientBounds.Height), Color.White); //draw image spriteBatch.DrawString(myfont, val, new Vector2(30.0f, 30.0f), Color.Orange); //draw the font
  • 24. spriteBatch.End();//end spite batch process Now you can run the project by pressing F5 or by clicking the run button.