View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# C++ falling asteroids

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.

4 replies to this topic

### #1sandorlev  Members

Posted 26 November 2012 - 09:26 AM

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!

### #2KnolanCross  Members

Posted 26 November 2012 - 09:46 AM

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

### #3sandorlev  Members

Posted 26 November 2012 - 10:03 AM

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.

### #4KnolanCross  Members

Posted 26 November 2012 - 10:45 AM

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 secondsif (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, 26 November 2012 - 10:59 AM.

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

### #5sandorlev  Members

Posted 26 November 2012 - 10:59 AM

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 secondsif (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!

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.