Jump to content

  • Log In with Google      Sign In   
  • Create Account


How to make ship capable of shooting


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
18 replies to this topic

#1 Tetriarch   Members   -  Reputation: 346

Like
0Likes
Like

Posted 22 March 2011 - 10:02 PM

Hi,

I am working on my own space invaders I called it "Retro Invaders" and I have a problem.

I don't know how to figure out shooting. At the begginig of dealing with this problem I had a lot of ideas, but each of this idea failed.

Well I don't want some kind of complex weapon system. I just want shoot straight up from down.

Does anyone know how to make it?

If does, I would be very glad when I will know it.

I am writing in C/C++ using SDL, nothing more, nothing less.




Please help.




Tetriarch






Sponsor:

#2 Splinter of Chaos   Members   -  Reputation: 239

Like
0Likes
Like

Posted 22 March 2011 - 11:30 PM

It's hard to tell what you need to know. Is your problem with spawning the bullet, moving it, colliding it with the other objects, making sure there's no friendly fire, or other?

#3 Tetriarch   Members   -  Reputation: 346

Like
0Likes
Like

Posted 23 March 2011 - 01:21 AM

I have prblem with spawning the bullet and with moving... Everytime I ran the program with my bullet system, the program fell down...



#4 simpler   Members   -  Reputation: 913

Like
0Likes
Like

Posted 23 March 2011 - 02:17 AM

If your program crashes then you've got a bug, which is really hard for us to help you with if you don't show some code. Try to debug and find out where it crashes and post that code.

#5 Tetriarch   Members   -  Reputation: 346

Like
0Likes
Like

Posted 23 March 2011 - 05:20 AM

Well, I don't know why it crushes. But I still thing I need how to design it,  how to solve problem with spawning andmoving of the bullet.




Sorry for mistakes, my english is bad.



#6 Tetriarch   Members   -  Reputation: 346

Like
0Likes
Like

Posted 23 March 2011 - 12:37 PM

I just want to figure out how to make it.

#7 simpler   Members   -  Reputation: 913

Like
0Likes
Like

Posted 23 March 2011 - 02:54 PM

Have a list of Bullets. When the player presses the fire button add a new bullet to the list, the direction of it should be the same as the player.

The list of bullets will get iterated in the update function for collision detection + moving and in the draw function to draw them. If the bullet have travelled more than X units or hitted a target then just remove it from the list and do what's necceassary with the target.

the stl vector or list should do the trick.

#8 kdmiller3   Members   -  Reputation: 176

Like
0Likes
Like

Posted 23 March 2011 - 03:51 PM

List is probably the best choice since bullets can get removed from the middle by a collision. (You'd otherwise want to use a deque instead of a vector since you would be adding new bullets to one end and removing expired bullets from the other.)

#9 Splinter of Chaos   Members   -  Reputation: 239

Like
0Likes
Like

Posted 23 March 2011 - 04:43 PM

List is probably the best choice since bullets can get removed from the middle by a collision. (You'd otherwise want to use a deque instead of a vector since you would be adding new bullets to one end and removing expired bullets from the other.)


I've found it really has no impact what container you use unless you're dealing with a performance critical section on code. Aside from that, the reallocations of the vector are not really so scary as they only happen a few times in the program's entire execution and reserving that space gets rid of the whole issue.

And on getting removed from the middle: To delete an element from a vector, or any array, one can swap it with the last element and then reduce the size of the vector by one. I have a game where at any one time, there can be five thousand particles stored in a vector. Removing particles from that vector is a trivial process in my game. I just use this line:

particles.erase ( 
                remove_if (
                    particles.begin(), particles.end(),
                    some_boolean_func
                ), 
                particles.end() 
            );

Just to give an idea of how trivial this is, that line is run four times a frame with often over sixty frames per second. That means it's run at least 240 times a second.

Point is, unless you have a good reason, use whatever container makes your job easiest.

#10 Tetriarch   Members   -  Reputation: 346

Like
0Likes
Like

Posted 23 March 2011 - 04:46 PM

Thank you very much simpler I am so glad I know how to figure out this. When I will have my code written I'll show it up...

#11 Tetriarch   Members   -  Reputation: 346

Like
0Likes
Like

Posted 28 March 2011 - 05:23 AM

Damn it, the problem still exists. Well I have written code which certainly should work. Well, I think it should. But it doesn't...

Can someone help me please and tell me where is the problem?

/* this is the function which I use when I press SPACE */

void Game::create_bullet()
{

Bullet tmpBullet;
tmpBullet.init("bullet.png");


/* Because I haven't class Player or something like that... */

tmpBullet.x = 320;
tmpBullet.y = 240;


bulletList.push_back(&tmpBullet);
}


bool Bullet::init(char *file)
{
if((surface = Surface::load_image(file)) == NULL) // surface is SDL_Surface* in the class Bullet. Surface::load_image is function in the Class Surface. load_image is static
{
printf("error! :: Can't load image %s\n", file);
return false;
}
w = surface->w;
h = surface->h;
return true;
}





void Game::loop() //Game is Class with main Loop
{
for(unsigned int i = 0; i < bulletList.size(); i++)
{
if(!bulletList[i])
{
bulletList[i]->move();
}
}
}



void Game::render()
{
for(int i = 0; i < bulletList.size(); i++)
{
if(!bulletList[i])
{
bulletList[i]->render(screen);
}
}

Surface::apply_surface(ahoj,screen,50,50);
SDL_Flip(screen);
}



The problem: When I press SPACE, nothing happens.

And I am clearly sure, that functions Surface::load_image(char *file); and Surface::apply_surface(SDL_Surface* source, SDL_Surface* destination, int x, int y); work properly.




Please help!




Tetriarch









#12 DeafManNoEars   Members   -  Reputation: 459

Like
0Likes
Like

Posted 28 March 2011 - 05:54 AM

if(!bulletList[i])
{
bulletList[i]->move();
}

... REMOVED ...

if(!bulletList[i])
{
bulletList[i]->render(screen);
}



Didn't review in it's entirety, but your conditionals don't look right. Try removing the '!' from them. You might want to add a "bAlive" boolean to your bullet class to determine when you should stop updating a specific bullet and delete it from the list.

#13 simpler   Members   -  Reputation: 913

Like
0Likes
Like

Posted 28 March 2011 - 06:52 AM

Yeah your conditions are messed. Right now it checks if bulletLust[i] is a NULL pointer and calls move if it is. Like DeafManNoaEars said take a look on that.

#14 Tetriarch   Members   -  Reputation: 346

Like
0Likes
Like

Posted 28 March 2011 - 07:53 AM

the program is now crushing down...

#15 Trienco   Crossbones+   -  Reputation: 2057

Like
0Likes
Like

Posted 28 March 2011 - 10:47 AM

You know, putting the pointer to a local variable into a list and expecting it to point to anything meaningful after leaving the function is quite optimistic. Unless you a) intend to derive different bullet classes with special behaviour or B) the objects are really big there is no point in dealing with pointers anyway. Else, create them on the heap with new and don't forget to delete them when they get removed.
f@dzhttp://festini.device-zero.de

#16 Tetriarch   Members   -  Reputation: 346

Like
0Likes
Like

Posted 28 March 2011 - 11:05 AM

Trienco - can u write me code for and explain me that? Not entire project but only the part when it creates bullets and then adds to vector, and than loops in game::loop and gane::render... Please




And the problem with local variable I saw that for example at http://www.sdltutorials.com/sdl-maps/ -> loading from file. I saw that with entities too.



#17 yaustar   Members   -  Reputation: 530

Like
0Likes
Like

Posted 28 March 2011 - 01:10 PM

Tertriarch - Do you know anything about variable scope?

#18 Tetriarch   Members   -  Reputation: 346

Like
0Likes
Like

Posted 28 March 2011 - 02:45 PM

ok just tell I am stupid... It seems that this is exactly what I need to know...

#19 Tetriarch   Members   -  Reputation: 346

Like
0Likes
Like

Posted 28 March 2011 - 03:00 PM

and to Trienco DeafManNoaEars and also simpler thanx, I already solved this problem...




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS