-
Text mode arcade game
Hello masters,
I'm teaching C++ in my technical school, and I wanted to introduce my class in games world with some simple experiences.
We are making a very simple arcade game, where a ship (^) fires some shots (* . ! A) and so on.
We made it with simple functions, and it worked, but cannot fire more than a projectile simultaneously. So, I decided to buid a class.
But, the main problem is that I have to create one shot when I press the keyboard (and this worked), but it have to keep moving even after I release it. And, when I press again, I want to have another shot created (and moving). How to do this?
If helps to understand, follow the class:
Code:
class cannon
{
protected:
typedef struct fire
{
char bullet;
int x;
int y;
};
void setArms(void)
{
arms[0].bullet='*';
arms[1].bullet='!';
}
public:
fire arms[2];
void shoot(int i)
{
clock_t runTime;
runTime = clock();
gotoxy(arms[i].x,(int)(arms[i].y-runTime/30));
printf("%c", arms[i].bullet);
gotoxy(arms[i].x,(int)(arms[i].y-runTime/30-1));
printf(" ");
};
cannon(int x,int y, int i)
{
setArms();
arms[i].x = x;
arms[i].y = y;
shoot(i);
};
};
-
Which OS/Compiler are you using?
You basically need to poll the keyboard in some way, so that even if there is no key press, you get a chance every half second or so to update the position of the projectiles.
-
If it is not too far for your students to reach (or if you can black-box it so they don't have to deal with it) I would do three simple things:
1. Create a "shot" class that maintains the shots current position, maybe a char to indicate what kind of shot it is (*, ., etc) and finally an "active" boolean initialized to false.
2. At program start, spin off a thread that periodically locks a mutex that would guard this data and walks the array. For each :live" shot, the coordinates would be updated to the next position, the old one erased and potentially do collision detection. If the shot has reached the top of your play-field w/o hitting something, just set the active flag to false.
3. In your main play loop, when the user fires, you lock the mutex guarding shot array, find the first one that is not active, set to coordinates to where ever the shot is supposed to start and set the active flag to true. Your animation thread will do the rest.