Home > Mobile >  Loop to change the set while moving the array - C
Loop to change the set while moving the array - C

Time:08-14

How to keep the smoke set changing while the rocket goes up?
I don't understood very well, but the set only changes while the rocket is at the base.And he wasn't supposed to stand still.
veja o movimento do vídeo no link -> https://i.imgur.com/RRvFqBR.mp4
The loop for(int h = 0; h < 29; h ){ maintains the set by changing the condition of the increment, and only takes off after that. Then the set stops changing.

#include <unistd.h>
#include <stdio.h>

#define LINE 11
#define COLN 12
#define R_COLN 12
#define R_LINE 9
#define R_SET 2

#define DELAY 95000

//string to display the rocket
const char rocket[LINE][COLN 1]={
"      ^     ",
"     /^\\    ",
"     |-|    ",
"     |R|    ",
"     |O|    ",
"     |C|    ",
"     |K|    ",
"     |E|    ",
"    /|T|\\   ",
"   / | | \\  ",
"  |  | |  | "
};
const char smoke[R_SET][R_LINE][R_COLN 1]={
{
"    '     * ",
"   *    . ' ",
"  -   .     ",
"  . '  :  . ",
"   '  ' * . ",
"  .  *   .  ",
"  .  '  :   ",
"  .   ' .   ",
"   '        "
},
{
"  *     '   ",
" ' .    *   ",
"     .   -  ",
" .  :  ' .  ",
" . * '  '   ",
"  .   *  .  ",
"   :  '  .  ",
"   . '   .  ",
"    '       "}
};
int main(){
    int jumpControlAtBottom = 0;
    int shifControl = 0;
    int smoke_set = 0;

    for(int h = 0; h < 29; h  ){ //frame
        for (jumpControlAtBottom = 0; jumpControlAtBottom < 28;   jumpControlAtBottom){
            // Jump to bottom of console
            printf("\n");
        }
        for(int i = 0; i< LINE; i  ){
            printf("%.*s\n", COLN, rocket[i]);
        }
        for(int y=0; y<R_LINE;   y){
            printf("%.*s\n", R_COLN, smoke[smoke_set][y]);
        }
        smoke_set=(smoke_set 1)%R_SET; // Advance to the next set
                                 // (or go back to the first one).
        fflush(stdout); // Draw the current frame on the screen.
        usleep(DELAY);  // Pause to be visible.
    }
    for (shifControl = 0; shifControl < 28;   shifControl){
        // Rocket move on the basis of delay
        usleep(DELAY);
        // move rocket a line upward
        printf("\n");
    }
    return 0;
}

CodePudding user response:

Currently your logic is:

  1. Draw one frame.
  2. Change smoke set.
  3. Repeat 1-2 for 29 frames.
  4. Draw line to push frame up.
  5. Repeat 4 to keep pushing frames up.

From that it is obvious the smoke will stop changing at step 4. So the logic needs to include the take off elevation in step 1. The easiest way to do that is to put the draw frame into a function and add the elevation as a parameter.

Here is an example:

void draw_frame(int elevation)
{
    int jumpControlAtBottom = 0;
    static int smoke_set = 0;

    for (jumpControlAtBottom = 0; jumpControlAtBottom < 28   elevation;   jumpControlAtBottom){
            // Jump to bottom of console
            printf("\n");
        }
        for(int i = 0; i< LINE; i  ){
            printf("%.*s\n", COLN, rocket[i]);
        }
        for(int y=0; y<R_LINE;   y){
            printf("%.*s\n", R_COLN, smoke[smoke_set][y]);
        }
        smoke_set=(smoke_set 1)%R_SET; // Advance to the next set
                                // (or go back to the first one).

        // Push image up by elevation
        for (int ix = 0; ix < elevation; ix  ) {
            printf("\n");
        }

        fflush(stdout); // Draw the current frame on the screen.
        usleep(DELAY);  // Pause to be visible.
}

int main(){
    int shifControl = 0;

    // No elevation - engine starting up
    for(int h = 0; h < 29; h  ){ //frame
        draw_frame(0);
    }

    // take off has occured
    for (shifControl = 0; shifControl < 28;   shifControl){
        // Rocket move on the basis of delay
        // move rocket a line upward
        draw_frame(shifControl);
    }
    return 0;
}

CodePudding user response:

It seems to me like you're struggling to understand the procedural nature of C, which probably means you're guessing and playing around with code from other (probably poor) examples.

This is akin to guessing how to prepare a chicken for cooking and then asking your guests how to prepare a chicken for cooking, after the chicken is cooked. You have an application that does most of what you want. How did you come up with this code, yet not know how to apply such a simple tweak?

If you don't understand the procedural nature of C, it stands to reason that any exercise that has you read the procedural nature of C in order to extract some logic and repeat it is terrible for you. Where is your book? Start with the basics.

To be clear, this answer may not seem relevant, but it does actually answer your question: you need to hoist some of your smoke-related logic out into a different function so that you can reuse it later... but before you do that, you need to be able to read C, and we're not here to do your work for you, rather to guide you in the right direction.

  • Related