Sign in to follow this  
sandorlev

C++ falling asteroids

Recommended Posts

sandorlev    193
Hey guys!

I recently started making a game in C++, using SDL. The idea is that you control a guy on the bottom of the screen and asteroids are falling from the sky. Now I'd like to make it so the longer the game goes, the more (and faster) asteroids are and I could really use some help with that. If anyone could show me some pseudo-code on how it's done, I'd be grateful!

Thanks in advance!

Share this post


Link to post
Share on other sites
KnolanCross    1974
How do you move the asterioids? Basically you should use a formula like this:

nextX = currentPosX + (cos(angle) * moveSpeed * dt);
nextY = currentPosY + (sin(angle) * moveSpeed * dt);

Where:
currentPosX/currentPosY are the current x, y coords of the asteroid.
moveSpeed is the speed which you want the asteroid to move.
dt is the time elapsed since the last time you moved the asteroid.

To calculate the dt you should use SDL_GetTicks().
Increase the moveSpeed variable and you should have your asteroid moving faster,

Share this post


Link to post
Share on other sites
sandorlev    193
The movement part I already have, I'm looking for a solution to making them appear at random times with their number of asteroids onscreen capped from above and below but increasing.

Share this post


Link to post
Share on other sites
KnolanCross    1974
Oh sorry, complety missunderstood what you said.

In this case I believe you need something like this:


[source lang="cpp"]srand(time(NULL));
unsigned numberOfAsteroids = (SDL_GetTicks()/30000) + 1; // 1 extra asteroid per 30 seconds
if (asteroidsOnScreen < numberOfAsteroids){
if (SDL_GetTicks() - lastTimeTriedToSpamAsteroid > 1000){ // try once per second
unsigned chance = rand() % 100;
if (chance >= 50){ // 50% chance to spawn an asteroid
bool up = rand() % 2;
if (up == false){
spawnAsteroid(x = rand()%WIDTH_OF_SCREEN, y = HEIGHT_OF_SCREEN);
}
else{
spawnAsteroid(x = rand()%WIDTH_OF_SCREEN, y = 0);
}
}
lastTimeTriedToSpamAsteroid = SDL_GetTicks();
}
}[/source]

EDIT: in this case y = 0 is top part of the screen. Edited by KnolanCross

Share this post


Link to post
Share on other sites
sandorlev    193
[quote name='KnolanCross' timestamp='1353948332' post='5004230']
Oh sorry, complety missunderstood what you said.

In this case I believe you need something like this:


[source lang="cpp"]srand(time(NULL));
unsigned numberOfAsteroids = (SDL_GetTicks()/30000) + 1; // 1 extra asteroid per 30 seconds
if (asteroidsOnScreen < numberOfAsteroids){
if (SDL_GetTicks() - lastTimeTriedToSpamAsteroid > 1000){ // try once per second
unsigned chance = rand() % 100;
if (chance >= 50){ // 50% chance to spawn an asteroid
bool up = rand() % 2;
if (up == false){
spawnAsteroid(x = rand()%WIDTH_OF_SCREEN, y = HEIGHT_OF_SCREEN);
}
else{
spawnAteroidOnLowerPart(x = rand()%WIDTH_OF_SCREEN, y = 0);
}
}
lastTimeTriedToSpamAsteroid = SDL_GetTicks();
}
}[/source]

EDIT: in this case y = 0 is top part of the screen.
[/quote]

Thank you, that answer was worth waiting for!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this