#include <stdio.h>
#include <windows.h>
#define RACT 22
#define BALL 2
void gotoxy(int x,int y)
{
    static HANDLE h = NULL;
    if(!h)
    {
        h= GetStdHandle(STD_OUTPUT_HANDLE);
    }
    COORD c = {x,y};
    SetConsoleCursorPosition(h,c);
}
void mohit();
void quit();
int main(int argc, char** argv)
{
LL:
    system("cls");
    system("color 1f");
    system("title GAME Mahdi Jones::
www.Rayanoos.ir");
    char i='\0';
    mohit();
    int chap = 137, rast = 27, bala = 4, paein = 18;
    int x=80,y=45;
    int xb=x,yb=y-1;


    gotoxy(0,3);
    printf("\
Press 'Space' for start\
\n\n  'D' = Move to right\
\n\n  'A' = Move to left\
\n\n  'R' = Restart\
\n\n  'Q' = Quit.");


    while (i!=' ')
    {
        i=getch();
        if (i=='q' || i=='Q')
            return(0);
    }
    gotoxy(0,3);
    printf("\
Press 'Space' for Attack\
\n\n  'D' = Move to right\
\n\n  'A' = Move to left\
\n\n  'R' = Restart\
\n\n  'Q' = Pause and Quit.");
    gotoxy(x,y);
    putchar(RACT);
    putchar(RACT);
    putchar(RACT);
    gotoxy(x+1,yb);
    putchar(BALL);
    gotoxy(x+1,y);
    while(1)
    {
        i=getch();
        if(i == 'd' || i=='D' )
        {
            if (x<chap)
            {
                gotoxy(x+1,y);
                printf("\b ");
                gotoxy(++x, y);
                putchar(RACT) ;
                putchar(RACT) ;
                putchar(RACT) ;
                gotoxy(x+1,yb);
                printf("\b ");
                putchar(BALL);
                gotoxy(x+1,y);


            }
        }
        else if(i == 'a' || i == 'A')
        {
            if (x>rast)
            {
                gotoxy(x+1,y);
                printf("  \b\b\b    ");
                gotoxy(--x, y);
                putchar(RACT) ;
                putchar(RACT) ;
                putchar(RACT) ;
                gotoxy(x+1,yb);
                putchar(BALL);
                printf(" ");
                gotoxy(x+1,y);

            }
        }
        else if(i == ' ' || i == ' ')
        {
            gotoxy(x-1,yb);
            printf("  ");
            for (; yb>bala+1 ; yb-- )
            {
                Sleep(30);
                printf("\b\b    ");
                int opa=x+rand()%6+120;
                if (opa>137)
                    gotoxy(x,yb);
                else
                    gotoxy(opa,yb);
                putchar(BALL);
            }
            for (; yb<y-1 ; yb=yb+2 )
            {
                Sleep(20);
                printf("\b\b    ");
                gotoxy(x,yb);
                putchar(BALL);
            }
            printf("\b\b  ");
            yb=y-1;
            gotoxy(x+1,yb);
            putchar(BALL);
        }
        else if(i == 'r' || i == 'R')
            goto LL;
        else if(i == 'q' || i == 'Q')
        {
            quit();
        }
    }

    return 0;
}

void mohit()

{
    system("mode 200");
    gotoxy(50,0);
    char str[50]={
    77,97,104,100,
    105,32,74,111,110,
    101,115,32,58,32,
    119,119,119,46,82,
    97,121,97,110,111,
    111,115,46,105,114,
    '\0'};
    printf("%s",str);
    int ai,ia=50;
    for (ai=26 ; ai < 140; ai++)
    {
        gotoxy(ai,2);
        putchar('_');
    }
    for (ai=25 ; ai < 140; ai++)
    {
        gotoxy(ai,ia);
        putchar(219);
    }
    for (ai=3 ; ai < ia+1; ai++)
    {Sleep(1);
        gotoxy(25,ai);
        putchar(222);
    }
    for (ai=3 ; ai < ia+1; ai++)
    {Sleep(1);
        gotoxy(140,ai);
        putchar(221);
    }
    //  mavane
   int mx,my;
    for (mx = 27 ; mx<137; mx=mx+6 )
    { Sleep(5);
        gotoxy(mx,5);
        putchar(219);
    }
    for (mx = 27 ; mx<137; mx=mx+4 )
    { Sleep(5);
        gotoxy(mx,7);
        putchar(223);
    }
    for (mx = 27 ; mx<137; mx=mx+2 )
    { Sleep(1);
        gotoxy(mx,9);
        putchar(219);
    }

}

void quit()
{   int x=63,y=24;
    gotoxy(x,y);
    printf(" _________________________________");
    gotoxy(x,y+1);
    printf("|   Are you sure to quit? (Y/N)   |");
    gotoxy(x,y+2);
    printf("|_________________________________|");
    char c;
    while(c!='y' || c!='Y' || c!='N' || c!='n')
    {
        c=getch();

        if(c=='y' || c=='Y')
            exit(0);
        else if(c=='N' || c=='n')
        {
            gotoxy(x,y);
            printf("                                   ");
            gotoxy(x,y+1);
            printf("                                   ");
            gotoxy(x,y+2);
            printf("                                   ");
            return(0);
        }
    }
} // Mahdi Jones
 // FaceBook.com