#1 Members - Reputation: 173
Posted 26 November 2012 - 09:26 AM
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!
#2 Members - Reputation: 450
Posted 26 November 2012 - 09:46 AM
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,
http://16bitsflag.blogspot.com.br/
#4 Members - Reputation: 450
Posted 26 November 2012 - 10:45 AM
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.
http://16bitsflag.blogspot.com.br/
#5 Members - Reputation: 173
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!






