Why are the enemies not appearing on the screen?

I’m writing a game based on these two tutorials Lazy Foo' Productions - Motion

I managed to get enemies on the screen with

now I’m trying to get the enemies firing back but they are not appearing on the screen. Csn anyone tell me what to do to fix this, I checked all the logic and it seems to match the source code for the tutorial. What have I missed?

$ g++ main.cpp -g -lSDL2_image -lSDL2

Like I wrote in your previous thread - don’t put the code on some dropbox. Create a github repository, put the code there and paste a link to it here.

Also, did you follow the steps that Peter87 suggested?

Hi I put some cout in the functions to see if they are being called and I also spotted a logic error in resetStage which I fixed. now the enemies are appearing but the ship is dissappearing and reappearing and the lasers are not firing regularly and the enemies aren’t firing. Please help, is it something to do with the ship being reset to null? Is the code in main causing any problems? Thanks.

Please show me how to move forward in pinpointing the problem.

The Stage.h/.cpp is missing. Add them and upload to the repo. I will help you with the game.

Thank you! Looking forward to some specialist tuition!

Ok man, I solve the bug.

So, in the FunctionsDefinitions.cpp inside the initPlayer() you miss to setup the player->health to some positive value. As you don’t inited it, probably the compiler put trash value inside it and the value goes negative. So, as in the doFighters() you are not prevendting the int health to go under 0 (but just == 0) and the game logic crash.

Then, just change it to e->health <= 0 inside doFighters() and remember to init the value of player->health to some positive integer. Oh, and also, set the player->next to NULL after you put the firghtersTail->next points to player. The player->next is not inited to NULL automatically!

The code is a little confusiing but enough because you are learning now.

Just some code tips:

1–always use indentation! Here in the forum editor is not good to give a example, but search at the google or some IA and ask “how to indent porperly my code”. There are some patterns as the gnu way, but you are free to invent the yours!

2–all classes in c++ call the constructor after creating the object.

For exemple: class Laser{} can have a constructor called the same name as the class inside it, but it is a function. Like class Laser{…Laser(){//here inside constructor}}

The constructor is always called when you create a variable with the type Laser, but, pointers like Laser *ptr never calls a constructor because is just a pointer, but if you make something like Laser * ptr = new Laser() so the new allocator will trigger the constructor of Laser()

3–c++ classes can have a destructor too! Desctructor is just a function which is called before free the memory of the class. You can use it to create ways of unload resources, etc.

4–study about data structures, that knowledge will give you the mysterious powers of computing! You already the practice now needs some theory and a little more of training in it.

And the game is good! I know is simple, but is something you should pride yourself for make it happens!

If you wanna some help, let me know.

Thanks so much! It works!

1 Like

I notice when the alien bullets are fired sometimes they move to the right, I want to flip the alienbullet with SDL_RenderCopyEx when this happens so its pointing in the right direction. I cant find anywhere in the code the alienBullet being rendered or blited. Where would I put the flip instruction in the code ? Thanks.

I notice when the alien bullets are fired sometimes they move to the right, I want to flip the alienbullet with SDL_RenderCopyEx when this happens so its pointing in the right direction.

You should do this things:

1–on the texture of the alienbullet, it is pointing in which direction? Right or left by default?

2–you must create a member at the Laser class to hold the direction ot the alien and player. And, when the alien shoot the bullet, so you set the bullet direction to the alien direction, with it you can flip the texture at the bli() function. You can make the direction on class Laser just a int.

3–you must create another paramenter on the function blit() which will receiver the direction of sprite. It can be just a single int.

The step 2 can be changed to this: when you will call the blit() you pass the parameter of direction relative to the velocity X (the member dx of Laser).

For example:


static void drawFighters(void)
{
	
	std::cout<<"drawFighters!"<<std::endl;
	Laser *e;

	for(e = stage.fighterHead.next ; e != NULL ; e = e->next)
	{
	blit(e->texture, e->x, e->y, e->dx > 0);
	std::cout<<"for loop drawFighters!"<<std::endl;
	}
}

So, the 4th parameter “e->dx > 0” is the direction will be 1 if the “e” is going to right. And inside the blit() you verify if the parameter direction is 1, if so, just pass the parameter SDL_FLIP_HORIZONTAL to the SDL_RenderCopyEx instead of SDL_FLIP_NONE.

Here, I’m supposing the default direction of texture from alienBullet is to left. If the direction of blit() is 0, you just pass the SDL_FLIP_NONE to SDL_RenderCopyEx to get the default texture direction.

Update:

--you should put the code of SDL_RenderCopyEx inside the function blit(). Instead of SDL_RenderCopy change to SDL_RenderCopyEx with the properly parameters!

ok I will attempt to implement this. First I am following the tutorial SDL2 - Creating a 2D Shoot 'Em Up - parallelrealities.co.uk

I am omitting the background and the stars but the explosion is not appearing. Do I have to adjust the logic? can you show me? Thanks.

Man, being honest with you: I think the tutorial you are following very over complicated!

I will write a tiny tutorial for you at my blog, there I only write in pt_BR, but I will write in english this time. I going to use the parallelrealities tutorial as base and make adjust to it.

Just wait some time until I finish it.

Ok will do. In the meantime can you give me some advice on how I may diagnose this logic problem? Thanks

You just need to call the addExplosions() at the if inside the bulletHitFighter(). After set the fighter health to 0 (it died) you must call the addExplosions() to put explosions where the fighter was located.

Ok I will try it tonight. P.S. what is the URL of your blog please?

The link:

https://tutorpro-sam.blogspot.com/

Man, I saw is better to write another code. The use of “linked list” like is killing the readability. I will try to evitate the use of STL and inheritance and keep the code easy to understand.

Maybe at 2 days I can make all the things.

What variables do I need to supply to add Explosions?

I got it to work using the laser x& y and setting num to 10. Thanks for helping me!

I am now trying to add sound

but I get:

In file included from main.cpp:14:
FunctionDefinitions.cpp: In function ‘void initSounds()’:
FunctionDefinitions.cpp:90:8: error: ‘sounds’ was not declared in this scope; did you mean ‘roundl’?
90 | memset(sounds, 0, sizeof(Mix_Chunk*) * SND_MAX);
| ^~~~~~
| roundl
FunctionDefinitions.cpp:91:1: error: ‘music’ was not declared in this scope
91 | music = NULL;
| ^~~~~
FunctionDefinitions.cpp: In function ‘void loadSounds()’:
FunctionDefinitions.cpp:98:1: error: ‘sounds’ was not declared in this scope; did you mean ‘roundl’?
98 | sounds[SND_PLAYER_FIRE] = Mix_LoadWAV(“334227__jradcoolness__laser.ogg”);

how do I define & declare sounds and music?

Thanks

It’s ok I did grep and found out how he declared and the sound FX are working