HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

RPG game using XNA

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
xnagameusingrpg

Problem

I know this code needs some organization like classes, enums, methods, arrays or something but I just want to know if I could have created a character movement script, thereby shortening the code without using enums, methods, array etc. (keep it the same). Is there any ways to shorten this?

```
namespace rpgProject
{

public class Game1 : Microsoft.Xna.Framework.Game

{

GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;

Texture2D Character;

Vector2 characterPos = new Vector2(400,240);
float speed1 = 2;

float speed2 = 2;

Point frameSize = new Point(32,63);

Point CurrentFrame = new Point(0, 0);

Point sheetSize = new Point(6, 4);

int timePassed;

int timeLimit=43;

bool right = true;

bool left = true;

bool up = true;

bool glitch = true;

bool down = true;

SpriteFont font1;

SpriteFont font2;

public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}

protected override void Initialize()
{

base.Initialize();
}

protected override void LoadContent()
{

spriteBatch = new SpriteBatch(GraphicsDevice);
Character = Content.Load("Sprites/test");
font1 = Content.Load("fonts/SpriteFont1");
font2 = Content.Load("fonts/SpriteFont1");

}

protected override void UnloadContent()
{

}

protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();

if (characterPos.X Window.ClientBounds.Width - Character.Width)//to make sure it does not colide with edge of the screen
{
if (characterPos.X = Window.ClientBounds.Height - Character.Height)
{

if (characterPos.Y = timeLimit)
{

timePassed -=timeLimit;
if (right==true)
{

if (charMovement

Solution

I've seen InputHandler classes very similar to this one created by Robb O'Driscoll

You could cut your Game1.cs down by a fair bit using it, make the following changes:

InputHandler inputHandler;
public Game1()
{
    graphics = new GraphicsDeviceManager(this);
    Content.RootDirectory = "Content";
    inputHandler = new InputHandler();
}


At the starting of your Update method add:

inputHandler.Update(gameTime);


You can then use:

if(inputHandler.KeyPressed(Keys.Right))
{
    //Move Character right
}


I am not 100% sure what you're doing with the boolean left,right,up,down so I didn't put much effort into changing your actual code.

The majority of your code is setting the four direction booleans back and forth though, perhaps simplifying what you are trying to do there will have a bigger impact.

The input handler linked should get you on the right path for pulling out the movement handling.

Code Snippets

InputHandler inputHandler;
public Game1()
{
    graphics = new GraphicsDeviceManager(this);
    Content.RootDirectory = "Content";
    inputHandler = new InputHandler();
}
inputHandler.Update(gameTime);
if(inputHandler.KeyPressed(Keys.Right))
{
    //Move Character right
}

Context

StackExchange Code Review Q#84055, answer score: 4

Revisions (0)

No revisions yet.