-1

this is game:my project is a simple game which is a car that should get a way from random bars.and if car accident with bars game is over! i want to write a simple code to move some characters and i'm not going to use c++ libraries functions (only c).

alright,i have a car which move, so I CAN NOT USE SLEEP. because it doesn't let car to move at normal speed. i want this access to move the car and at the same time bars come down from up of page to down.

    #include <stdio.h>
    #include <conio.h>
    #include <Windows.h>
    #include <ctime>

    void gotoxy(int x, int y){
        HANDLE consoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
        COORD cursorCoord;
        cursorCoord.X = x;
        cursorCoord.Y = y;
        SetConsoleCursorPosition(consoleHandle, cursorCoord);
    }
    void sleep(unsigned int mseconds)
    {
        clock_t goal = mseconds + clock();
        while (goal > clock());
    }
    void Road(){
        int i;
        int x = 15, y = 17;
        for (i = 1; i < 22; i++)
            printf("\\                                 / \n");
        printf(" _________________________________");
        gotoxy(x, y);
        printf("****");
        gotoxy(x, y + 1);
        printf("****");
        gotoxy(x, y + 2);
        printf("****");
        gotoxy(x, y + 3);
        printf("****");
    } //Road of game + car 
    void CarMove(){
        int lastx, lasty;
        int x = 15, y = 17;
        char a;
        lastx = x; lasty = y;
        gotoxy(x, y);
        printf("****");
        gotoxy(x, y + 1);
        printf("****");
        gotoxy(x, y + 2);
        printf("****");
        gotoxy(x, y + 3);
        printf("****");
            if (_kbhit()){
                gotoxy(lastx, lasty);
                printf("     ");
                gotoxy(lastx, lasty + 1);
                printf("     ");
                gotoxy(lastx, lasty + 2);
                printf("     ");
                gotoxy(lastx, lasty + 3);
                printf("     ");

                a = _getch();
                if ((a == 77) && (x <= 28)) x++;  else x--;
                if ((a == 75) && (x >= 1))  x--;  else x++;
                gotoxy(x, y);
                printf("****");
                gotoxy(x, y + 1);
                printf("****");
                gotoxy(x, y + 2);
                printf("****");
                gotoxy(x, y + 3);
                printf("****");
                ;
                lastx = x; lasty = y;
            }
            //this function just make a car which move on the terminal
        }

    void main(){
        Road();
        int lastx;
        int x = 15, y = 17;
        int m, n, lastn;
        int askichar;
        int size, i, j, k;

        lastx = x;
        //car code
        do{
            if (_kbhit()){
                gotoxy(lastx, y);
                printf("     ");
                gotoxy(lastx, y + 1);
                printf("     ");
                gotoxy(lastx, y + 2);
                printf("     ");
                gotoxy(lastx, y + 3);
                printf("     ");
                askichar = _getch();
                if ((askichar == 75) && (x >= 2))  x--;
                if ((askichar == 77) && (x <= 28)) x++;
                gotoxy(x, y);
                printf("****");
                gotoxy(x, y + 1);
                printf("****");
                gotoxy(x, y + 2);
                printf("****");
                gotoxy(x, y + 3);
                printf("****");
                ;
                lastx = x;
            }//car code finished
            //bar movement code
            srand(time(NULL));
            size = rand() % (8) + 4;
            m = rand() % (18) + 3;
            n = 0; lastn = 0;
            while (n <= 20){
                gotoxy(m, lastn);
                for (j = 1; j <= size; j++)
                    printf(" ");
                n++; lastn = n;
                gotoxy(m, n);
                for (k = 1; k <= size; k++)
                    printf("+");
            }
            gotoxy(m, n);
            for (i = 1; i <= size; i++)
                printf("_");
            //bar movement code finished
        } while (m != x && n != y  && n + 1 != y + 1 && n + 2 != y + 2 && n + 3 != y + 3);
        printf("                                 GAME OVER                 \n");

    }

what can i do what is different ways to do this ? one more side question: at first i wrote 3 functions 1-road(not important here) 2- carmove (contain a car which move on left or right ) 3- barMovement (bars with random situation and size that come down from top of terminal to end of it) in the main function when i call carmove then i call barMovemen barMovemen never run and this is the code:

#include <stdio.h>
#include <conio.h>
#include <Windows.h>
#include <ctime>

void gotoxy(int x, int y){
    HANDLE consoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
    COORD cursorCoord;
    cursorCoord.X = x;
    cursorCoord.Y = y;
    SetConsoleCursorPosition(consoleHandle, cursorCoord);
}
void sleep(unsigned int mseconds)
{
    clock_t goal = mseconds + clock();
    while (goal > clock());
}
void Road(){
    int i;
    int x = 15, y = 17;
    for (i = 1; i < 22; i++)
        printf("\\                                 / \n");
    printf(" _________________________________");
    gotoxy(x, y);
    printf("****");
    gotoxy(x, y + 1);
    printf("****");
    gotoxy(x, y + 2);
    printf("****");
    gotoxy(x, y + 3);
    printf("****");
} //Road of game + car
void CarMove(){
    int lastx, lasty;
    int x = 15, y = 17;
    char a;
    lastx = x; lasty = y;
    gotoxy(x, y);
    printf("****");
    gotoxy(x, y + 1);
    printf("****");
    gotoxy(x, y + 2);
    printf("****");
    gotoxy(x, y + 3);
    printf("****");
    while (1){
        if (_kbhit()){
            gotoxy(lastx, lasty);
            printf("     ");
            gotoxy(lastx, lasty + 1);
            printf("     ");
            gotoxy(lastx, lasty + 2);
            printf("     ");
            gotoxy(lastx, lasty + 3);
            printf("     ");

            a = _getch();
            if ((a == 77) && (x <= 28)) x++;  else x--;
            if ((a == 75) && (x >= 1))  x--;  else x++;
            gotoxy(x, y);
            printf("****");
            gotoxy(x, y + 1);
            printf("****");
            gotoxy(x, y + 2);
            printf("****");
            gotoxy(x, y + 3);
            printf("****");
            ;
            lastx = x; lasty = y;
        }
        //this function just make a car which move on the terminal
    }
}


void BarMovement(){
    int m, n, lastm, lastn;
    int i, j, k;
    int size;
    srand(time(NULL));
    size = rand() % (8) + 4;
    m = rand() % (18) + 3;
    n = 0; lastn = 0;
    while (n <= 20){
        gotoxy(m, lastn);
        sleep(200);
        for (j = 1; j <= size; j++)
            printf(" ");
        n++; lastn = n;
        gotoxy(m, n);
        for (k = 1; k <= size; k++)
            printf("+");
    }
    gotoxy(m, n);
    for (i = 1; i <= size; i++)
        printf("_");
    //bar movement code finished
}
void main(){
    Road();
    CarMove();
    BarMovement();

}

and sorry for English

alizeyn
  • 2,300
  • 1
  • 20
  • 30

1 Answers1

1

Alright I believe I understand the question the user has here. He basically wants to know how to get user input without blocking since the rest of his program will freeze and that is undesirable.

So to explain this concept of accepting input but not freezing the rest of the program is normally called NonBlocking input, you can find a basic talk about that in C here. How do you do non-blocking console I/O on Linux in C?

That said I would propose that instead you learn multi-threading, this would allow you to have a thread that blocks for user input in one thread without locking up the main thread of your game. It seems scary at first but pthread is a very easy tool to use for your case. Below I provide a link which contains a basic example to pthread which I suggest you look at. http://timmurphy.org/2010/05/04/pthreads-in-c-a-minimal-working-example/

For your case you would need a single thread to spawn which simply waits for user input with any typical user input method, while you have main continue to play the game as normal.

Edit: If you insist on doing things from a timing perspective (bad habit) I can suggest learning interrupts which is a more confusing topic. The basics can be found here Simple Signals - C programming and alarm function and here http://pubs.opengroup.org/onlinepubs/009695399/functions/alarm.html You are looking for something called an Alarm although this is a more confusing topic then pthreads in my opinion. Please do not just use a function like Clock and keep checking the time until a certain amount passes it's a very bad habit to get into.

Community
  • 1
  • 1
arduic
  • 659
  • 3
  • 12
  • tnx but i'm gonna use simple ways like use clock() or some thing like this – alizeyn Dec 04 '15 at 15:12
  • Alright it seems I misunderstood your question to. Please edit your question since it seems that no one knows exactly what you are actually looking for. – arduic Dec 04 '15 at 15:34