Hi, im brand new to coding, some context. i found a tutorial on youtube on how to code a game and i followed the video, but now i wanna make it more unique. i already fixed some of my own screw ups (Wrong lines here and there making the player car invisible, some graphic clean ups). I want to add a timer function, but i dont know how. I did find a proper way to add it in without needing a secondary INT, however i want the timer to be a game over thing. Once time runs out, you see your score (Something else i cant figure out), and its game over. Also a speed up function so the longer it goes, the harder it gets. ill post the code (Its not an original besides some of the graphics). any and all help will be welcomed.
(Summary: I want to know where and how to add in a timer function, make it visible, and a end screen score function)
int collision(){
if( enemyY[0]+4 >= 23 ){
if( enemyx[0] + 5 - carPos >= 0 && enemyx[0]+ 4 - carPos < 9 ){
return 1;
}
}
return 0;
}
void gameover(){
system("cls");
cout<<endl;
cout<<"\t\t--------------------------"<<endl;
cout<<"\t\t------- You Crashed ------"<<endl;
cout<<"\t\t--------------------------"<<endl<<endl;
cout<<"\t\tPress any key to go back to the menu."<<endl;
getch();
}
void updateScore(){
gotoxy(WIN_WIDTH + 7, 5);cout<<"Score: "<<score<<endl;
}
void instructions(){
system("slc");
cout<<"instructions";
cout<<"\n-----------------";
cout<<"\n Avoid Cars by moving left or right. ";
cout<<"\n\n Press 'a' to move left";
cout<<"\n Press 'd' to move right";
cout<<"\n Press 'escape' to exit";
cout<<"\n\nPress any key to go back to menu";
getch();
}
int delay_time = 50;// in milliseconds
int game_time = 0; // in milliseconds
int game_limit = 60000; // in milliseconds, == 1 minute
while (1) {
if (kbhit()) {
char ch = getch();
if (ch == 'a' || ch == 'A') {
if (carPos > 18)
carPos -= 4;
}
if (ch == 'd' || ch == 'D') {
if (carPos < 50)
carPos += 4;
}
if (ch == 27) {
break;
}
}
game_time += delay_time; // time in the game
drawCar();
drawEnemy(0);
drawEnemy(1);
drawTime(game_time); // show it on screen somewhere
if (collision() == 1 || game_time == game_limit) {
gameover();
return;
}
Sleep(delay_time);
// make it harder, by randomly reducing the delay_time
if ( rand() % 100 == 0 && delay_time > 0)
delay_time--;