Friday, 21 September 2012

Draw image in c++ using Computer Graphics

Draw the image  in c++ using Computer Graphics



#include "newgfx.cpp"
#include <iostream.h>
#include <iostream.h>

bitimage bas= {{-1,-1,-1,2,2,2,2,-1,-1,-1},
     {-1,-1,2,2,2,2,2,2,-1,-1},
     {-1,2,2,2,2,2,2,2,2,-1},
     {2,2,2,2,2,2,2,2,2,2},
     {2,2,2,2,2,2,2,2,2,2},
     {2,2,2,2,2,2,2,2,2,2},
     {2,2,2,2,2,2,2,2,2,2},
     {2,2,2,2,2,2,2,2,2,2},
     {-1,2,2,2,2,2,2,2,2,-1},
     {-1,-1,2,2,2,2,2,2,-1,-1},
     {-1,-1,-1,2,2,2,2,-1,-1,-1}},
   temp={0},
   ship={{-1,-1,-1,-1,25,-1,-1,-1,-1},
     {-1,-1,-1,25,25,25,-1,-1,-1},
     {-1,-1,25,25,105,25,25,-1,-1},
     {-1,25,25,105,105,105,25,25,-1},
     {25,25,25,25,25,25,25,25,25},
     {-1,-1,-1,25,41,25,-1,-1,-1},
     {-1,-1,112,41,41,41,112,-1,-1},
     {-1,-1,-1,112,41,112,-1,-1,-1},
     {-1,-1,-1,-1,112,-1,-1,-1,-1},
     {-1,-1,-1,-1,-1,-1,-1,-1,-1},
     },
   red={{-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
      {-1,-1,-1,-1,112,112,-1,-1,-1,-1},
      {-1,-1,112,112,41,41,112,112,-1,-1},
      {-1,112,41,41,105,105,41,41,112,-1},
      {-1,112,41,105,0,0,105,41,112,-1},
      {-1,112,41,41,105,105,41,41,112,-1},
      {-1,-1,112,41,177,177,41,112,-1,-1},
      {-1,-1,112,41,41,41,41,112,-1,-1},
      {-1,-1,-1,112,112,112,112,-1,-1,-1},
      {-1,-1,105,105,105,105,105,105,-1,-1}},
   arrow={{-1,-1,-1,-1,150,-1,-1,-1,-1},
      {-1,-1,-1,150,150,150,-1,-1,-1},
      {-1,-1,140,140,140,140,140,-1,-1},
      {-1,41,41,41,41,41,41,41,-1},
      {-1,140,41,140,41,140,41,140,-1},
      {-1,-1,140,126,140,126,140,-1,-1},
      {-1,-1,-1,140,126,140,-1,-1,-1},
      {-1,-1,-1,-1,140,-1,-1,-1,-1},
      {-1,-1,-1,-1,-1,-1,-1,-1,-1}},
   kalien={{-1,-1,-1,-1,43,-1,-1,-1,-1,-1,},
       {-1,-1,-1,46,4,46,-1,-1,-1,-1},
       {-1,-1,46,27,42,27,46,-1,-1,-1},
       {-1,46,4,56,105,56,4,46,-1,-1},
       {-1,46,4,105,105,105,4,46,-1,-1},
       {-1,-1,46,27,42,27,46,-1,-1,-1},
       {-1,-1,-1,46,4,46,-1,-1,-1,-1},
       {-1,-1,2,-1,2,-1,2,-1,-1,-1},
       {-1,2,-1,2,-1,2,-1,2,-1,-1}},
   square={{-1,-1,-1,140,140,140,140,140,-1,-1},
       {-1,-1,140,120,120,120,120,120,140,-1},
       {-1,-1,140,120,120,140,120,120,140,-1},
       {-1,-1,140,140,140,1,140,140,140,-1},
       {-1,-1,140,120,120,140,120,120,140,-1},
       {-1,-1,140,120,120,120,120,120,140,-1},
       {-1,-1,140,130,130,120,130,130,140,-1},
       {-1,-1,140,120,120,120,120,120,140,-1},
       {-1,-1,-1,140,140,140,140,140,-1,-1}};


struct coord {
int x;
int y;
unsigned char color;
};
struct coord2
{
int x;
int y;
unsigned int on;
};
class alien
{  public :
 int x;
 int y;
 int recx;
 int recy;
 int hitpoints;
 int alienp;
 unsigned int down;
 coord2 fire;
 void drawalien(int change=1);
};
class boss
{
 public :
 int x;
 int y;
 int hitpoints;
 int alienp;
 int rounds; //amount of bullets
 coord2 fire[60]; // ship weapon array
 void drawalien(int change=1);
};

void die();
void explode(int x, int y);

void main()
{
 randomize();
 int rowa=13; //rows of aliens
 int cola= 5; //columns of aliens
 int score=0, level=0; // 0 is 1 and so on
 alien enemy[15][11]={0}; // alien array
 boss ship; // your ship
  ship.rounds=6;
  ship.x=30;
  ship.y=MaxY-13;
  ship.alienp=9;
  ship.hitpoints=10;
  ship.drawalien(); // end of ship
 char tmp; // input from keyboard
 startgfx();
//this is where you put all the new levels
end:
 level++;
 cleargfx(0);
 gotoxy(18, 11);
 cout << "Level " << level;
 delay(2000);
 cleargfx(0);
 for(int x=0; x<15; x++)
  for(int y=0; y<11; y++)
  {
   enemy[x][y].down=0;
   enemy[x][y].fire.on = 0;
  }
if(level==1)
 {
 rowa=11;
 cola= 8;
 for(x=0; x<rowa; x++)
  for( y=0; y<cola; y++)
  {
   enemy[x][y].x = x*12;
   enemy[x][y].y = y*13;// +80;
   enemy[x][y].recy = y*13;
   enemy[x][y].alienp = ((y%2)&&(x%2))?2:3;
   enemy[x][y].hitpoints = ((y%2)&&(x%2))?3:2;
   enemy[x][y].drawalien();
  }
 }
else if(level==2)
 {
 ship.hitpoints++;
 score+=50;
 ship.rounds+=2;
 rowa=10;
 cola= 7;
 for(int x=0; x<rowa; x++)
  for(int y=0; y<cola-1; y++)
    {
   enemy[x][y].x = x*12;
   enemy[x][y].y = y*13;
   enemy[x][y].alienp = 4;
   enemy[x][y].hitpoints = 4;
   enemy[x][y].drawalien();
   }
 }
else if(level==3)
 {
 ship.hitpoints+=2;
 score+=50;
 ship.rounds+=2;
 rowa=10;
 cola= 5;
 for(int x=0; x<rowa; x++)
  for(int y=0; y<cola-1; y++)
    {
   enemy[x][y].x = x*12;
   enemy[x][y].y = y*13;
   enemy[x][y].alienp = 5;
   enemy[x][y].hitpoints = 7;
   enemy[x][y].drawalien();
   }
 }
else if(level==4)
{
 ship.hitpoints+=2;
 score+=50;
 ship.rounds+=2;
 rowa=13;
 cola= 7;
 for(int x=0; x<rowa; x++)
  for(int y=0; y<cola-1; y++)
    {
   enemy[x][y].x = x*12;
   enemy[x][y].y = y*13;
   enemy[x][y].alienp = ((y%2)&&(x%3))?2:(x%2)?3:4;
   enemy[x][y].hitpoints = ((y%2)&&(x%3))?3:(x%2)?4:5;
   enemy[x][y].drawalien();
   }
}

 for(x=0; x<ship.rounds; x++)
  ship.fire[x].on = 0;
 /*
 lr is left and right
 mxleft is the maximum left point
 mxright, mxy, mny are all basically the same
 */
 int lr=1, mxleft=MaxX, mxright=0, mxy=0, mny=MaxY;
 int rcount=0;
 ship.drawalien();
 start:
 while(!kbhit())
 {
 //gets the max x for the alien array and the min x
 //also does damage to alien if laser hits them
  for(x=0; x<rowa; x++)
   for(int y=0; y<cola; y++)
   {
   if(enemy[x][y].fire.on == 0 && enemy[x][y].hitpoints>0)
   {
    if(!(rand()%150)) //this is where the enemy fires
    {
     enemy[x][y].fire.x = enemy[x][y].x+5;
     enemy[x][y].fire.y = enemy[x][y].y+11;
     enemy[x][y].fire.on = 1;
    } // end of fire
    if(!(rand()%3000) && enemy[x][y].down==0)
    {
     enemy[x][y].down = 1;
     enemy[x][y].recy = enemy[x][y].y;
     enemy[x][y].recx = enemy[x][y].x;
    }
   }
   if(!enemy[x][y].down)
   {
    if(enemy[x][y].hitpoints > 0)
     mxleft = mxleft<enemy[x][y].x?mxleft:enemy[x][y].x;
    if(enemy[x][y].hitpoints > 0)
     mxright = mxright>enemy[x][y].x?mxright:enemy[x][y].x;
    if(enemy[x][y].hitpoints > 0)
     mxy = mxy>enemy[x][y].y?mxy:enemy[x][y].y;
    if(enemy[x][y].hitpoints > 0)
     mny = mny<enemy[x][y].y?mxy:enemy[x][y].y;
   }
   enemy[x][y].drawalien(2);
   //this is where your guy's bullets hit the aliens
   //if your shots in the right place
   for(int real=0; real<ship.rounds; real++)
    if(ship.fire[real].on == 1)
     if(ship.fire[real].x >= enemy[x][y].x
      && ship.fire[real].x <= enemy[x][y].x+12
      && ship.fire[real].y >= enemy[x][y].y
      && ship.fire[real].y <= enemy[x][y].y+12
      && enemy[x][y].hitpoints>0)
      {
       enemy[x][y].hitpoints--;
       if(enemy[x][y].hitpoints<1)
       {
        explode(enemy[x][y].x, enemy[x][y].y);
        delay(20);
        enemy[x][y].drawalien(2);
        score++;
        if(enemy[x][y].fire.on == 1)
         {
         pixel(enemy[x][y].fire.x, enemy[x][y].fire.y, 0);
         enemy[x][y].fire.on = 0;
         }
       }
       pixel(ship.fire[real].x, ship.fire[real].y, 0);
       ship.fire[real].on = 0;
       rcount--;
      }
     else pixel(ship.fire[real].x, ship.fire[real].y, YELLOW);
     //end of bullet test
   }
 // end of max, min function
 //start of bullet function for ship, moves bullet
 if(rcount>0)
  {
   for(int real=0; real<ship.rounds; real++)
   {
    if(ship.fire[real].on == 1)
    {
     if(ship.fire[real].y>0)
     {
      pixel(ship.fire[real].x, ship.fire[real].y, 0);
      ship.fire[real].y-=2;
      pixel(ship.fire[real].x, ship.fire[real].y, YELLOW);
     }
     else
     {
      pixel(ship.fire[real].x, ship.fire[real].y, 0);
      ship.fire[real].on = 0;
      rcount--;
     }
    }
   }
  }
 //end of bullet function
 //this is where the aliens move left and right and down
 if(lr==1 && (mxright < MaxX-13))
  for(x=0; x<rowa; x++)
   for(y=0; y<cola; y++)
    if(!enemy[x][y].down)
     enemy[x][y].x++;
    else
    {
     if(enemy[x][y].y<MaxY-13)
     {
      enemy[x][y].y++;
      enemy[x][y].recx++;
     }
     else if(enemy[x][y].recx+11>mxright)
     {
      enemy[x][y].recx++;
      enemy[x][y].y = 0;
     }
     else
     {
      enemy[x][y].x = enemy[x][y].recx+1;
      enemy[x][y].y = enemy[x][y].recy;
      enemy[x][y].down = 0;
     }
    }
 else if(lr == 0 && (mxleft >=1))
  for(x=0; x<rowa; x++)
   for(y=0; y<cola; y++)
    if(!enemy[x][y].down)
     enemy[x][y].x--;
    else
    {
     if(enemy[x][y].y<MaxY-13)
     {
      enemy[x][y].y++;
      enemy[x][y].recx--;
     }
     else if(enemy[x][y].recx<mxleft)
     {
      enemy[x][y].recx--;
      enemy[x][y].y = 0;
     }
     else
     {
      enemy[x][y].x = enemy[x][y].recx-1;
      enemy[x][y].y = enemy[x][y].recy;
      enemy[x][y].down = 0;
     }
    }
 else
 {
  lr = !lr;
//  lr = (lr)?0:1; // reverse lr
  for(x=0; x<rowa; x++)
   for(y=0; y<cola; y++)
   {
   if(enemy[x][y].hitpoints>0)
    if(mxy+20 < MaxY)
     if(enemy[x][y].down)
      enemy[x][y].recy+=10;
     else enemy[x][y].y+=10;
   }
 }
 //this is the end of the function where aliens move left and right and down
 //this function draws the aliens if there alive
 for(x=0; x<rowa; x++)
  for(y=0; y<cola; y++)
  {
   //this tests the range of numbers for the ship compared to the alien
   if(enemy[x][y].hitpoints>0
    &&
    (( enemy[x][y].x > ship.x
    && enemy[x][y].x < ship.x+11)
    || (enemy[x][y].x+11 > ship.x
    && enemy[x][y].x+11 < ship.x+11))
    &&
    (( enemy[x][y].y > ship.y
    && enemy[x][y].y < ship.y+11)
    || (enemy[x][y].y+11 > ship.y
    && enemy[x][y].y+11 < ship.y+11)))
   {
    sound(300);
    if(enemy[x][y].down)
    {
     enemy[x][y].x = enemy[x][y].recx;
     enemy[x][y].y = enemy[x][y].recy;
    }
    ship.hitpoints-= enemy[x][y].hitpoints;
    enemy[x][y].hitpoints=0;
    delay(20);
    nosound();
    if(ship.hitpoints<=0)
     die();
   }
   if(enemy[x][y].hitpoints>0)
    enemy[x][y].drawalien();
   if(enemy[x][y].fire.on == 1)
   {
    //this tests if the alien bullets hit your ship.
    if (enemy[x][y].fire.y < MaxY)
    {
     if(enemy[x][y].fire.x > ship.x
      && enemy[x][y].fire.x < ship.x+11
      && enemy[x][y].fire.y > ship.y
      && enemy[x][y].fire.y < ship.y+6)
     {
      sound(400);
      enemy[x][y].fire.on = 0;
      pixel(enemy[x][y].fire.x, enemy[x][y].fire.y, 0);
      ship.hitpoints--;
      delay(50);
      nosound();
      if(ship.hitpoints>0)
       ship.drawalien();
      else
      {
       // this is if you die from losing all you lives
       die();
      }
     } //end of test
     else //moves the bullet
     {
     pixel(enemy[x][y].fire.x, enemy[x][y].fire.y, 0);
     enemy[x][y].fire.y += (!enemy[x][y].down)?1:3;
     pixel(enemy[x][y].fire.x, enemy[x][y].fire.y, 41);
     }
    }
    else enemy[x][y].fire.on = 0;
   }
  }
 //if there are no more aliens goto the end which takes you to
 //the next level
 if(mxright==0 && mxy ==0 && mxleft == MaxX && mny == MaxY)
  goto end;
 //end of level move
 mxright = mxy = 0;
 mxleft = MaxX;
 mny=MaxY;
 gotoxy(1, 1);
 cout << "Life =" << ship.hitpoints << " Score =" << score;
 ship.drawalien();
 delay(20);
 }
 //end of while loop
 //start of keboard input.
 putpic(ship.x, MaxY-13, temp, 9, 12);
 tmp = getch();
 if(tmp == 0)
  tmp = getch();
 //if button is space bar fire
 if(tmp == 32)
 {
  if(rcount<ship.rounds)
  {
   for(int ggod=0; ggod<ship.rounds; ggod++)
   {
    if(!ship.fire[ggod].on)
    {
     sound(50);
     ship.fire[ggod].on = 1;
     ship.fire[ggod].x = ship.x+4;
     ship.fire[ggod].y = MaxY-15;
     rcount++;
     delay(20);
     nosound();
     goto start;
    }
   }
  }
 } //end of space bar
 else if(int(tmp) == 75 && ship.x > 0)  // left arrow
  ship.x-=5;
 else if(int(tmp) == 77 && ship.x < MaxX-13) // right arrow
  ship.x+=5;
 else if(int(tmp) == 27) // escape button
  exit(1);
 ship.drawalien();
 goto start;
 //the end which adds to the level
}
void explode(int x, int y)
{
int radius=6;
sound(200);
for(int count=radius; count>0; count--)
 {
  circle(x+5, y+5, count, RED);
  delay(.75);
 }
for(count=radius; count>0; count--)
 {
  circle(x+5, y+5, count, 0);
 }
nosound();
}

void boss::drawalien(int change)
{
if(change == 1 && hitpoints > 0)
{
 if(alienp == 9)
  putpic(x, y, ship, 9, 12);
}
else if (change > 1)
 putpic(x, y, temp, 11, 10);
}
void alien::drawalien(int change)
{
if(change == 1 && hitpoints > 0)
{
 if (alienp == 1)
  putpic(x, y, bas, 11, 10);
 else if (alienp == 2)
  putpic(x, y, red, 11, 10);
 else if (alienp == 3)
  putpic(x, y, arrow, 11, 10);
 else if (alienp == 4)
  putpic(x, y, kalien, 11, 10);
 else if (alienp == 5)
  putpic(x, y, square, 11, 10);
}
else if (change > 1)
 putpic(x, y, temp, 11, 10);
}

void die()
{
for(int die=0; die<MaxX; die++)
 {
 if(kbhit())
  exit(1);
 circle(MidX, MidY, die, 1);
 cout << "YOU DIED!";
 delay(5);
 }
getch();
exit(1);
}

No comments:

Post a Comment