Beginner programmer needs help creating objects on-the-fly

Hello everyone!

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, because I want a new instance of the laser created every time, and deleted once it goes off the screen, so the player can shoot at any time.

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. (“http://pastebin.com/8xe0UdpW”)

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.

I am pretty clueless as to what to try next, can anyone help a newbie out?

Thanks in advance :slight_smile:

Hello everyone!

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.

What I do is I have a base class for all game objects called game_object.
All classes that you want to run in the game loop will inherit from the
game_object class. In the game engine you have a vector (dynamic array) of
the game_object type ( ie. “vector<game_object*> objects” ) which you
iterate through calling whatever functions you need to update the object.

My example: http://pastebin.com/ENYpgDLn

Advantages:
Easy Object management

Disadvantages:
You must read/write variables through special getter/setter
functions
object management overhead

Improvements:
game_object getter/setter functions (included in the comments)
game_object string identifiers - makes in game objects search able
Put all game resources into vectors and give them string
identifiers (eg. vector< pair< string,SDL_Surface* > >) - makes game
resources searchable, to get a resource you don’t have to read a file
(slow) you just load it from memory (fast)

Vector datatype info: www.cplusplus.com/reference/stl/vector/

Hope this helps in your programming endeavors
Jason WhiteOn Sun, Jan 1, 2012 at 7:20 PM, Norbo11 wrote:

I would go to the sdl site and browse the examples there. There are so many
ways to get where you want to be that is hard to even start. If you can’t
seem to find any examples that help you then email me and ill send you one.
necronology at cox.netOn Sun, Jan 1, 2012 at 6:20 PM, Norbo11 wrote:

**
Hello everyone!

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, because I want a new
instance of the laser created every time, and deleted once it goes off the
screen, so the player can shoot at any time.

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.

I am pretty clueless as to what to try next, can anyone help a newbie out?

Thanks in advance [image: Smile]


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org