C++ falling asteroids

Started by
3 comments, last by sandorlev 11 years, 4 months ago
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!
Advertisement
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,

Currently working on a scene editor for ORX (http://orx-project.org), using kivy (http://kivy.org).

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.
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.

Currently working on a scene editor for ORX (http://orx-project.org), using kivy (http://kivy.org).


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.


Thank you, that answer was worth waiting for!

This topic is closed to new replies.

Advertisement