Ncurses- zmiana aktualnego stanu ekranu

0
#include <ncurses.h>
#include <stdlib.h>

struct coordinates{
    int x;
    int y;
};
void animation(struct coordinates monkey, struct coordinates banana);
void draw(struct coordinates monkey, struct coordinates banana);
void clear_win(struct coordinates monkey, struct coordinates banana);
void find_new(struct coordinates monkey, struct coordinates banana);

int main(){
    
    srand(time(NULL));
    
    int number1, number2;
    
    FILE *c_File = fopen("coordinates.c", "r");
    if(c_File == NULL){
        printf("File does not exist");
        return 1;
    }
    fscanf(c_File, "%d%d", &number1, &number2);
    
    struct coordinates banana = {number1, number2};
    struct coordinates monkey = {banana.x + 1, banana.y + 1};

    initscr();
    
    curs_set(0);
    
    animation(monkey, banana);
    
    fclose(c_File);
    endwin();
    
    return 0;
}
    
void animation(struct coordinates monkey, struct coordinates banana){ 
    
    int step, max_steps = 20;
    
    for(step = 1; step < max_steps; step++){  //tutaj się coś sypie
        draw(monkey, banana);
        refresh();
        system("sleep 0.5");
        clear_win(monkey, banana);
        refresh();
    }
}
    

void draw(struct coordinates monkey, struct coordinates banana){
    mvprintw(banana.y, banana.x, "]");
    mvprintw(monkey.y, monkey.x, "@");
}

void clear_win(struct coordinates monkey, struct coordinates banana){
    mvprintw(banana.y, banana.x, " ");
    mvprintw(monkey.y, monkey.x, " ");
}

void find_new(struct coordinates monkey, struct coordinates banana){
    int random = rand()%3-1;
    
    banana.x += 5;
    banana.y -= 5;
    monkey.x = banana.x + random;
    monkey.y = banana.y - random;
}

Mam następujący problem. Gdzie bym nie wrzucił funkcji find_new**(dlatego też aktualnie nie jest nigdzie umieszczona!!)**, nie są wyliczane nowe współrzędne do funkcji draw. Drukowanie na ekranie jest tylko dla wartości podstawowych i tam są przez 20 "step'ów". Wczytywane wartości z pliku to 0 oraz 0.

0

do find new powinienieś przekazać parametry przez wskaźnik. Teraz przekazywana jest kopia dlatego zmiany nie są widoczne.

0
void animation(struct coordinates monkey, struct coordinates banana){ 
 
    int step, max_steps = 20;
 
    for(step = 1; step < max_steps; step++){  //tutaj się coś sypie
        draw(monkey, banana);
        refresh();
        system("sleep 0.5");
        clear_win(monkey, banana);
        refresh();
        find_new(&monkey, &banana);
    }
}
void find_new(struct coordinates *monkey, struct coordinates *banana){
    int random = rand()%3-1;
 
    banana += 5;
    banana -= 5;
    monkey = banana + random;
    monkey = banana - random;
} 

Jeżeli chodzi o to, to dalej znaczki stoją w miejscu. Chyba, że nie tylko do find_new muszę przekazać wskaźnik?

0

Prawie dobrze, tylko dlaczego w funkcji find_new przestałeś zmieniać koordynaty x i y banana i małpy, a zacząłeś przsuwać i cofać o tą samą wartość (5 i random) wskaźniki na nie?

banana += 5;
banana -= 5;
monkey = banana + random;
monkey = banana - random; 

Zmieniaj wartości spod wskaźnika:

banana->x += 5;
banana->y -= 5;
//  itd... 

a zacznie się wszystko przesuwać.

1 użytkowników online, w tym zalogowanych: 0, gości: 1