Last active
December 15, 2015 12:18
-
-
Save lesthack/5258831 to your computer and use it in GitHub Desktop.
pong.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdio.h> | |
#include<stdlib.h> | |
#include<graphics.h> | |
#include<conio.h> | |
main() | |
{ | |
int a,b; | |
int n,xc; | |
int v; /*velocidad con que se recorre*/ | |
int act; /*bandera para activar el game*/ | |
int xp, yp; | |
int sentido, sentido2; | |
int verdadero; | |
int fin; | |
int puntaje; | |
/*referente a la pantalla */ | |
a=DETECT; | |
initgraph(&a,&b,"d:\\tc\\bgi"); | |
setbkcolor(1); | |
cleardevice(); | |
/*---*/ | |
/*asignacion de variables*/ | |
v=30; | |
xc=10; | |
xp=28; | |
yp=53; | |
sentido=1; | |
sentido2=1; | |
verdadero=1; | |
fin=0; | |
puntaje=0; | |
/*---*/ | |
lado1a(xc); | |
lado2a(xc); | |
circle(xp,yp,5); | |
gotoxy(32,1);printf("Puntaje: "); | |
do{ | |
gotoxy(32,1);printf("Puntaje: %d",puntaje); | |
/* cuando presione una tecla se activara */ | |
if(kbhit()) | |
{ | |
n=getch(); | |
/*printf("%i",n);*/ | |
} | |
/* selcciona la tecla arriba o abajo */ | |
switch(n) | |
{ | |
case 80: | |
{ | |
/*abajo*/ | |
if(xc<370) | |
{ | |
lado1b(xc); | |
xc=xc+v; | |
} | |
n=0; | |
act=1; | |
break; | |
} | |
case 72: | |
{ | |
/*arriba*/ | |
if(xc>10) | |
{ | |
lado1b(xc); | |
xc=xc-v; | |
} | |
n=0; | |
act=1; | |
break; | |
} | |
} | |
lado1a(xc); | |
if(yp+20>xc && yp-25<(xc+80)) | |
{ | |
verdadero=1; | |
} | |
else | |
{ | |
verdadero=0; | |
} | |
/* atencion => pelota */ | |
if(act==1) | |
{ | |
setcolor(1); | |
circle(xp,yp,5); | |
if(sentido==1) /*hacia abajo*/ | |
{ | |
if(yp>=460) | |
{ | |
sentido=0; | |
} | |
if(xp<=30) | |
{ | |
if(verdadero==1) | |
{ | |
sentido2=1; | |
puntaje=puntaje+10; | |
} | |
else | |
{ | |
act=0; | |
fin=1; | |
} | |
} | |
if(sentido2==1) /* hacia la derecha */ | |
{ | |
xp=xp+10; | |
} | |
else /* hacia la izquierda */ | |
{ | |
xp=xp-10; | |
} | |
yp=yp+10; | |
if(xp>=615) | |
{ | |
sentido2=0; | |
} | |
} | |
if(sentido==0) /* hacia arriba */ | |
{ | |
if(xp==610) | |
{ | |
sentido=1; | |
} | |
if(yp<=15) | |
{ | |
sentido=1; | |
} | |
if(xp<=30) | |
{ | |
if(verdadero==1) | |
{ | |
sentido2=1; | |
puntaje=puntaje+10; | |
} | |
else | |
{ | |
act=0; | |
fin=1; | |
} | |
} | |
if(sentido2==1) /* hacia la derecha */ | |
{ | |
xp=xp+10; | |
} | |
else /* hacia la izquierda */ | |
{ | |
xp=xp-10; | |
} | |
yp=yp-10; | |
if(xp>=615) | |
{ | |
sentido2=0; | |
} | |
} | |
pelota(xp,yp,5); | |
} | |
/* aki termina pelota */ | |
/* inicia la otra tabla */ | |
if(act==1) | |
{ | |
if(sentido==1) | |
{ | |
lado2b(yp-55); | |
lado2a(yp); | |
} | |
else{ | |
if(sentido==0) | |
{ | |
lado2b(yp+55); | |
lado2a(yp); | |
} | |
} | |
} | |
/* termina aki */ | |
}while(n!=27 && fin!=1); | |
/* fin del juego */ | |
setfillstyle(1,15); | |
bar(140,180,500,280); | |
setfillstyle(1,1); | |
bar(143,183,497,277); | |
gotoxy(25,14); | |
printf("Has perdido el juego, adios."); | |
gotoxy(25,15); | |
printf("Presione r - repetir Esc - Salir"); | |
gotoxy(25,16); | |
printf("Derechos reservados JLHC"); | |
do{ | |
n=getch(); | |
if(n=='r') | |
{ | |
main(); | |
} | |
}while(n!=27); | |
cleardevice(); | |
closegraph(); | |
} | |
lado1a(int x) | |
{ | |
setfillstyle(1,15); | |
bar(10,x,20,x+100); | |
} | |
lado1b(int x) | |
{ | |
setfillstyle(1,1); | |
bar(10,x,20,x+100); | |
} | |
lado2a(int x) | |
{ | |
setfillstyle(1,15); | |
bar(620,x,630,x+100); | |
} | |
lado2b(int x) | |
{ | |
setfillstyle(1,1); | |
bar(320,x,630,x+100); | |
} | |
pelota(int x,int y) | |
{ | |
setcolor(15); | |
circle(x,y,5); | |
delay(8000); | |
setcolor(1); | |
circle(x,y,5); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment