Why are the enemies not appearing on the screen?

I also got the rocket to fire backwards.

Ok man! Good to know you are evolving on the tutorial. Keep going and if you need some help, ask me.

now I am trying to addthe text of a highscore.

I get a white box at the bottom of the screen and the scrolling is messed up. What did I do wrong?

How do I fix?

Thanks.

I noticed when I change the window size back to 640 x 480 the problem is gone except the score is not incrementing.

Also the text is blurry. Is this right that it looks like that?

I noticed when I change the window size back to 640 x 480 the problem is gone except the score is not incrementing.

That is because the BG texture is with a dimension lower than the window. See, the window is now with 800x600 and the BG texture is with 640x480. Resize it to the window size and the problema is gone!

Also the text is blurry. Is this right that it looks like that?

I fixed the bug. See the FunctionsDefinitions.cpp at the blitRect(), you are not using the parameter SRC from SDL_RenderCopy(), it is passed as NULL and so, the function will use ALL the texture size as source. To correct it, just put the src rect as parameter source from SDL_RenderCopy()

Note too at the main, you put two init() call, delete the second, you just need to call init() once.

I now added power ups when an enemy dies, however I am getting segfaults when collecting the power ups. What is the cause of this? How can I correct?

gdb states:

Thread 15 “SDLAudioP2” received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0x7fffd37bd640 (LWP 417452)]
0x00007ffff7e0f1a8 in ?? () from /lib/x86_64-linux-gnu/libSDL2-2.0.so.0

How do I track this down to the offending line of code?

Sorry for the delay.

How do I track this down to the offending line of code?

So, you should before compile with the flag -g on the cmd line. After that, run the program inside gdb (I think you did it) and after the seg fault, run the gdb up cmd or down cmd to go up or down the points.

But, the bug is caused because you loaded only 5 sounds, but the array sounds have the size of 4.

//inside FunctionsDefinitions.h
enum
{
SND_PLAYER_FIRE,// turns 0
SND_ALIEN_FIRE,// turns 1
SND_PLAYER_DIE, //turns 2
SND_ALIEN_DIE,//turns 3
SND_MAX,//turns 4
SND_POINTS//turns 5
};
//pointer only to 4 (SND_MAX) positions
static MixChunk * sounds[SND_MAX];
//at the code of loadSounds you do:
sounds[SND_PLAYER_FIRE] = Mix_LoadWAV("334227__jradcoolness__laser.ogg");
sounds[SND_ALIEN_FIRE] = Mix_LoadWAV("196914__dpoggioli__lasergun.ogg");
sounds[SND_PLAYER_DIE] = Mix_LoadWAV("245372__quaker540__hq-explosion.ogg");
sounds[SND_ALIEN_DIE] = Mix_LoadWAV("10 Guage Shotgun-SoundBible.com-74120584.ogg");
sounds[SND_POINTS] = Mix_LoadWAV("342749__rhodesmas__notification__01.ogg");
//its the same AS:
sounds[0] = Mix_LoadWAV("334227__jradcoolness__laser.ogg");
sounds[1] = Mix_LoadWAV("196914__dpoggioli__lasergun.ogg");
sounds[2] = Mix_LoadWAV("245372__quaker540__hq-explosion.ogg");
sounds[3] = Mix_LoadWAV("10 Guage Shotgun-SoundBible.com-74120584.ogg");
sounds[5] = Mix_LoadWAV("342749__rhodesmas__notification__01.ogg");

See it? Th SND_POINTS have the value 5 but because of SND_MAX==4 you just have 4 values to 4 mixchunk pointers!

To solve this, you can make the SND_MAX the last value inside the enum ALWAYS! If you put it before any other, it will take the new position as a lower value and crash.

Fantastic! Thanks for helping me spot that! :slightly_smiling_face:

now I have added music and a highscore table,

the highscore table is not appearing. Is it because I did Highscores highscores; instead of

void initHighscoreTable(void)
{
int i;

memset(&highscores, 0, sizeof(Highscores));

for (i = 0 ; i < NUM_HIGHSCORES ; i++)
{
    highscores.highscore\[i\].score = NUM_HIGHSCORES - i;
}

}

on SDL2 - Creating a 2D Shoot 'Em Up - parallelrealities.co.uk

Thanks.

I have made some progress in getting te highscore table on the screen but now when I press ctrl the game wont start. can you show me why?

Also I notice the functions in the source code for part 13 are different from the code in the tutorial so it is difficult to know the correct procedure.

I saw and you are calling initStage() always when the ctrl is pressed. In truth, you must call it only once and hide the blit of highscores.

In this case, I strong recommends you to learn about state machines. In games, it works like a “shor memory” for the current general actions taken.

For example:

1–you create a int state to keep the game state

2–in your logic() you must make something like this:

//ne logic code
if (state == SCREEN_GAME) {
    std::cout<<"logic of game"<<std::endl;
	doPlayer();

	doEnemies();

	doFighters();

	doBullets();

	spawnEnemies();

	clipPlayer();

	if (player == NULL && --stageResetTimer <= 0)
	{
		resetStage();
	}

	doExplosions();

	doDebris();

	doPointsPods();
}
else if (state == SCREEN_HIGHSCORES) {
  std::cout<<"Here you run the logic of highscores\n";

}

See the code above NOT HAVE any way to go to another state, but just the fixed state.

In each state, you must check for events (keyboard, mouse, click button etc) and make the variable state change its value to the other screen.

For example:

--ask yourself: If the player is in the SCREEN_GAME AND him die, which screen must it have to go? Keep the same SCREEN_GAME? Or it must go to SCREEN_HIGHSCORES? And yourself answer it and you should have the new state. Note that the “player die” is a event only happens on the SCREEN_GAME, but never on the SCREEN_HIGHSCORES! So, you use a “if” to check if player dies, and when that happens, you change the variable state to the newer screen (SCREEN_HIGHSCORE)

--now, on the new state SCREEN_HIGHSCORES, you must make others questions similar to the above: if we are on the SCREE_HIGHSCORES and the player press ctrl, what I should make? You said that should do “change to the SCREEN_GAME” and start a new game session. And so, you can finalliy change the state to SCREEN_GAME and call the initState(). On the next loop, the logic will go to the if (state == SCREEN_GAME) and the logic changed again!

--and go on on this go to one, go to other. It is the state machine on a simplified way.

All state machines have this properties:

1–a variable who keeps the state (I always use int)

2–identify what if (“events”) I must check inside each if(“current state”)

3–identify which are the events and what states them must goes to after the events occour.

When you understand this, your life as gamedev will go explode! The trick is simple, but you need to practise a lot to make it high efficient.

Ok will research this. Thanks for your help

If you have some doubt, just ask, I have the whole day without any work.

I have now implemented a game state if else ladder and it is running. I want to add a variable lives to laser object player in static int bulletHitFighter(Laser *b) and increment it down from 5 every time the player is destroyed and when it reaches zero I change game state to HIGHSCORESCREEN. I added some cout to the if statement to track player lives. Why is the cout not responding>

Thanks

Man, the code of your state machine to control the flow of the game is wrong. I will teach better how to you can learn fast:

But firstly, the wrong assumptions:

1–a state machines has live at the gameloop, in your game, you are using something like this:

if (myGameState == GAMESCREEN) {
  ...
  here you started a new game loop with while
  //the loop draw all the things and receives inputs
}
else if (myGameState == HIGHSCORESCREEN) {
 // here, it havent any loop!
}

2–note what I speak about the if/else of states was INSIDE the function logic(), that is because the main loop will call the function logic() and inside the logic() you can choose the flow for which the current state is! So, is just a little misundertood and dont can be a “sin”.

See it? What you can do to correct the code is:

1–firstly, you should see the game code as a main loop which will always run repeatidly.

2–the game code and the screen uses the same main loop and you just need to control the flow it them through the variable myGameState

3–you dont need many main loops to change the screen, you just need to change the flow of its through a variable as the myGameState. So, just myGameState needs to change its values and the game by itself changes the flow to the “if(myGameState == STATE)”

4–you can check the myGameState in any part of the game! And that too inside the main loop, the main draw() function, etc, and even the event loop! So, if you are in the game screen, you call the events just for the game screen with a:


Inside the event loop
if (myGameState == GAMESCREEN) {
  process events for gamescreen
}
else if (myGameState == HIGHSCORESCREEN){
  process events for highscore screen
}
else if(myGameState == OTHERS_MANY_MORE){
  process events just only for others many more
}

The same if/else ladder can be done to logic and draw too!

I see that you are the right way to understand the state machines, so, just keep trying to you really learn! I can guide you in this work. So, just give the answers is not a good thing to you, sometimes is a good thing to fight for what we desire, by, fight as a donkey is never a good strategy cause you gain only “hit on the 4ss” to learn you never really learn! hahaha

You can ask me and keep going the question in this topic.

Good luck!

ok I have updated the game loop and if else ladder and it runs the game. Now I am trying to do if(lives<=0) myGameState = HIGHSCORESCREEN. Why is it not changing the variable and showing the cout?

Thanks