Wednesday, February 18, 2009

First Full Circle XNA Game

After going through the tutorial last week I decided my best bet was to create a full circle game. That's a game with a main menu, the game, and a game over screen that moves back to the main menu. This is a shot of that main menu (very simple).


There also needs to be some sort of scoring or timing sytem. This will typically end up being the template for the rest of the games from this point forward. I used the art work from the tutorial and created a grid. Then added on model that could move up, down, left and right (only 2d) in the grid. One of the models is spinning and the idea is to move the red model on top of that model with as few moves as possible (looping around the other side is a key mechanic). In the screen shot, pressing the right key will move on top of the spinning ship as clicking the left key will take three moves. Get it?

For a piece of sample code, this is the row column stuff and grid keyboard movement I have written a million times. Maybe I can come back here next time and just cut and paste.

private int rowFromPos(int p)
{
return p / tilerows;
}
private int colFromPos(int p)
{
return p % tilecols;
}
private int posFromRowCol(int row, int col)
{
return (row * tilecols) + col;
}

private void positionXYFromIndex(int idx, ref Vector3 vSet)
{
vSet.X = fLeftX + colFromPos(idx)*gapX;
vSet.Y = fTopY + rowFromPos(idx)*gapY;
vSet.Z = 0.0f;
}

if ((newState.IsKeyDown(Keys.Right)) && (!oldState.IsKeyDown(Keys.Right)))
{
int row = rowFromPos(curSel);
int col = colFromPos(curSel);

col++;
if (col >= tilecols)
{
col = 0;
}

curSel = posFromRowCol(row, col);
positionXYFromIndex(curSel, ref selPosition);
moves++;
score--;
}
else if (oldState.IsKeyDown(Keys.Right))
{
}

if ((newState.IsKeyDown(Keys.Left)) && (!oldState.IsKeyDown(Keys.Left)))
{
int row = rowFromPos(curSel);
int col = colFromPos(curSel);

col--;
if (col < 0)
{
col = tilecols - 1;
}

curSel = posFromRowCol(row, col);
positionXYFromIndex(curSel, ref selPosition);
moves++;
score--;
}
else if (oldState.IsKeyDown(Keys.Left))
{
}

if ((newState.IsKeyDown(Keys.Up)) && (!oldState.IsKeyDown(Keys.Up)))
{
int row = rowFromPos(curSel);
int col = colFromPos(curSel);

row++;
if (row >= tilerows)
{
row = 0;
}

curSel = posFromRowCol(row, col);
positionXYFromIndex(curSel, ref selPosition);
moves++;
score--;
}
else if (oldState.IsKeyDown(Keys.Up))
{
}

if ((newState.IsKeyDown(Keys.Down)) && (!oldState.IsKeyDown(Keys.Down)))
{
int row = rowFromPos(curSel);
int col = colFromPos(curSel);

row--;
if (row < 0)
{
row = tilerows - 1;
}

curSel = posFromRowCol(row, col);
positionXYFromIndex(curSel, ref selPosition);
moves++;
score--;
}
else if (oldState.IsKeyDown(Keys.Down))
{
}

No comments: