Question

I recently discovered winBGIm libraries, and I find it very useful to learn to code, so I started creating something to get some practice but I got stuck with my program.

The program should show a little ball and two rectangles approaching to the ball itself, and the player can move the ball up and down simply by pressing a button on the keyboard. Initially, I wrote everything in the main to make it fast, but since this is terrible I divided the program into functions.

First of all, even when everything was together, the getch function seemed to not work, because, while it should wipe the input buffer for the kbhit function, it made the ball not move at all, while the mere kbhit function worked, but obvioulsy the ball continued going up even when you stopped pressing a key. I used the same procedure in another text-only program and it worked very well, so I don't know where the problem is.

The second and most important problem is that, after splitting the program into functions, it became static, since the main loop which would make the graphic move stops at the end of the first iteration. It only restarts working when I delete the cleardevice function at the end of the loop and I disable the duble buffering in the initwindow function, but I can't understand the relationship between these things.

Finally, when I set a new background color, if it is not 0 (black), the window remains completely black.

I hope someone can help me.

Best regards, Giacomo.

#include <graphics.h>
#include <time.h>
#include <stdio.h>

int rettangoli(int b);
void bird(int x_default, int y_default);

void bird();

int main() {
    int a=640;
    int b=480;
    int x_default=150;
    int y_default=400;
    int verifica=0;
    srand(time(NULL));
    initwindow(a, b, "BGI", 0, 0, true, true);
    setbkcolor(0);
    while(1) {
        bird(x_default, y_default);
        verifica=rettangoli(b);
        if(verifica==1) {
            outtextxy(0, 0, "HAI PERSO");
            outtextxy(0,20, "PREMERE UN TASTO PER CONTINUARE");
            break;
        }
        delay(50);
        while(kbhit()) {
            getch();
        }
        swapbuffers();
        cleardevice();
    }
    delay(350);
    getch();
    closegraph();
    return 0;
}

void bird(int x_default, int y_default) {
    static int x_pos=x_default;
    static int y_pos=y_default;
    if(kbhit()) {
        if(y_pos-16>0)
            y_pos=y_pos-5;
        circle(x_pos, y_pos, 16);
        setfillstyle(SOLID_FILL, YELLOW);
        floodfill(x_pos, y_pos, 15);
    }
    else {
        while(y_pos<400)
            y_pos=y_pos+15;
        circle(x_pos, y_pos, 16);
        setfillstyle(SOLID_FILL, YELLOW);
        floodfill(x_pos, y_pos, 15);
    }
    return;
}

int rettangoli(int b) {
    static int x_rett=615;
    int verifica=0;
    int contatore;
    int altezza;
    if(x_rett==615)
        altezza=rand()%(b-60)+1;
    rectangle(x_rett, 0, x_rett+25, altezza);
    rectangle(x_rett, altezza+60, x_rett+25, 480);
    setfillstyle(SOLID_FILL, GREEN);
    floodfill(x_rett+1, 1, 15);
    floodfill(x_rett+1, 479, 15);
    for(contatore=0;contatore<altezza && x_rett>100;contatore++) {
        if(getpixel(x_rett-1, contatore)!=getbkcolor())
            verifica=1;
    }
    for(contatore=altezza+60;contatore<b && x_rett>100;contatore++) {
        if(getpixel(x_rett-1, contatore)!=getbkcolor())
            verifica=1;
    }
    for(contatore=0;contatore<15;contatore++) {
        if(getpixel(contatore, altezza)!=getbkcolor())
            verifica=1;
    }
    for(contatore=0;contatore<15;contatore++) {
        if(getpixel(contatore, altezza+60)!=getbkcolor())
            verifica=1;
    }
    x_rett=x_rett-5;
    printf("%i", x_rett);
    return verifica;
}

Was it helpful?

Solution

Nevermind, I solved the problem on my own. There was an incompatibility issue between Windows 8 x64 and Codeblocks-EP, and I think there is a general incompatibility between winBGIm and x64 systems.

I created a virtual machine using Vmware Player and Windows XP x86 and now everything is fine.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top