Thread: Random arrows to come after each other

  1. #1
    Registered User
    Join Date
    Nov 2016
    Posts
    6

    Random arrows to come after each other

    hi im new to programming and actually this is my project for first semester in collage and i kinda hit a dead end.
    i need some arrow shapes to come after each other like 300 miliseconds delays.i wirte the code to randomly create a number every 300 mili seconds but the problem is after i assign the number to the function which i create arrow shape and all that one arrow come and goes to the end and after it being complete the next arrow appear when i want them to appear in the same time like three arrows be in the screen for about after a second. heres the code for random function and one of the arrows.

    Code:
    void Random(void)
    {
        int a,n;
        int RANDOM[1000];
        while(1)
        {
    
    
                for(n=0;n<1000;++n)
                {
                       RANDOM[n]= rand()%(4);
                }            
                for(int i=0;i<1000;i++)
                {
                    a = RANDOM[i];
                    if (a==0)
                    {
                        Up();
                    }
                    else if (a==1)
                    {
                        Down();
                    }
                    else if(a==2)
                    {
                        Left();
                    }
                    else 
                    {
                        Right();
                    }
                    break;
                }    
        }
    }
    Code:
    void Up (void)
    {    
        void gotoxy(int,int);
        int i;
        char btn;
            for (i=80;(i>=15)&&(!kbhit());i-=2)
            {
                Human();
                Wall();
                Grass();
                SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),121);
                gotoxy(i,19);
                printf("||");
                gotoxy(i,18);
                printf("||");
                gotoxy(i,17);
                printf("/\\");
                Sleep(100);
                system("cls");
            if ((i==16)&&(!kbhit()))
            {
                system("cls");
                printf("GAME OVER\n");
                printf("%d\n",HIGHSCORE);
                getch();
                printf("ENTER YOUR NAME:");
                Menu();
            }
            }
            btn=getch();
            if (btn==UP)
            {
                system("cls");
                HIGHSCORE++;
                Random();
            }
            else if (btn==(DOWN)||(LEFT)||(RIGHT))
            {
                system("cls");
                printf("GAME OVER\n");
                printf("%d\n",HIGHSCORE);
                getch();
                printf("ENTER YOUR NAME:");
                Menu();
            }
    }
    thanks.
    Last edited by parham-box; 11-25-2016 at 04:58 PM.

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    I can't understand your question.

    The RANDOM array serves no identifiable purpose.

    This
    Code:
    else if (btn==(DOWN)||(LEFT)||(RIGHT))
    should be
    Code:
    else if (btn == DOWN || btn == LEFT || btn == RIGHT)

  3. #3
    Registered User
    Join Date
    Nov 2016
    Posts
    6
    thanks for the respond.
    look right now one arrow comes and after it key is pressed will be gone.and there will be always only one arrow in the screen. i want arrows to come after each other like if didn't press any key after every 300 milisecods an other arrow appear in the screen while the arrow before is still in the screen.
    and about the array a had written this program with out it but one of my friends told me the thing i want to do is possible if i write it with array so i write it with array.
    i hope you understand my problem now.
    thanks

  4. #4
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    I think I understand what you want.

    In Up you have a loop going from 80 down to 16. If it reaches 16, the game is over. There is a delay of 100ms between each iteration of the loop and it draws an arrow (the same arrow) every iteration. You want other arrows to be displayed while this loop is still going on, every 300ms. I don't know if you want the arrows drawn on top of each other, beside each other. I don't know what your game looks like at all so its hard to think about it.

    Anyway, hopefully you can see the difficulty with the current design. I don't know how to fix it without an overhaul. It seems unlikely that the loop should be in Up, but again I don't know what your program is supposed to do. Looks like some very strange recursive calls going on there.

    You'd have to post the whole program.

  5. #5
    Registered User
    Join Date
    Nov 2016
    Posts
    6
    ok i was working non stop and i finally did it using this code :
    Code:
    void Random(int x)
    {
        if(x == 1){
            int a ;
            srand(time(NULL));
            a = rand() % 4;
            switch(a){
            case 0 : 
                ArrowShape[Arrow] = 'w';
                break;
            case 1 : 
                ArrowShape[Arrow] = 's';
                break;
            case 2 : 
                ArrowShape[Arrow] = 'a';
                break;
            case 3 : 
                ArrowShape[Arrow] = 'd';
                break;
            }
            Arrow++;
        }
        system("cls");
        int k;
        gotoxy(2,8);
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),121);
        for(k = 0; k < Arrow ;k++){
            switch(ArrowShape[k]){
                case 'w' :
                    char btn;
                    if(!kbhit())
                    {
                    gotoxy(80 - (Arrow - k)*8,17);        
                    printf("/\\");
                    gotoxy(80 - (Arrow - k)*8,18);
                    printf("||");    
                    gotoxy(80 - (Arrow - k)*8,19);
                    printf("||");                
                    Sleep(200);
                }
                    if((80 - (Arrow*8)) == 16)
                    {
                        GameOver();
                        break;
                 }
            break;
    
    
    
    
                case 's' : 
                    gotoxy(80 - (Arrow - k)*8,17);        
                    printf("||");
                    gotoxy(80 - (Arrow - k)*8,18);        
                    printf("||");
                    gotoxy(80 - (Arrow - k)*8,19);
                    printf("\\/");
                    Sleep(200);
                    if((80 - (Arrow*8)) <= 16)
                    {
                        GameOver();
                        break;
                    }
                break;
                case 'a' : 
                    gotoxy(80 - (Arrow - k)*8,17);        
                    printf("/---");
                    gotoxy(80 - (Arrow - k)*8,18);        
                    printf("\\---");
                    Sleep(200);
                    if((80 - (Arrow*8)) <= 16)
                    {
                        GameOver();
                        break;
                    }
                break;
                case 'd' :
                    gotoxy(80 - (Arrow - k)*8,17);        
                    printf("---\\");
                    gotoxy(80 - (Arrow - k)*8,18);        
                    printf("---/");
                    Sleep(200);
                    if((80 - (Arrow*8)) <= 16)
                    {
                        GameOver();
                        break;
                    }
                break;
            }
        }
    
    
    }
    but now the problem is that i dont how to make the arrow in left deleted when i click one of W,A,S or D keys.any ideas how to do that?
    thanks

  6. #6
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    srand(time(NULL)) should only be called once in the entire run of your program. Put it early in main.

    Look how much cleaner your code can look:
    Code:
    BOOL setAttr(WORD w) {
        return SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), w);
    }
    
    void printat(int x, int y, const char *str) {
        gotoxy(x, y);
        printf("%s", str);
    }
    
    void Random(int x) {
        if (x == 1)
            ArrowShape[Arrow++] = "wsad"[rand() % 4];
    
        system("cls");
        setAttr(121);
    
        int k;
        for (k = 0; k < Arrow; k++) {
            int x = 80 * (Arrow - k) * 8;
    
            switch (ArrowShape[k]) {
            case 'w':
                printat(x, 17, "/\\");
                printat(x, 18, "||");    
                printat(x, 19, "||");                
                break;
            case 's':
                printat(x, 17, "||");
                printat(x, 18, "||");
                printat(x, 19, "\\/");
                break;
            case 'a':
                printat(x, 17, "/---");
                printat(x, 18, "\\---");
                break;
            case 'd':
                printat(x, 17, "---\\");
                printat(x, 18, "---/");
                break;
            }
    
            Sleep(200);
            if (80 - (Arrow * 8) == 16)
                GameOver();
        }
    }
    I think you are asking how to remove the displayed arrow from the screen when the matching key (w,a,s,d) is pressed. You would need to remove that arrow from the ArrowShape array (presumably shifting the array contents over).

    Is more than one arrow of the same direction allowed? Is it possible for more than 4 arrows to be displayed?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Up/down arrows
    By nowber in forum C Programming
    Replies: 3
    Last Post: 10-31-2009, 08:35 AM
  2. how to use arrows
    By wyvern in forum C++ Programming
    Replies: 9
    Last Post: 10-12-2005, 01:19 PM
  3. Arrows
    By 14n in forum C++ Programming
    Replies: 4
    Last Post: 10-02-2002, 11:30 PM
  4. Arrows in Dev?
    By Inquirer in forum Game Programming
    Replies: 5
    Last Post: 08-21-2002, 04:30 PM
  5. arrows
    By Commander in forum C Programming
    Replies: 2
    Last Post: 02-25-2002, 07:56 AM

Tags for this Thread