Funding for 'IT Lab' Project, Phase 1: Progress of sticker sales. Purchase a sticker to help us reach our target.Updated: 2010-02-04 05:34
7.2%
Getting Started with XBOX 360 game development using Microsoft XNA.

 

by Uditha Sampath Bandara


This month we`ll dicuss about the code and steps to write your first hello world programme.

First select XBOX 360 project from the file->new->project


And the intilize file code struture is same as the windows game.


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.



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>

Final   Arial.spritefont  file.

<?xml version="1.0" encoding="utf-8"?>

<!--

This file contains an xml description of a font, and will be read by the XNA

Framework Content Pipeline. Follow the comments to customize the appearance

of the font in your game, and to change the characters which are available to draw

with.

-->

<XnaContent xmlns:Graphics="Microsoft.Xna.Framework.Content.Pipeline.Graphics">

  <Asset Type="Graphics:FontDescription">

    <!--

    Modify this string to change the font that will be imported.

    -->

    <FontName>Arial</FontName>

    <!--

    Size is a float value, measured in points. Modify this value to change

    the size of the font.

    -->

    <Size>14</Size>

    <!--

    Spacing is a float value, measured in pixels. Modify this value to change

    the amount of spacing in between characters.

    -->

    <Spacing>0</Spacing>

    <!--

    UseKerning controls the layout of the font. If this value is true, kerning information

    will be used when placing characters.

    -->

    <UseKerning>true</UseKerning>

    <!--

    Style controls the style of the font. Valid entries are "Regular", "Bold", "Italic",

    and "Bold, Italic", and are case sensitive.

    -->

    <Style>Regular</Style>

    <!--

    If you uncomment this line, the default character will be substituted if you draw

    or measure text that contains characters which were not included in the font.

    -->

    <!-- <DefaultCharacter>*</DefaultCharacter> -->

    <!--

    CharacterRegions control what letters are available in the font. Every

    character from Start to End will be built and made available for drawing. The

    default range is from 32, (ASCII space), to 126, ('~'), covering the basic Latin

    character set. The characters are ordered according to the Unicode standard.

    See the documentation for more information.

    -->

    <CharacterRegions>

  <CharacterRegion>

    <Start> </Start>

    <End>~</End>

  </CharacterRegion>

    </CharacterRegions>

  </Asset>

</XnaContent>

 Now in the Game 1.cs you need to write code to draw a string in the screan.


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);

//drawString() method is getting parameters for font type,string valuve,x and y positions in the screen where you need to put the string,and the font color.

      //draw the font in the scrern

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

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



Final code of Game1.cs

using System;

using System.Collections.Generic;

using System.Linq;

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.Media;

using Microsoft.Xna.Framework.Net;

using Microsoft.Xna.Framework.Storage;

namespace Hello_world_Xbox360Game

{

    /// <summary>

    /// This is the main type for your game

    /// </summary>

    public class Game1 : Microsoft.Xna.Framework.Game

    {

    GraphicsDeviceManager graphics;

    SpriteBatch spriteBatch;

    SpriteFont spritefont; //intilize sprite font

    public Game1()

    {

    graphics = new GraphicsDeviceManager(this);

    Content.RootDirectory = "Content";

    }

    /// <summary>

    /// Allows the game to perform any initialization it needs to before starting to run.

    /// This is where it can query for any required services and load any non-graphic

    /// related content.  Calling base.Initialize will enumerate through any components

    /// and initialize them as well.

    /// </summary>

    protected override void Initialize()

    {

    // TODO: Add your initialization logic here

    base.Initialize();

    }

    /// <summary>

    /// LoadContent will be called once per game and is the place to load

    /// all of your content.

    /// </summary>

    protected override void LoadContent()

    {

    // Create a new SpriteBatch, which can be used to draw textures.

    spriteBatch = new SpriteBatch(GraphicsDevice);

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

    //load the sprite font

    }

    /// <summary>

    /// UnloadContent will be called once per game and is the place to unload

    /// all content.

    /// </summary>

    protected override void UnloadContent()

    {

    // TODO: Unload any non ContentManager content here

    }

    /// <summary>

    /// Allows the game to run logic such as updating the world,

    /// checking for collisions, gathering input, and playing audio.

    /// </summary>

    /// <param name="gameTime">Provides a snapshot of timing values.</param>

    protected override void Update(GameTime gameTime)

    {

    // Allows the game to exit

    if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)

    this.Exit();

  

    base.Update(gameTime);

    }

    /// <summary>

    /// This is called when the game should draw itself.

    /// </summary>

    /// <param name="gameTime">Provides a snapshot of timing values.</param>

    protected override void Draw(GameTime gameTime)

    {

    GraphicsDevice.Clear(Color.CornflowerBlue);

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

    spriteBatch.DrawString(spritefont, "Hello world", new Vector2(100.0f, 100.0f), Color.YellowGreen);

    //draw the font in the screan

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

    base.Draw(gameTime);

    }

    }

}

   

This is the end of the First hello world programe for XBOX 360.


Form next tutorial you learn about working with 2d graphics in XNA for XBOX 360

                                                                                   Previous article