I'm trying to do finishing touches on a simple game for a competition and I hit a snag I hadn't put any thought into until now. I have the random number generator:
My plan was to do aiShip.setCoords(iSecret, -20);, but I don't know how to make iSecret continuously generate random numbers so the ship redraws itself on different parts of the screen. I have to do this for a few ships so that you have more than one ship on screen, and plan to make a second variable to randomize the velocity of each ship between say 1 and 5.
You're not using srand() prior to every rand() call, are you? You'll only get a different result once per second if you do. Call srand() once at the beginning of the program only.
No, I only have srand(time(NULL)); at that start of main. So I just need to do the iSecret = rand() % ... after it is destroyed or goes off screen? Never used it outside of guess my number programs so wasn't sure how to use it for continuous random numbers.
how to make iSecret continuously generate random numbers so the ship redraws itself on different parts of the screen.
You want the ship to appear in different positions (except smooth transitions due to velocity) how often?
One of the things I'm still working out. It is kind of a trial and error type thing. I'm not even sure how many ships I will have appearing on screen and still have to decide if I want to modify the code more to space out the 'entry/launch' time or just leave it to randomly set the velocity and leave it at that. I'm leaning towards the latter as this is due by midnight tomorrow for Moosader's Villain and One button competition.
I just need to do the iSecret = rand() % ... after it is destroyed or goes off screen?
You'd use it when creating a new ship (whether that happens because another one was destroyed, or because it's part of arriving reinforcements or whatever). Then you'd modify its location once per frame with a velocity that might have been initialized with a rand for x and a rand for y. You'd need to modify the velocities to make it change course, naturally.
Can u post a little more code?
Yeah, the competition is pretty much over and I never got it done, so I'm not worried about it now. I got it semi working to like it and feel I accomplished what I set out to do (minus finishing the game). Forgive some of the ugly code, I started trying different ideas without much thought to see what it would do to the game (which is why there are tons of rand calls, was trying to see what effects it would have..some worked like I planned and others not so much).
game.h
init.h
#ifndef INIT_H #define INIT_H #include "game.h" void init(GAME *game); #endif
init.cpp
sprite.h
sprite.cpp
main.cpp