I'm quite new to game programming and this forum. I use C++ and SDL as my graphics API to produce games, and so far I have 1 tic tac toe game under my belt, which I have completed about a year ago. Since then I haven't touched a single piece of code, and a week ago I started going back into it and I'm really rusty.
I decided that my next project would be a simple space shooter game clone, and I am having some problems with it. Basically, every time the player presses space-bar, I want to create a laser beam on the screen, move it all the way up with the speed determined by the laserSpeed variable, then deleting the laser once it goes off the top of the screen. I'm a little bit confused as to how I should go about doing that.
I tried various things, setting various flags, trying a bit of arrays, and I don't seem to be doing it right. Would anyone kindly point me in the right direction?
Here is the source code.
Its quite small, all I have so far is the loading/cleanup functions and 2 basic player and laser classes. The movement of the spaceship is done. If you are willing to help, the functions which would interest you are main/player::handleLogic/player::render/laser::handleLogic/laser::render.
Currently I'm going wrong on the visible and hasShot variables... feel free to delete them, they are probably utter rubbish.
I am pretty clueless as to what to try next, can anyone help a newbie out?
I'll try to explain what I'd do in such a situation.(hope I got right what you want though)
Have some class called projectile or whatever - it can contain the x,y and time_alive.
static bool Create();
static void Update();
static void Shutdown();
Now, each time you check if the user hits space you should use Create() to create a new projectile. The create function should take care of initializing your projectile correctly and putting it into a list with all of your other projectiles.
The Update() function should check this list for each of the projectiles in the "projectile's list" and do a collisions check(or whatever you want), and check how much time the given projectile is "alive". Basically each time you create a projectile(Create()) created_at = Millisecs() (Millisecs() should be some function that returns the current time in milliseconds). In the Update check you can decide how much time you'll give each of your projectiles to live ( 1s example : if created_at+1000\ destroy the given projectile). You can also do a check if the projectile has "reached the end of the screen" each Update() (example: if( y>screen_borderY) -> destroy the projectile). Btw you gotta remove the projectile from the list where you keep track of each of the projectiles each time you destroy one.
And when you end your program you gotta clean up all of the left projectiles in the list (basically your Shutdown function should do this).
You should have something like this in the main loop in main:
Error("Could not create projectile");
//at the end of main or where you end the program