How will i add this in?

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)

[code]
#include<iostream>
#include<conio.h>
#include<dos.h>
#include <windows.h>
#include <time.h>
#include <stdio.h>
#include <chrono>


#define SCREEN_WIDTH 90
#define SCREEN_HEIGHT 26
#define WIN_WIDTH 70

using namespace std;

HANDLE consol = GetStdHandle(STD_OUTPUT_HANDLE);
COORD CursorPosition;

int enemyY[3];
int enemyx[3];
int enemyFlag[3];
char car[4][4] = { ' ','±','±',' ',
'±','±','±','±',
' ','±','±',' ',
'±','±','±','±' };

int carPos = WIN_WIDTH/2;
int score = 0;

void gotoxy(int x, int y){
CursorPosition.X = x;
CursorPosition.Y = y;
SetConsoleCursorPosition(consol, CursorPosition);
}
void setcursor(bool visible, DWORD size) {
if(size == 0)
size = 20;

CONSOLE_CURSOR_INFO lpCursor;
lpCursor.bVisible = visible;
lpCursor.dwSize = size;
SetConsoleCursorInfo(consol,&lpCursor);
}
void drawBorder(){
for(int i=0; i<SCREEN_HEIGHT; i++){
for(int j=0; j<17; j++){
gotoxy(0+j,i); cout<<"±";
gotoxy(WIN_WIDTH-j,i); cout<<"±";
}
}
for(int i=0; i<SCREEN_HEIGHT; i++){
gotoxy(SCREEN_WIDTH,i); cout<<"±";
}
}
void genEnemy(int ind){
enemyx[ind] = 17 + rand()%(33);
}
void drawEnemy(int ind){
if( enemyFlag[ind] == true ){
gotoxy(enemyx[ind], enemyY[ind]); cout<<"±±±±";
gotoxy(enemyx[ind], enemyY[ind]+1); cout<<" ±± ";
gotoxy(enemyx[ind], enemyY[ind]+2); cout<<"±±±±";
gotoxy(enemyx[ind], enemyY[ind]+3); cout<<" ** ";
}
}
void eraseEnemy(int ind){
if( enemyFlag[ind] == true ){
gotoxy(enemyx[ind], enemyY[ind]); cout<<" ";
gotoxy(enemyx[ind], enemyY[ind]+1); cout<<" ";
gotoxy(enemyx[ind], enemyY[ind]+2); cout<<" ";
gotoxy(enemyx[ind], enemyY[ind]+3); cout<<" ";
}
}
void resetEnemy(int ind){
eraseEnemy(ind);
enemyY[ind] = 1;
genEnemy(ind);
}

void drawCar(){
for(int i=0; i<4; i++){
for(int j=0; j<4; j++){
gotoxy(j+carPos, i+22); cout<<car[i][j];
}
}
}
void eraseCar(){
for(int i=0; i<4; i++){
for(int j=0; j<4; j++){
gotoxy(j+carPos, i+22); cout<<" ";
}
}
}

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();
}

void play(){
carPos = -1 + WIN_WIDTH/2;
score = 0;
enemyFlag[0] = 1;
enemyFlag[1] = 0;
enemyY[0] = enemyY[1] = 1;

system("cls");
drawBorder();
updateScore();
genEnemy(0);
genEnemy(1);

gotoxy(WIN_WIDTH + 3, 2);cout<<"Midnight Drivers";
gotoxy(WIN_WIDTH + 6, 4);cout<<"----------";
gotoxy(WIN_WIDTH + 6, 6);cout<<"----------";
gotoxy(WIN_WIDTH + 7, 12);cout<<"Control ";
gotoxy(WIN_WIDTH + 7, 13);cout<<"---------";
gotoxy(WIN_WIDTH + 2, 14);cout<<" A Key - Left";
gotoxy(WIN_WIDTH + 2, 15);cout<<" D Key - Right";

gotoxy(18, 5);cout<<"Press any key to start";
getche();
gotoxy(18, 5);cout<<" ";

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;
}
}

drawCar();
drawEnemy(0);
drawEnemy(1);
if( collision() == 1 ){
gameover();
return;
}
Sleep(50);
eraseCar();
eraseEnemy(0);
eraseEnemy(1);

if( enemyY[0] == 10 )
if( enemyFlag[1] == 0)
enemyFlag[1] = 1;

if( enemyFlag[0] == 1 )
enemyY[0] += 1;

if( enemyFlag[1] == 1 )
enemyY[1] += 1;

if( enemyY[0] > SCREEN_HEIGHT-4 ){
resetEnemy(0);
score++;
updateScore();
}
if( enemyY[1] > SCREEN_HEIGHT-4 ){
resetEnemy(1);
score++;
updateScore();
}
}
}

int main()
{
setcursor(0,0);
srand( (unsigned)time(NULL));

do{
system("cls");
gotoxy(10,5); cout<<" -------------------------- ";
gotoxy(10,6); cout<<" | Midnight Drivers | ";
gotoxy(10,7); cout<<" -------------------------- ";
gotoxy(10,9); cout<<"1. Start Game";
gotoxy(10,10); cout<<"2. Instructions";
gotoxy(10,11); cout<<"3. Quit";
gotoxy(10,14); cout<<"Select Option";
char op = getche();

if( op=='1') play();
else if( op=='2') instructions();
else if( op=='3') exit(0);

}while(1);


return 0;
}
Some ideas to think about.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
  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--;

All but one part works, it keeps telling me that drawtime was not declared in this scope. Any fix to that?
> Any fix to that?
Yeah, it's something you need to write.

It's like drawEnemy, only it prints the time in a format and style of your choosing.
Topic archived. No new replies allowed.