#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
#include<graphics.h>
#include<iostream.h>
#include<dos.h>
//============================================//
class HCN;
class TUONG;
class BONG;
class THANH;
class PINBALL;
//===========================================//
class HCN
{
private:
int x,y,d,r,m;
public:
void taoHCN(int,int,int,int,int);
void ve();
void xoa();
int &getX();
int &getY();
int &getD();
int &getR();
};
class TUONG
{
private:
HCN a[50];
int n;
public:
void taotuong();
void ve();
void xoa();
HCN getTUONG(int k)
{
return a[k];
}
int &getN()
{
return n;
}
};
class BONG
{
private:
int x,y,r,m;
public:
void taobong(int,int,int,int);
void ve();
void xoa();
int &getX();
int &getY();
int &getR();
};
class THANH
{
private:
int x,y,d,r,m;
public:
void taothanh(int,int,int,int,int);
void ve();
void xoa();
int &getX();
int &getY();
int &getD();
int &getR();
};
class PINBALL
{
private:
TUONG tuong;
BONG bong;
THANH thanh;
public:
void khoitao();
void play();
int ktDobong();
int ktTruotbong();
};
//================================================//
//---------Cac ham trong class BONG----------------//
void BONG::taobong(int x1,int y1,int r1,int m1)
{
x=x1;
y=y1;
r=r1;
m=m1;
}
void BONG::ve()
{
setcolor(m);
circle(x,y,r);
circle(x,y,r+1);
}
void BONG::xoa()
{
setcolor(BLACK);
circle(x,y,r);
circle(x,y,r+1);
}
int &BONG::getX()
{
return x;
}
int &BONG::getY()
{
return y;
}
int &BONG::getR()
{
return r;
}
//----------------- Cac ham trong class HCN---------//
void HCN::taoHCN(int x1,int y1,int d1, int r1, int m2)
{
x=x1;
y=y1;
d=d1;
r=r1;
m=m2;
}
void HCN::ve()
{
setcolor(m);
rectangle(x,y,x+d,y+r);
}
void HCN::xoa()
{
setcolor(getbkcolor());
rectangle(x,y,x+d,y+r);
}
int &HCN::getX()
{
return x;
// return x2;
}
int &HCN::getY()
{
return y;
}
int &HCN::getD()
{
return d;
}
int &HCN::getR()
{
return r;
}
//----------------------------------------------//
void ktdh()
{
int gm=0,gd=0;
initgraph(&gm,&gd,"..\\BGI");
if(graphresult()!=0)
{
cout<<"Loi do hoa";getch();exit(1);
}
}
//------------------ tao khung---------------//
void khung(int maukhung)
{
setcolor(maukhung);
rectangle(0,0,getmaxx(),getmaxy());
line(getmaxx()-70,0, getmaxx()-70, getmaxx());
setcolor(13);
outtextxy(getmaxx()-50, 50,"DIEM");
outtextxy(getmaxx()-50, 150,"TRUOT");
setcolor(WHITE);
outtextxy(getmaxx()-60, 350,"PINBALL");
outtextxy(getmaxx()-50, 365,"GAME");
outtextxy(getmaxx()-50, 380,"10B3");
}
int diem=0,truot=0;
void sodiem()
{
char s[3];
setcolor(BLACK);
setfillstyle(SOLID_FILL, BLACK);
bar(getmaxx()-50, 60, getmaxx()-5,90);
setcolor(YELLOW);
outtextxy(getmaxx()-37, 70, s);
}
void sotruot()
{
char s[3];
setcolor(BLUE);
setfillstyle(SOLID_FILL, BLACK);
bar(getmaxx()-50, 160, getmaxx()-10, 180);
setcolor(YELLOW);
outtextxy(getmaxx()-33,170, s);
}
//--------- Cac ham trong class THANH--------//
void THANH::taothanh(int x1,int y1,int d1, int r1, int m1)
{
x=x1;
y=y1;
d=d1;
r=r1;
m=m1;
}
void THANH::ve()
{
setcolor(m);
setfillstyle(1,13);
rectangle(x,y,x+d,y+r);
}
void THANH::xoa()
{
setcolor(getbkcolor());
setfillstyle(1,getbkcolor());
rectangle(x,y,x+d,y+r);
}
int &THANH::getX()
{
return x;
}
int &THANH::getY()
{
return y;
}
int &THANH::getD()
{
return d;
}
int &THANH::getR()
{
return r;
}
//----------------- Cac ham trong class TUONG ---------//
void TUONG::taotuong()
{
int i,j;
n=9;
a[0].taoHCN(10,5,50,10,WHITE);
for(i=1;i<=n;i++)
{
a[i].taoHCN(a[i-1].getX()+56,5,50,10,WHITE);
}
}
void TUONG::ve()
{
for(int i=0;i<=n;i++)
{ setcolor(WHITE);
a[i].ve();
}
}
void TUONG::xoa()
{
for(int i=0;i<=n;i++)
a[i].xoa();
}
//=================================================//
void PINBALL::khoitao()
{
int maukhung;
khung(maukhung);
sodiem();
sotruot();
bong.taobong(240,454,5,YELLOW);
bong.ve();
thanh.taothanh(200,466,80,10,WHITE);
thanh.ve();
tuong.taotuong();
tuong.ve();
}
void PINBALL::play()
{
khoitao();
int dx=1;
int dy=1;
int i;
char ch;
int n=11;
do
{
int xb,yb;
xb=bong.getX();
yb=bong.getY();
bong.ve();
delay(5);
bong.xoa();
bong.getX()+=dx;
bong.getY()-=dy;
if(bong.getX()>getmaxx()-78||bong.getX()<8)
{
dx=-dx;
}
if(bong.
getY()<8||abs(thanh.
getY()-bong.
getY())<=8&&thanh.
getX()<=bong.
getX()&&bong.
getX()<=(thanh.
getX()+80)) {
dy=-dy;
}
for(i=0;i<=n;i++)
{
if(abs(yb
-tuong.
getTUONG(i
).
getY()-10)<=5&&xb
>tuong.
getTUONG(i
).
getX()&&xb
<(tuong.
getTUONG(i
).
getX()+50)) {
tuong.getTUONG(i).xoa();
// dy=-dy;
// dx=dx;
diem++;
sodiem();
break;
}
}
if((xb<thanh.getX()||xb>(thanh.getX()+80))&&yb>=(getmaxy()-12))
{
truot++;
bong.taobong(thanh.getX()+40,thanh.getY()-10,5,YELLOW);
bong.ve();
delay(5);
bong.xoa();
bong.getX()+=dx;
bong.getY()-=dy;
sotruot();
}
if(truot==3)
{
break;
}
if(kbhit())
{
ch=getch();
if(ch==27)
break;
if(ch==77&&thanh.getX()<getmaxx()-165)
{
thanh.xoa();
thanh.getX()=thanh.getX()+15;
thanh.ve();
}
if(ch==75&&thanh.getX()>10)
{
thanh.xoa();
thanh.getX()=thanh.getX()-15;
thanh.ve();
}
}
} while(ch!=27);
}
void main()
{
ktdh();
PINBALL ok;
ok.play();
getch();
}