Thursday, May 17, 2007

Cannon3- third times the charm

Cannon3 is the third time we programmed the same game but each time we did it was to move the class closer to working in XNA.

Cannon1 was an event driven visual C# game. The player was moved by the apps mousemove event. The projectile reset by the onclick event .




Cannon 2 was still a visual C# game but this time it was programed more like an xna game with separate update and draw methods and classes for game items and huditems.



Cannon 3 was our first step into xna. Most of the code, except the draw method, was simply copied from cannon 2 making it very quick and easy to code. Now when it was done it looked just like Cannon 2 but everyone wanted to add their own functionality to the game. I add sound effects, multiple targets and bullets, and a plane that flew across the screen dropping out targets based on time and the current limit of targets on screen.



I did this just by using the help files and when ever I had a problem I couldn't work out somebody usually was on the same sort of stuff and was willing to give a hand. The hardest part of my cannon3 game was trying to figure out how to compare all the items in ListA to all the items in ListB to use for collision detection. I had couldn't find anything in the help files and even Google came out empty for me. I had to draw out a picture of what I wanted the code to do to figure it out. And when it worked I was ecstatic . Check it out.



foreach (Bullets bullet in projectiles)

{

for (int i = targets.Count - 1; i > -1; i--)

{

if (Distance(bullet.Position, targets[i].Position+targets[i].Origin )<>
{

bullet.Alive = false;

targets.RemoveAt(i);

}

}
}



I don't know if this is the is the easiest way but it works.

No comments: