Kulldox Inc. Weblog
Un blog despre tot ce ma interesează pe mine, mai mult sau mai puţin.

Primul meu program în Borland C++, DOS

Iată că acum câteva zile în urmă am ajuns la etapa acceptabilă (deajuns pentru a fi validată de către profesor :) ) a primului meu program în C++. Descrierea programului:

Problemă: A calcula traiectoria zborului proiectilului artileric propulsat din ţeava unui tun la un unghi “alpha” faţă de orizont şi cu o viteză V.
V = 60 m/s;
alpha = 35 grade;
t(iniţial) = 0,01 s;
delta t = 0,1 s;

Sarcina:
1. A inversa traiectoria proiectilului. (asta pentru că de obicei centrul sistemului de coordonate x=0 şi y=0 sunt în colţul stănga sus)
2. A desena proiectilulu zburând.
3. A desena tunul conform unghiului dat.
4. A desena deflagraţia la momentul impactului.
5. A desena un helicopter care apare aleator pe ecran.
6. În momentul impacului helicopterului cu proiectilul a desena deflagraţia, după care helicopterul cade şi iar se desenează deflagraţia.
7. De sonorizat zborul proiectilului şi căderea helicopterului.

Poftim codul programului [bullet.cpp]:

/*
*Programmed by KullDox
*site: http://kulldox.inc.md
*/
#include <stdio.h>
#include <conio.h>
#include <math.h>
#include <graphics.h>
#include <stdlib.h>
#include <iostream.h>
#include <dos.h>

// draw the blast
void blast(int xg, int yg, int color_in, int color_out){
	//start blast
	xg = xg + 10;
	setcolor(color_out);
	line(xg+5,yg,xg+10,yg-10);
	line(xg+10,yg-10,xg-5,yg-5);
	line(xg-5,yg-5,xg-10,yg-15);
	line(xg-10,yg-15,xg-15,yg-5);
	line(xg-15,yg-5,xg-30,yg-10);
	line(xg-30,yg-10,xg-20,yg);
	line(xg-20,yg,xg-30,yg+10);
	line(xg-30,yg+10,xg-15,yg+5);
	line(xg-15,yg+5,xg-10,yg+20);
	line(xg-10,yg+20,xg-5,yg+5);
	line(xg-5,yg+5,xg+10,yg+10);
	line(xg+10,yg+10,xg+5,yg);

	setfillstyle(1,color_in);
	floodfill(xg-10,yg-2,color_out);
    //	putpixel(xg+3,yg-5,YELLOW);
}//end blast

//draw helicopter
void helicopter (int x, int y, int color_in, int color_out){
	//start Helicopter
	setcolor(color_out);
	setfillstyle(1,color_in);
	fillellipse(x,y,10,7);
	line(x,y-7,x,y-10);
	line(x-10,y-10,x+10,y-10);
	line(x+10,y,x+17,y);
	line(x+20,y-5,x+15,y+5);
}//end helicopter

//check if position of x,y is inside of a virtual rectangle(hel_x_st, hel_y_st, hel_x_en, hel_y_en)
int check_position (int x, int y, int hel_x_st, int hel_y_st, int hel_x_en, int hel_y_en){
	//start check_position
	int result;
	if (((x >= hel_x_st) && (x <= hel_x_en)) && ((y >= hel_y_st) && (y <= hel_y_en))) {
		result = 1;
	} else {
		result = 0;
	}
	return(result);
}//end check_position

//***start draw bullet
void bullet(int x, int y, int fill_color, int color){
    setcolor(color);
    setfillstyle(1,fill_color);
    fillellipse(x,y,3,3);
}//***end draw bullet

//***start draw the monitor
void area(){
    setcolor(WHITE);
    rectangle(0,30,getmaxx(),getmaxy());
}//***end draw the monitor

void main()//Start main
   {
      //Initialize variables
      float V, Vx, Vy, alpha, t, x, y, xg, yg, yg1;
      int randx, randy, gunl, gunx, guny, end;

      //Start Graphics initialization;
      int gdriver = DETECT, gmode, errorcode;
      initgraph(&gdriver, &gmode, "D:\\BORLANDC\\BGI");
      errorcode = graphresult();
      if (errorcode != grOk) {
	  cout<<"Graphics error!!!"<<endl;
	  cout<<"Press any key to halt.."<<endl;
	  getch();
	  exit(1);
      }//End Graphics initialization;
      nosound();
      end=10;
      do {

      nosound();
      //Start generation of random coordinates of helicopter
      randomize();
      randx=rand()%630;
      //generate randy till it will be more than 40
      //to make shure the helicopter is drawn in the monitor
      do {
      randy=rand()%470;
      } while (randy<40);

      setcolor(WHITE);
      helicopter(randx, randy, BLUE, WHITE);
      area();//draw monitor

      printf("Tries:%d",end);
      printf("   Enter V and Alpha: ");
      scanf("%f %f", &V, α);
      printf("\n");
      alpha = 3.14*alpha/180;//transform grades to radians

//*********** start draw the gun
      gunl = 30;//gun length
      gunx = guny = 0;
      gunx = gunl*cos(alpha);//calculate x of the gun end
      guny = getmaxy() - gunl*sin(alpha);//calculate y of the gun end
      line(0,getmaxy(),gunx,guny);//draw gun
//*********** end draw gun

      Vx = V*cos(alpha);//calculate x speed
      Vy = V*sin(alpha);//calculate y speed
      t = 0.01;
      xg = yg= 0;
      area();//draw monitor
      do {
	    x = Vx*t;//calculate x
	    y = Vy*t-4.9*t*t;//calculate y
	    xg = x + gunx;//move x to the gun end
	    yg = guny - y;//move y to the gun end

		//if the bullet is inside the virtual rectangle around the helicopter
		if (check_position(xg,yg,randx-15,randy-10,randx+15,randy+10) == 1){
			blast(xg, yg, RED, YELLOW);//draw blast
			delay(30);
			blast(xg, yg, BLACK, BLACK);//ERASE: draw blast
			helicopter(randx, randy, BLACK, BLACK);
			int i;
			i = randy;
			while (i<getmaxy()) {
			i++;
			helicopter(randx, i, BLUE, WHITE);
			delay(5);
			helicopter(randx, i, BLACK, BLACK);
			sound(i-800);
			nosound();
			}
			blast(randx, getmaxy(), RED, YELLOW);//draw blast
			end--;
			break;//breake the cycle
		}
		//if the bullet is outside the visible monitor area
		if (check_position(xg,yg,0,30,getmaxx()-30,getmaxy()) == 0){
			blast(xg, yg+16, RED, YELLOW);//draw blast
			break;//breake the cycle
		}
	    bullet(xg,yg,RED,WHITE);//draw bullet
	    delay(3);//wait 3s
	    sound(yg);
            sound(xg);
	    bullet(xg,yg,BLACK,BLACK);//Erasing: draw black bullet
	    t+=0.01;
	 } while ((y+guny)>0);
	  area();//draw monitor
	  end--;
	  nosound();
	  getch();
	  //getch();
	  clrscr();
	  cleardevice();
      } while (end>0);
  closegraph();
}//end of main;

:) Dacă aveţi nevoie de file-ul executabil, scrieţ-imi şi vil trimit prin e-mail.

P.S. Am folosit [http://www.w3clubs.com/htmlentities.php] pentru înlocuirea siblolurilor speciale, pentru a putea arata codul programului în întregime.

3 Responses to “Primul meu program în Borland C++, DOS”

  1. interesant programelul
    da-mi add pe mess idu meu e gadjomatto , poate facem ceva impreuna

  2. Vreau eu exe email a_alex_1993@yahoo.com
    Nu am reusit sal fac sa mearga
    :( (

  3. Sal , dar ceva asemanato pt jocul de table sau bacggammon aveti .
    Sau stiti unde pot gase. :) Merci


Leave a Reply