BALL_0 A c++ Game

edited September 2014 in C++ Game Development

//CREATED BY DAVID CHRISTIE
//BALL-O v1.0....A part of PROJECT CONNECTO

#include<iostream.h>
#include<graphics.h>
#include<conio.h>
#include<stdlib.h>
#include<dos.h>

int score=0;
char scor[10];

void screen()
{
 setbkcolor(LIGHTBLUE);
 setfillstyle(9,YELLOW);
 setcolor(YELLOW);
 line(500,0,500,480);
 floodfill(550,20,YELLOW);
 setcolor(GREEN);
 line(0,50,640,50);
 setfillstyle(9,GREEN);
 floodfill(25,25,GREEN);
 settextstyle(3,0,6);
 setcolor(RED);
 outtextxy(200,-2,"BALL-O");
 setcolor(RED);
 settextstyle(3,0,4);
 outtextxy(520,60,"Life");
 setfillstyle(1,YELLOW);
 bar(520,100,580,110);
 setcolor(RED);
 outtextxy(520,150,"SCORE");
 setfillstyle(1,YELLOW);
 bar(520,200,600,250);
 settextstyle(3,0,2);
 outtextxy(520,380,"CREATED BY");
 setcolor(YELLOW);
 bar(520,420,600,450);
 setcolor(RED);
 settextstyle(4,0,4);
 outtextxy(520,420,"WOLFPACK");

}

void instructions()
{
 setbkcolor(RED);
 setcolor(YELLOW);
 settextstyle(6,0,6);
 outtextxy(200,20,"INSTRUCTIONS");
 settextstyle(1,0,3);
 outtextxy(20,100,"1.Don't get hit by the lasers");
 outtextxy(20,200,"2.Eat as much cakes as you can");
 outtextxy(20,300,"3.Press space to exit.....AND HAVE FUN");
 outtextxy(20,400,"4.Use W,A,S,D to move");
 outtextxy(100,450,"PRESS ANY KEY TO PLAY");
 getche();
 cleardevice();
}

void gameover()
{
 setcolor(RED);
 settextstyle(4,0,6);
 outtextxy(250,200,"Game Over");
 delay(2000);
 cleardevice();
 setbkcolor(YELLOW);
 settextstyle(5,0,6);
 outtextxy(100,40,"Thank you dor playing");
 outtextxy(100,100,"You have scored:");
 outtextxy(100,175,scor);
 outtextxy(100,250,"Press any key....");
 getch();
 getch();
}

void main()
{
 char key;
 int speed=1,life=3,movementspeed=5;
 randomize();
 clrscr();
 unsigned int size;
 int gdriver=DETECT,gmode;
 initgraph(&gdriver,&gmode,"c:\\tc\\BGi\\");

 //HEART FOR LIFE
 setbkcolor(LIGHTBLUE);
 cout<<(char)3;
 void *heart;
 size=imagesize(0,0,12,12);
 heart=malloc(size);
 getimage(0,0,12,12,heart);
 cleardevice();

 //ENEMY
 setfillstyle(1,RED);
 setcolor(YELLOW);
 line(2.5,0,2.5,40);
 bar(0,0,5,40);
 struct
 {
  void *enemy;
  int x,y;
 }enemy[3];
 size=imagesize(0,0,5,40);
 for(int i=0;i<3;i++)
 {
  enemy[i].enemy=malloc(size);
  getimage(0,0,10,40,enemy[i].enemy);
 }

 //now for PLAYER
 cleardevice();
 setbkcolor(LIGHTBLUE);
 setcolor(YELLOW);
 circle(20,20,20);
 setfillstyle(1,YELLOW);
 floodfill(20,20,YELLOW);
 struct
 {
  void *player;
  int x,y;
 }player;
 size=imagesize(0,0,40,40);
 player.player=malloc(size);
 getimage(0,0,40,40,player.player);
 cleardevice();

 //Food
 struct
 {
  void *cake;
  int x,y;
 }cake;
 {
  int T[6]={0,0,0,30,30,30};
  setfillstyle(2,YELLOW);
  fillpoly(3,T);
 }
 size=imagesize(0,0,30,30);
 cake.cake=malloc(size);
 getimage(0,0,30,30,cake.cake);
 cleardevice();

 //The Screen
 instructions();
 screen();

 //beauty
 for(i=0;i<5000;i++)
 {
  putpixel(random(500),random(480)+50,i%16);
  delay(1);
 }

 //The initial coordinates
 cake.x=random(420);
 cake.y=random(410)+50;
 player.x=250;
 player.y=230;
 for(i=0;i<3;i++)
 {
  enemy[i].x=random(480);
  enemy[i].y=80;
  for(int j=0;j<i;j++)
  {
   if(abs(enemy[i].x-enemy[j].x)<50)
    i--;
  }
 }
 //THE Mechanism
 key='o';
 int temp=520;
 putimage(cake.x,cake.y,cake.cake,XOR_PUT);
 while(key!=' ')
 {
  //lives
  temp=520;

  for(i=0;i<life;i++)
  {
   putimage(temp,100,heart,COPY_PUT);
   temp+=20;
  }
  //score;
  settextstyle(4,0,4);
  itoa(score,scor,10);
  setcolor(RED);
  outtextxy(530,200,scor);
  putimage(player.x,player.y,player.player,OR_PUT);
  for(i=0;i<3;i++)
   putimage(enemy[i].x,enemy[i].y,enemy[i].enemy,XOR_PUT);
  // bad
  int loopcanceller=0;
  for(i=0;i<3;i++)
   {
    if(abs(enemy[i].x-player.x)<20&&abs(enemy[i].y-player.y)<40)
    {
     if(life==0)
      {
       gameover();
       return;
      }
    setcolor(RED);
    settextstyle(3,0,2);
    if(life!=1)
     outtextxy(520,295,"LIFE LOST");
    else
     outtextxy(520,295,"LAST LIFE");
    setfillstyle(1,YELLOW);
    bar(520,100,580,110);
    delay(1000);
    setcolor(LIGHTBLUE);
    if(life!=1)
     outtextxy(520,295,"LIFE LOST");
    else
     outtextxy(520,295,"LAST LIFE");
    life--;
    loopcanceller=1;
   }
  }
  if(loopcanceller==1)
  {
   //DELETING
   putimage(player.x,player.y,player.player,XOR_PUT);
   player.x=250;
   player.y=220;
   loopcanceller=0;
   i--;
   for(int j=0;j<3;j++)
   putimage(enemy[j].x,enemy[j].y,enemy[j].enemy,XOR_PUT);
   enemy[i].x=random(480);
   enemy[i].y=80;
   continue;
  }
  //good
  setfillstyle(1,LIGHTBLUE);
  bar(520,280,640,320);
  setfillstyle(1,YELLOW);
  if(abs(player.x-cake.x)<30&&abs(player.y-cake.y)<30)
  {
   settextstyle(4,0,4);
   setcolor(LIGHTBLUE);
   outtextxy(530,200,scor);
   score+=10;
   putimage(cake.x,cake.y,cake.cake,XOR_PUT);
   cake.x=random(420);
   cake.y=random(400)+50;
   putimage(cake.x,cake.y,cake.cake,OR_PUT);
   setcolor(YELLOW);
   bar(520,200,600,250);
  }
  //player movement
  if(kbhit())
  {
   key=getche();
   putimage(player.x,player.y,player.player,XOR_PUT);//DELETING
   switch(key)
   {
    case 'a':
    case 'A':
    if(player.x>10);
     {
      player.x-=movementspeed;
      break;
     }

    case 'd':
    case 'D':
    if(player.x<460)
     {player.x+=movementspeed;
      break;
     }

    case 'w':
    case 'W':
    if(player.y>80)
     {
      player.y-=movementspeed;
      break;
     }

    case 's':
    case 'S':
    if(player.y<450)
     player.y+=movementspeed;
    break;
   }
  }
  //Enemy movement
  for(i=0;i<3;i++)
  {
   putimage(enemy[i].x,enemy[i].y,enemy[i].enemy,XOR_PUT);//DELETING
   enemy[i].y+=movementspeed*0.15*(random(3)+1);
   if(enemy[i].y>460)
   {
    enemy[i].y=0;
    enemy[i].x=random(420);
   }
  }
 delay(speed);
 }
}

Comments

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion