2014-12-23 24 views
0

我使用graphics.h爲一個小程序,爲教育目的而寫。它有一個環繞地球的月球。問題是,經過幾次迭代,整個屏幕變爲空白(白色)。我嘗試了很多方法,但無法找到問題所在。請查看下面的代碼並查看是否可以找出問題所在?C++圖形使用graphics.h

#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(); 
} 

謝謝你的幫忙!

+0

這是Turbo-C++嗎? – Borgleader

+0

不是。使用的編譯器是GCC mingw。 – KhuramAli

+1

月球輪廓是否真的閉合? –

回答

0

我不熟悉你使用的庫,所以我找不到錯誤,但有一些東西看起來不太好。

int Moon_x,Moon_y; //他們應該是浮動的(因爲你指定了三角函數的結果)

new_page = abs(old_page-0); // - >這應該是-1嗎?作爲一般的調試技巧,我會建議嘗試從場景中移除東西(例如,移除月球,然後移除地球等),並查看是否得到相同的結果。通過這種方式,您可以檢測導致問題的原因。

0

代碼似乎沒有任何錯誤。我認爲你不是以正確的方式編譯代碼。如果您使用的是Code :: Blocks,則需要先在您的編譯器庫中包含graphics.h頭文件。這tutorial應該幫助!