Getting Started with XBOX 360 game development using Microsoft XNA

Part V
This month we`ll discuss about the code and steps to use XBOX360 controller in a XBOX360 game.

XBOX360 Controller

Xbox360 game screen.
In the Game1.cs file initialize these variables.
SpriteFont myfont;//add a sprite font to draw String in the screan
Texture2D mytx;//creating texture object
Color mycolor;//define a color variable
float Position = 0.0f;//initializing left right position
float Position2 = 0.0f;//initializing up down position
Now in the LoadContent() method write code to load the assert.
mytx = Content.Load<Texture2D>(pic);
myfont = Content.Load<SpriteFont>(Arial);
Then in the Update() method you can check for XBOX360 Contraller Inputs
GamePadState state = GamePad.GetState(PlayerIndex.One);
//capturing the plyer one XBOX360 Controller state
float moveFactorPerSecond = 80 *
(float)gameTime.ElapsedRealTime.TotalMilliseconds / 1000.0f; // Move 400 pixels each second
// Allows the game to exit
if (state.Buttons.Back == ButtonState.Pressed)
this.Exit();
//A Buttons
if (state.Buttons.A == ButtonState.Pressed)
{
>mycolor = Color.Red;
//Set vibration
GamePad.SetVibration(0, 4f, 3f);
}
else
{
mycolor = Color.White;
}
//Direction Pad
if( state.DPad.Left == ButtonState.Pressed)
{
Position -= moveFactorPerSecond;
}
if( state.DPad.Right == ButtonState.Pressed)
{
Position += moveFactorPerSecond;
}
if(state.DPad.Up == ButtonState.Pressed)
{
Position2 -= moveFactorPerSecond;
}
if (state.DPad.Down == ButtonState.Pressed)
{
Position2 += moveFactorPerSecond;
}
In the Draw () method you can see the Changes of the inputs by drawing the images.
spriteBatch.Begin(); //start process
spriteBatch.Draw(mytx, new Vector2(100f+Position, 100f+Position2), mycolor);
//setting position with variables (Position1) and (Position2)
//those variables change the position of the image according to the key pressing
spriteBatch.DrawString(myfont, Use D Pad to move the image and Press A for change the color,new Vector2(10f,10f),Color.Yellow);
//drawing text on the screan
spriteBatch.End(); //end process
Now you can run the project by pressing F5 or by clicking the run button.

Coding for Other Buttons.
// ABXY buttons
public static bool BButton { get { return (state.Buttons.B == ButtonState.Pressed); } }
public static bool XButton { get { return (state.Buttons.X == ButtonState.Pressed); } }
public static bool YButton { get { return (state.Buttons.Y == ButtonState.Pressed); } }
//Start and Back buttons
public static bool BackButton { get { return (state.Buttons.Back == ButtonState.Pressed); } }
public static bool StartButton { get { return (state.Buttons.Start == ButtonState.Pressed); } }
//Thumb sticks
public static bool LeftThumbButton { get { return (state.Buttons.LeftStick == ButtonState.Pressed); } }
public static float LeftThumbX { get { return state.ThumbSticks.Left.X; } }
public static float LeftThumbY { get { return state.ThumbSticks.Left.Y; } }
public static bool RightThumbButton { get { return (state.Buttons.RightStick == ButtonState.Pressed); } }
public static float RightThumbX { get { return state.ThumbSticks.Right.X; } }
public static float RightThumbY { get { return state.ThumbSticks.Right.Y; } }
//Shoulder Buttons
public static bool LeftShoulderButton { get { return (state.Buttons.LeftShoulder == ButtonState.Pressed); } }
public static bool RightShoulderButton { get { return (state.Buttons.RightShoulder == ButtonState.Pressed); } }
//Triggers
public static float LeftTrigger { get { return state.Triggers.Left; } }
public static float RightTrigger { get { return state.Triggers.Right; } }
Final code of Game1.cs
/*
content created by -uditha sampath bandara
udithamail@yahoo.com
*/
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 XBOX360_Controller
{
///<summary>
/// This is the main type for your game
///</summary>
>public class Game1 :Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
SpriteFont myfont;//add a sprite font to draw String in the screan
Texture2D mytx;//creating texture object
Color mycolor;//define a color variable
float Position = 0.0f;//initializing left right position
float Position2 = 0.0f;//initializing up down position
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()
{
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);
mytx = Content.Load<Texture2D>(pic);
myfont = Content.Load<SpriteFont>(Arial);
}
///<summary>
/// UnloadContent will be called once per game and is the place to unload
/// all content.
///</summary>
protected override void UnloadContent()
{
}
///<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)
{
GamePadState state = GamePad.GetState(PlayerIndex.One);
//capturing the plyer one XBOX360 Controller state
float moveFactorPerSecond = 80 *
(float)gameTime.ElapsedRealTime.TotalMilliseconds / 1000.0f; // Move 400 pixels each second
// Allows the game to exit
if (state.Buttons.Back == ButtonState.Pressed)
this.Exit();
//A Buttons
if (state.Buttons.A == ButtonState.Pressed)
{
mycolor = Color.Red;
//Set vibration
GamePad.SetVibration(0, 4f, 3f);
}
else
{
mycolor = Color.White;
}
//Direction Pad
if( state.DPad.Left == ButtonState.Pressed)
{
Position -= moveFactorPerSecond;
}
if( state.DPad.Right == ButtonState.Pressed)
{
Position += moveFactorPerSecond;
}
if(state.DPad.Up == ButtonState.Pressed)
{
Position2 -= moveFactorPerSecond;
}
if (state.DPad.Down == ButtonState.Pressed)
{
Position2 += moveFactorPerSecond;
}
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.Black);
spriteBatch.Begin(); //start process
spriteBatch.Draw(mytx, new Vector2(100f+Position, 100f+Position2), mycolor);
//setting position with variables (Position1) and (Position2)
//those variables change the position of the image according to the key pressing
spriteBatch.DrawString(myfont, Use D Pad to move the image and Press A for change the color,new Vector2(10f,10f),Color.Yellow);
//drawing text on the screan
spriteBatch.End(); //end process
base.Draw(gameTime);
}
}
}
This is the end of using Xbox360 game Controller in a XBOX360 game tutorial.
Also source project is attached with this article.
Form next tutorial you learn about working with game Menus in a XBOX360 game.



Post new comment