Pages

Categories

XNA Game Dev blog #4

2011
05.16
XNA Game Dev blog #4
Category: Uncategorized / Tags: no tag / Add Comment
To follow on from yesterday’s post, I got a bit done today. I got round to making the lasers and collisions collide with each other after playing around a bit. I also changed the playership slightly, to give it less of an advantage. I removed the side large cannons, of the ship and plan on making them an upgrade option. So now the ship looks like this to start with: Had a slight epiphany while I was doing the collision detection. Originally I was doing:
Selec All Code:
1
2
3
4
5
6
7
if ( laser.pos.x >= alien.pos.x & laser.pos.x + width <= alien.pos.x + width )
{
if ( laser.pos.Y >= alien.pos.Y & laser.pos.Y + height <= alien.pos.x + height )
{
//Collision detected
}
}
This code looks very messy and is so easy to make an error in the logic, and even harder to find it. So instead what I did was create 2 Rectangle objects that represent the bounds of the alien and projectile, then checked if they intersect, like so:
Selec All Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
for ( int i = 0; i < projectiles.Count(); i++ )
{
for ( int j = 0; j < aliens.Count(); j++ )
{

if ( projectiles[ i ].Active && aliens[ j ].Active )
{
Rectangle rectangle1 = new Rectangle( ( int ) projectiles[ i ].Position.X, ( int ) projectiles[ i ].Position.Y, projectiles[ i ].Width, projectiles[ i ].Height );
Rectangle rectangle2 = new Rectangle( ( int ) aliens[ j ].Position.X, ( int ) aliens[ j ].Position.Y, aliens[ j ].Width, aliens[ j ].Height );

if ( rectangle1.Intersects( rectangle2 ) )
{

//aliens[ i ].Active = false;
aliens[ j ].health–;

projectiles[ i ].Active = false;
}
}
}
}
If I can find a way to post code to a blog that doesn’t mess up the white space formatting and maybe has syntax highlighting for C# I’d paste the actual code. But until then here’s the current solution file for you to view the code yourself. Some classes aren’t used and I’m sure some things could be done better.
Small “error” that started bugging me for a good 30 minutes is the movement. While developing this game I noticed that for some reason, I could move in an angle by pressing the up and left arrow together, and same with down and right, and every variation in between. However when I implemented the shooting with the space bar, I was able to move up and right while shooting, but not up and left. I spent ages looking over the code for some syntax error, until it dawned on me that I’m developing on a laptop. This might not sound like an issue at first, but my laptop’s keyboard is recognised as a PS/2 connection, which if you’re familiar with one for gaming, does not support multiple key press detection as a USB one would. To prove that it was indeed the laptop that is causing the error, I plugged in my old logitech G11 USB keyboard and played the game, and as expected, movement was perfectly fine!
Here’s a small animated picture of the current game in action though:
Next to do is add animations, although for this I’ll have to create my own animation class to work through a sprite sheet at a set speed. I’m worried that if I create these animations I may have to redo a bit of my current drawing code of the baseObject class to support it and keep things modular. I’ll do explosion animations first and then see what I need to do to get the animation class to work with my current objects on the screen such as the player and alien ships. I may not add animations for the lasers as they are so small anyway and fly past the screen.
There’s no set objective yet, or scoring/ending of the game, the aliens just keep respawning and you have to keep shooting. Also there’s no collision between the aliens and the player ship.
Here’s a download link for the current solution though.