   AI Shoot player query
  Hey, I am using SDL (just learning), I've done a bit and learnt a few things, completed most of Lazy Foo's tutorials on SDL.I was just wondering how to get an enemy (the AI) to shoot at the player?I mean, I have absolutly no idea how to get the bullet on the screen, let alone set it on a destination to the player.My function for applying a surface to the screen is as follows:void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip = NULL ){    //Holds offsets    SDL_Rect offset;    //Get offsets    offset.x = x;    offset.y = y;    //Blit    SDL_BlitSurface( source, clip, destination, &offset );}That works perfectly fine for drawing my player, the enemy and the platforms in which the player/enemy stand on, but I cannot draw a bullet, except for at runtime, then all that happens is it follows the player around, yet I cannot get it to move as would happen when the player fires at the enemy or vice versa.   
  Moving to 'For Beginners'.Ok, first thing to remember: SDL is just a media library. It overwrites data on your screen to show things, but doesn't handle any 'moving' of objects. You have to do all that yourself. apply_surface is not relevant here as that's just for the drawing part.To create a bullet, you'd need to:  - create a new object representing the bullet. At the least this will require an x and y position, and something to determine the direction of travel (eg. x_velocity and y_velocity values, perhaps).  - every frame:    - update each bullet by altering the x and y values, typically by adding the velocity values to them.    - check to see if each bullet has hit the player by comparing their locations and their size.    - draw the bullet at its current location (using your apply_surface) function.If you need help on any of these individual points, say which one you have a problem with, and someone should be able to advise.   ;  Thank you for your help, the problem I am having is drawing the bullet to the screen, thats the first task, I am trying to use the following code, I don't know if the bullet is getting drawn behind the background or what is happening, anyway here is the code that I am attempting to use:case SDLK_a:    for(int i = 0; i <= enemy1.x; ++i)    {    apply_surface(pAmmo.x, pAmmo.y, pBullet, screen);    pAmmo.x += bXVel;}   break; I have used a for loop to keep adjusting the bullets x velocity (bXVel = bullet x velocity) until it hits the enemy.;Quote:Original post by Artificial_InstituteThank you for your help, the problem I am having is drawing the bullet to the screen, thats the first task, I am trying to use the following code, I don't know if the bullet is getting drawn behind the background or what is happening, anyway here is the code that I am attempting to use:case SDLK_a:    for(int i = 0; i <= enemy1.x; ++i)    {    apply_surface(pAmmo.x, pAmmo.y, pBullet, screen);    pAmmo.x += bXVel;}   break; I have used a for loop to keep adjusting the bullets x velocity (bXVel = bullet x velocity) until it hits the enemy.That code looks as though you are going from the far left of the screen to the enemy's X position and drawing a bullet at each point along the way.. Incrementing the x position by bXVel.. However, it looks as though it is only executed when hit the 'a' key on your keyboard. Also, since your SDL_Rect (I am assuming it is a rect due to the notation) for your bullet, pAmmo, does not reset, the x will just keep on increasing infinitely until it is eventually off screen.. I would go ahead and set pAmmo.x to 0 until you can get it to show up, like you said, then worry about velocities.A problem I might see here is: if before or after this line you have an SDL_FillRect() call or other apply_surface for a background, all those pAmmo apply_surface's will be covered right up without you seeing them.. Maybe there might be a flash for a single frame. What you want to do is make sure you are apply_surface()'ing after any FillRect()/Background-blittings you might have, and outside of that special case where the letter 'a' is pressed.Also do exactly what Kylotan said once you get your ammo to show up.   ;  Right, got the bullet to show thank you! Now I want to know how to unload the image when it hits an enemy?I have tried SDL_FreeSurface(enemy); but that causes the program to exit, presumably because SDL will not let you unload surfaces durinbg run time, any suggestions?   ;Quote:Right, got the bullet to show thank you! Now I want to know how to unload the image when it hits an enemy?Typically you would not free the image when the bullet entity expires, but rather keep it around so that you can re-use it for the next bullet that's created.Quote:I have tried SDL_FreeSurface(enemy); but that causes the program to exit, presumably because SDL will not let you unload surfaces durinbg run time, any suggestions?You can free a surface (or other similar resource) at any time. If this is causing your program to exit, it's probably because you're doing something else wrong (such as freeing the surface more than once, or trying to use the surface after it's been freed).   ;  Thanks for that, how would I go about "hiding" the bullet then? Or in the same sense, unloading or making the enemy disapear once it has been hit?   ;Quote:Thanks for that, how would I go about "hiding" the bullet then? Or in the same sense, unloading or making the enemy disapear once it has been hit?A typical approach would be to store the entities (bullets, enemies, etc.) in a dynamic container of some sort; in C++, for example, you might use std::vector for this.When an entity is created, you add it to the container. When it's time to remove it from the simulation (that is, when you want it to 'disappear'), you remove it from the container.   