简体   繁体   English

使用graphics.h的C ++图形

[英]C++ graphics using graphics.h

I am using graphics.h for a small program, written for educational purpose. 我正在将graphics.h用于一个用于教育目的的小程序。 It has a moon orbiting around earth. 它有绕地球旋转的月亮。 the issue is, after a few iteration, whole screen went blank(white). 问题是,经过几次迭代后,整个屏幕变成空白(白色)。 I have tried many alternatives but could not able to find the problem. 我尝试了许多替代方法,但是找不到问题。 Please review below code and see if you can find out the issue? 请查看下面的代码,看看是否可以找到问题所在?

#include <stdio.h>
#include <conio.h>
#include <graphics.h>
#include <math.h>

int main()
{
int gd,gm;

detectgraph(&gd,&gm);
initgraph(&gd,&gm,"c:\\tc\\bgi");

int Earth_x,Earth_y;
Earth_x=10+390/2;
Earth_y=60+340/2;

int Moon_x,Moon_y;
Moon_x=Earth_x+100;         //Moon initial coordinates
Moon_y=Earth_y;
float t = 0;

int new_page, old_page; // declare integer variables representing two graphics pages

while(1)
        {  
            old_page = getvisualpage( ); // set old_page to the number of the visual page
            new_page = abs(old_page-0); // set new_page to the visual page number-1
            setactivepage(new_page); // set the active page to the value of the new page
            cleardevice( ); // erase the active page

            //rectangle(x1,y1,x2,y2);
            rectangle(10,60,400,400);

            //code for drawing and filing Earth.
            setcolor(GREEN);
            setfillstyle(1,GREEN);
            circle(Earth_x,Earth_y,30);
            floodfill(Earth_x,Earth_y,GREEN);
            setcolor(WHITE);
            outtextxy(Earth_x, Earth_y, "Earth");

            //code for drawing and filling Moon.
            setfillstyle(1,WHITE);
            circle(Moon_x,Moon_y,10);
            floodfill(Moon_x,Moon_y,WHITE);

            //****We can add delay to slow down the moon*** 
            //delay(1);    

             setvisualpage(new_page); // move the activepage to the visual page

            //Code for modification of Moon coordinates
             Moon_x=Earth_x+100*cos(t*3.1415/180.0);
             Moon_y=Earth_y+100*sin(t*3.1415/180.0);
             t=t+1;
        }

getch();
closegraph();
}

Thank you for your help! 谢谢您的帮助!

I am not familiar with the library you are using so I cannot find the error, but there are a few things that don't look all right. 我不熟悉您正在使用的库,因此找不到错误,但是有些事情看起来并不正常。

int Moon_x,Moon_y; int Moon_x,Moon_y; // they should be floats (since you assign the result of a trigonometric function) //它们应该是浮点数(因为您分配了三角函数的结果)

new_page = abs(old_page-0); new_page = abs(old_page-0); // -> should this be -1 ??? //->应该为-1 ???

As a general debugging tip, I would recommend try removing things from the scene (eg remove the moon, then the earth, and so on) and see if you get the same results. 作为一般的调试技巧,我建议尝试从场景中移除事物(例如,移除月亮,然后移除地球,等等),看看是否得到相同的结果。 This way you can detect what causes the problem. 这样,您可以检测到导致问题的原因。

There doesn't seem to be any error with the code. 该代码似乎没有任何错误。 I think you're not compiling the code the right way. 我认为您没有以正确的方式编译代码。 If you're using Code::Blocks, you'll need to first include graphics.h header file in your compiler library. 如果您使用的是Code :: Blocks,则需要首先在编译器库中包含graphics.h头文件。 This tutorial should help! 教程应该有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM