Day Four

Skipped an actual day of the week (Did no work on space invaders during the 2nd of December) with other work.

Got into it today, the thing i wanted working today is human movement and firing.

Started by first solving a memory access area I was facing due to not setting a pointer (SDL_Surface) to NULL after freeing it. Added a struct for representing all the invaders on the field, this was as followed, it has a boolean which is is it alive or dead (can be considered as visible or invisible), and the invader instance, this is a pointer to either topInvader, middleInvader or bottomInvader. Each of those 3 are an Invader struct which has a pointer to the surfaces for frame1 and frame2, granted this could really be inline so it doesn’t have to go find the other struct (but this way saves a little bit of memory). The invader struct was created before when the images were to be loaded on the Initialization of the game state.

struct Invader {
SDL_Surface *frame1;
SDL_Surface *frame2;
};
struct InvaderInstance {
struct Invader *invader;
bool alive;
};

Also did some cosmetics changes, fixed some assets, and added the background to the game window, and introduced a structure for the Player information (position and surface), might consider moving score and lives into this. So added the code to load the player image on init, and then display it to the screen, then changed the event handler so escape will quit, left and right will move left and right. Originally I was using the SDL function to Repeat keys so if you hold a key down for a certain time it triggers a repeat which will generate that event multiple times at a set interval, this would of been good as it meant Time Based rather than frame rate depend movement. I then later changed it to be Frame based, so when you hold the arrow key it will remember that and if u keep doing it each frame it will move you a pixel. While reading the SDL documentation I found there is a method called SDL_DisplayFormat which will create a new surface from an existing one but Covert it to use the same modes as the current display. Otherwise each time you bit the surface to the screen it will have to perform the conversion.

Had a kind of big and frustrating setback, a bug in the movement code, it ended up being I had forgotten the break statement after the LEFT arrow key handler so it was then performing the RIGHT arrow key handler. Got side tracked a lot after this thinking how best to handle the Fire event, i had considered making use of a custom ‘user’ SDL event, so, when you press fire it creates the event, puts it on the event queue, and then have a handler so if fire, then we decrease the Y (moving it to the top of the screen), and also then handle the collision detection there, so its in the event handler, and if its not out of screen bound or hit something then Push it back n the queue so it gets called next time. Okay that wasn’t entirely true, that what i have in mind now and i actually like that idea as it moves the detection/handling of a fire into the Event Handler and out of the Rendering code. At the time i was thinking of a Timer which would generate the Fire events every X seconds. At the time of writing this im thinking when i get time I will indeed replace the fire stuff with an event, which it will process in the event handler.

For the collision detection i choose to do 4 prelim condition statements, first consider if the bullet is Inside the frame of invaders so the Bullets Y is below the bottom of the bounding box of all the invaders as a whole, then did if Bullet is pass the top of the invaders, then is it to the left of the bounding box, then to the right. Really i should switch that around so it does the pass the top. last. because if its with in above the bottom, and not within in the x it wll wait till it gets to the top to ignore it, so it will keep doing the y ones then the x, where other way around it will exit sooner on the case where you shoot outside and if you not within the sides (X and X + width), then there is no point of testing if you above it.

Very last thing i did was added the the scoring since Weesals mentioned it, which was quite easy based on the row which the invader we hit is in, add that to the score.

%ATTACHMENT: si_g4.jpg%

So still plenty to do, like the changing of how firing works to use events, make the Vader MOVE, (just a matter of incrementing the position base, and dealing with wrapping bouncing etc), thou Vaders movements are suppose to be timed based on the number left, so should be fun.

Then once that done if they reach the end decrease a life and TRY again, then Vader firing, the shields, loosing a life if a Vader hits you. Also just noticed I initializes the lives to 0 😛 will make that 3 or 5. Oh also forgot I need to do the PAUSE screen, and will need a Game Over screen.

Really want to do the pause screen first, but if the vaders move then, that version can be shared as a demo since it has a some point.

Advertisements