Ping pong

El clásico juego del Ping Pong para MS-DOS, escrito en C++.
				 //PingPng.cpp
//Miguel Angel Cardoso Ramos
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
#include<stdlib.h>

int dx,dy,px;
int xmax,ymax;
int fin;
int vel=3,velr=20;

class objeto {
protected:
int x;
int y;
int color;
public:
void asigna(int nx,int ny,int clr);
};

class pelota: public objeto {
protected:
int radio;
public:
void leerad(int rad);
void dibuja(void);
void movpel(void);
};

class raqueta: public objeto {
protected:
int tamx;
int tamy;
public:
void leeraq(int tx,int ty);
void dibuja(void);
void movraq(void);
};

void objeto::asigna(int nx,int ny,int clr)
{
x=nx;
y=ny;
color=clr;
}

void pelota::leerad(int rad)
{
radio=rad;
}

void pelota::dibuja(void)
{
setbkcolor(BLACK);
setcolor(BLACK);
setfillstyle(1,color);
fillellipse(x,y,radio,radio);
}

void pelota::movpel(void)
{
setcolor(BLACK);
setbkcolor(BLACK);
setfillstyle(1,getbkcolor());
delay(5);
fillellipse(x,y,radio,radio);
x=x+dx;
y=y+dy;
setfillstyle(1,color);
fillellipse(x,y,radio,radio);
if(x<5)dx=vel;
if(x>(xmax-5))dx=-vel;
if(y<5)dy=vel;
if(y>ymax) fin=1;
if((y>=(ymax*3/4-6)) && (getpixel(x,y+6)==BLUE))
dy=-vel;
}
void raqueta::leeraq(int tx,int ty)
{
tamx=tx;
tamy=ty;
}
void raqueta::dibuja(void)
{
setbkcolor(BLACK);
setcolor(BLACK);
setfillstyle(1,color);
bar(x,y,x+tamx,y+tamy);
}
void raqueta::movraq(void)
{
setbkcolor (BLACK);
setcolor(BLACK);
setfillstyle(1,getbkcolor());
bar(x,y,x+tamx,y+tamy);
x=x+px;
if(x>(xmax-40)) x=xmax-40;
if(x<0)x=0;
setfillstyle(1,color);
bar(x,y,x+tamx,y+tamy);
}

void inigraf(void)
{
int ag=DETECT,mg;
initgraph(&ag,&mg,"c:bc5bgi");
}
void fingraf(void)
{
closegraph();
}
void main()
{
char opcion,tecla;
pelota pel;
raqueta raq;
inigraf();
xmax=getmaxx();
ymax=getmaxy();
pel.leerad(5);
raq.leeraq(40,5);
randomize();
do{
cleardevice();
pel.asigna(xmax/2,ymax*3/4-6,RED);
raq.asigna(xmax/2-20,ymax*3/4,BLUE);
pel.dibuja();
raq.dibuja();
do{
tecla=getch();
}while(tecla!=13);
dy=-vel;
if(random(2)==0)dx=vel; else dx=-vel;
fin=0;
do{
pel.movpel();
if(kbhit())
{
tecla=getch();
if(tecla==0)tecla=getch();
switch(tecla)
{
case 77:
px=velr;
break;
case 75:
px=-velr;
break;
case 27:
fin=1;
break;
}
if(tecla!=27)raq.movraq();
}
}while(fin!=1);
setcolor(WHITE);
outtextxy(300,440,"otro juego(s/n)?");
opcion=getch();
}while((opcion!='n')&& (opcion!='n'));
fingraf();
}
Descargar adjuntos
COMPARTE ESTE TUTORIAL

COMPARTIR EN FACEBOOK
COMPARTIR EN TWITTER
COMPARTIR EN LINKEDIN
COMPARTIR EN WHATSAPP
TUTORIAL ANTERIOR

SIGUIENTE TUTORIAL