Skip to content

Instantly share code, notes, and snippets.

@zrobit
Created January 18, 2015 01:49
Show Gist options
  • Select an option

  • Save zrobit/7701302e9155fd21af71 to your computer and use it in GitHub Desktop.

Select an option

Save zrobit/7701302e9155fd21af71 to your computer and use it in GitHub Desktop.
c++ example
// PONG.cpp : main project file.
#include "stdafx.h"
#include <conio.h>
#include <stdio.h>
#include <math.h>
#include <windows.h>
using namespace System;
void limpia_linea(int x,int y)
{
for (int i=0;i<85;i++)
{
Console::SetCursorPosition(i,y);
printf(" ");
}
}
void dibuja_pong(int x,int y){
if(y<=25){
Console::SetCursorPosition(x,y);
printf("PPPPPPPPPPPPPPPPP OOOOOOOOO NNNNNNNN NNNNNNNN GGGGGGGGGGGGG \n");
}
else
{ Console::SetCursorPosition(x,y-25);
printf("PPPPPPPPPPPPPPPPP OOOOOOOOO NNNNNNNN NNNNNNNN GGGGGGGGGGGGG \n");
}
if(y+1<=25)
{
Console::SetCursorPosition(x,y+1);
printf("P::::::::::::::::P OO:::::::::OO N:::::::N N::::::N GGG::::::::::::G \n");
}
else{
Console::SetCursorPosition(x,y+1-25);
printf("P::::::::::::::::P OO:::::::::OO N:::::::N N::::::N GGG::::::::::::G \n");
}
if(y+2<=25)
{ Console::SetCursorPosition(x,y+2);
printf("P::::::PPPPPP:::::P OO:::::::::::::OO N::::::::N N::::::N GG:::::::::::::::G \n");
}
else
{
Console::SetCursorPosition(x,y+2-25);
printf("P::::::PPPPPP:::::P OO:::::::::::::OO N::::::::N N::::::N GG:::::::::::::::G \n");
}
if(y+3<=25){
Console::SetCursorPosition(x,y+3);
printf("PP:::::P P:::::PO:::::::OOO:::::::ON:::::::::N N::::::N G:::::GGGGGGGG::::G \n");}
else{
Console::SetCursorPosition(x,y+3-25);
printf("PP:::::P P:::::PO:::::::OOO:::::::ON:::::::::N N::::::N G:::::GGGGGGGG::::G \n");}
if(y+4<=25){
Console::SetCursorPosition(x,y+4);
printf(" P::::P P:::::PO::::::O O::::::ON::::::::::N N::::::N G:::::G GGGGGG \n");}
else{
Console::SetCursorPosition(x,y+4-25);
printf(" P::::P P:::::PO::::::O O::::::ON::::::::::N N::::::N G:::::G GGGGGG \n");}
if(y+5<=25){
Console::SetCursorPosition(x,y+5);
printf(" P::::P P:::::PO:::::O O:::::ON:::::::::::N N::::::NG:::::G \n");}
else{
Console::SetCursorPosition(x,y+5-25);
printf(" P::::P P:::::PO:::::O O:::::ON:::::::::::N N::::::NG:::::G \n");}
if(y+6<=25){
Console::SetCursorPosition(x,y+6);
printf(" P::::P P:::::PO:::::O O:::::ON:::::::::::N N::::::NG:::::G \n");}
else{
Console::SetCursorPosition(x,y+6-25);
printf(" P::::P P:::::PO:::::O O:::::ON:::::::::::N N::::::NG:::::G \n");}
if(y+7<=25){
Console::SetCursorPosition(x,y+7);
printf(" P:::::::::::::PP O:::::O O:::::ON::::::N N::::N N::::::NG:::::G GGGGGGGGGG \n");}
else{
Console::SetCursorPosition(x,y+7-25);
printf(" P:::::::::::::PP O:::::O O:::::ON::::::N N::::N N::::::NG:::::G GGGGGGGGGG \n");}
if(y+8<=25){
Console::SetCursorPosition(x,y+8);
printf(" P::::PPPPPPPPP O:::::O O:::::ON::::::N N::::N:::::::NG:::::G G::::::::G \n");}
else{
Console::SetCursorPosition(x,y+8-25);
printf(" P::::PPPPPPPPP O:::::O O:::::ON::::::N N::::N:::::::NG:::::G G::::::::G \n");}
if(y+9<=25){
Console::SetCursorPosition(x,y+9);
printf(" P::::P O:::::O O:::::ON::::::N N:::::::::::NG:::::G GGGGG::::G \n");}
else{
Console::SetCursorPosition(x,y+9-25);
printf(" P::::P O:::::O O:::::ON::::::N N:::::::::::NG:::::G GGGGG::::G \n");}
if(y+10<=25){
Console::SetCursorPosition(x,y+10);
printf(" P::::P O:::::O O:::::ON::::::N N::::::::::NG:::::G G::::G \n");}
else{
Console::SetCursorPosition(x,y+10-25);
printf(" P::::P O:::::O O:::::ON::::::N N::::::::::NG:::::G G::::G \n");}
if(y+11<=25){
Console::SetCursorPosition(x,y+11);
printf(" P::::P O:::::O O:::::ON::::::N N::::::::::NG:::::G G::::G \n");}
else{
Console::SetCursorPosition(x,y+11-25);
printf(" P::::P O:::::O O:::::ON::::::N N::::::::::NG:::::G G::::G \n");}
if(y+12<=25){
Console::SetCursorPosition(x,y+12);
printf("PP::::::PP O:::::::OOO:::::::ON::::::N N::::::::N G:::::GGGGGGGG::::G \n");}
else{
Console::SetCursorPosition(x,y+12-25);
printf("PP::::::PP O:::::::OOO:::::::ON::::::N N::::::::N G:::::GGGGGGGG::::G \n");}
if(y+13<=25){
Console::SetCursorPosition(x,y+13);
printf("P::::::::P OO:::::::::::::OO N::::::N N:::::::N GG:::::::::::::::G \n");}
else{
Console::SetCursorPosition(x,y+13-25);
printf("P::::::::P OO:::::::::::::OO N::::::N N:::::::N GG:::::::::::::::G \n");}
if(y+14<=25){
Console::SetCursorPosition(x,y+14);
printf("P::::::::P OO:::::::::OO N::::::N N::::::N GGG::::::GGG:::G \n");}
else{
Console::SetCursorPosition(x,y+14-25);
printf("P::::::::P OO:::::::::OO N::::::N N::::::N GGG::::::GGG:::G \n");}
if(y+15<=25){
Console::SetCursorPosition(x,y+15);
printf("PPPPPPPPPP OOOOOOOOO NNNNNNNN NNNNNNN GGGGGG GGGG \n");}
else{
Console::SetCursorPosition(x,y+15-25);
printf("PPPPPPPPPP OOOOOOOOO NNNNNNNN NNNNNNN GGGGGG GGGG \n");}
if(y+16<=25){
Console::SetCursorPosition(x,y+16);
printf(" PRESIONE LA TECLA (s o S)START GAME PARA COMENZAR A JUGAR!!");}
else{
Console::SetCursorPosition(x,y+16-25);
printf(" PRESIONE LA TECLA (s o S)START GAME PARA COMENZAR A JUGAR!!");}
if(y+17<=25){
Console::SetCursorPosition(x,y+17);
printf(" %c 2009 UPC Peru, all rights reserved%c\n",184,169);}
else{
Console::SetCursorPosition(x,y+17-25);
printf(" %c 2009 UPC Peru, all rights reserved%c\n",184,169);}
if(y+18<=25){
Console::SetCursorPosition(x,y+18);
printf(" Created By Gerardo Miranda and Javier Kometter\n");}
else{
Console::SetCursorPosition(x,y+18-25);
printf(" Created By Gerardo Miranda and Javier Kometter\n");}
}
void intro(){
int n,x=0,y=0;
Console::SetWindowSize(85,26);
while(!_kbhit())
{
n=Random::Random().Next(1,5);
if(n==1)
Console::ForegroundColor=ConsoleColor::Red;
if(n==2)
Console::ForegroundColor=ConsoleColor::Green;
if(n==3)
Console::ForegroundColor=ConsoleColor::Yellow;
if(n==4)
Console::ForegroundColor=ConsoleColor::Blue;
y++;if(y==6)y=0;
dibuja_pong(x,y);Sleep(150);
Console::Clear();
}
}
void instrucciones()
{
Console::SetCursorPosition(20,3);
Console::ForegroundColor=ConsoleColor::Yellow;
printf(" INSTRUCCIONES(IMPORTANTE)\n\n\n");
printf("1- Al inicio del juego ingrese su nombre de usuario\n\n");
printf("2- Para mover las barras horizontales presionar las siguientes teclas :\n\n");
printf(" %c (izquierda) y %c (derecha)\n\n",17,16);
printf("3- Para mover las barras verticales presionar las siguientes teclas :\n\n");
printf(" %c (arriba) y %c (abajo)\n\n",30,31);
printf("4- Para comenzar a jugar presione (a o A), despues INGRESE su usuario\n\n");
}
void user(char *inicial){
//char inicial[100];
printf("INGRESA TU NOMBRE DE USUARIO: ");
flushall();
scanf("%s",inicial);
}
void escenario(){
int x=0, y=0, dx,dy;
Console::SetWindowSize(80,40);
Console::SetWindowPosition(0,0);
Console::BackgroundColor=ConsoleColor::White;
Console::Clear();
Console::ForegroundColor=ConsoleColor::Black;
while(1){
Console::SetCursorPosition(x,y);
if(x==0&&y==1){printf("%c",186);break;}
if(x==0&&y==0){printf("%c",201);dx=1;dy=0;}
if(x==79&&y==0){printf("%c",187);dx=0;dy=1;}
if(x==79&&y==38){printf("%c",188);dx=-1;dy=0;}
if(x==0&&y==38){printf("%c",200);dx=0;dy=-1;};
if(x!=0 && x!=79 &&y==0) printf("%c",205);
if(x!=0 && x!=79 &&y==38) printf("%c",205);
if(x==0 && y!=0 && y!=38) {
if(x==0&&y==5)printf("%c",204);
else printf("%c",186);
};
if(x==79 && y!=0 && y!=38){
if(x==79&&y==5)printf("%c",185);
else printf("%c",186);
};
x= x + dx;
y= y + dy;
}
x=1;
y=5;
Console::SetCursorPosition(x,y);
while(x>=1&&x<=79){
printf("%c",205);
Console::SetCursorPosition(x++,y);
}
}
void verticales(int Y, int dir){
int largo=10;
if(dir==-1){
Console::SetCursorPosition(2,Y+largo);
printf(" ");
Console::SetCursorPosition(77,Y+largo);
printf(" ");
}
if(dir==1){
Console::SetCursorPosition(2,Y-1);
printf(" ");
Console::SetCursorPosition(77,Y-1);
printf(" ");
}
for(int i=0;i<largo;i++){
Console::SetCursorPosition(2,Y+i);
printf("%c",206);
Console::SetCursorPosition(77,Y+i);
printf("%c",206);
}
}
//funcion barras horizontales
void horizontales(int X, int dir){
int largo=10;
if(dir==-1){
Console::SetCursorPosition(X+largo,6);
printf(" ");
Console::SetCursorPosition(X+largo,37);
printf(" ");
}
if(dir==1){
Console::SetCursorPosition(X-1,6);
printf(" ");
Console::SetCursorPosition(X-1,37);
printf(" ");
}
for(int i=0;i<largo;i++){
Console::SetCursorPosition(X+i,6);
printf("=");
Console::SetCursorPosition(X+i,37);
printf("=");
}
}
//fin funcion horizontales
int main(array<System::String ^> ^args)
{
int posX=35, posY=15, x, y, score=0, vidas=10;
float a=7.0, b=7.0, dx=0.1, dy=0.1;
char key;
char userIni[100];
intro();
while(1){
if(_kbhit())if(_getch()=='s' || 'S'){
Console::Clear();
break;
}
}
instrucciones();
while(1){
if(_kbhit())if(_getch()=='a' || 'A'){
Console::Clear();
break;
}
}
user(userIni);
escenario();
verticales(posY,0);
horizontales(posX,0);
while(vidas>0){
if(_kbhit()){
key=_getch();
switch(key){
case 72:if(posY==7)verticales(posY,-1);
else verticales(--posY,-1);
break;
case 80:if(posY==27)verticales(posY,1);
else verticales(++posY,1);
break;
case 77:if(posX==67)horizontales(posX,1);
else horizontales(++posX,1);
break;
case 75:if(posX==3)horizontales(posX,-1);
else horizontales(--posX,-1);
break;
}//fin de captura de teclas
}
//pelota
x=posX;
y=posY;
Console::SetCursorPosition(a,b);
printf(" ");
//restriccion horizontal
if(((x<=int(a)&&int(a)<x+3)||(x+7<int(a)&&int(a)<=x+10))&&(int(b)==7||int(b)==36)){
dy*=-1;
score+=100;
Console::Beep();
}
if((x+3<=int(a)&&int(a)<=x+7)&&(int(b)==7||int(b)==36)){
dy*=-1;
score+=100;
Console::Beep();
}
//restriccion vertical
if(((y<=int(b)&&int(b)<(y+3))||((y+7)<int(b)&&int(b)<=(y+10)))&&(int(a)==3||int(a)==76)){
dx*=-1;
score+=100;
Console::Beep();
}
if((y+3<=int(b)&&int(b)<=(y+7))&&(int(a)==3||int(a)==76)){
dx*=-1;
score+=100;
Console::Beep();
}
//restriccion de salida de marco
if((3<=int(a)&&int(a)<=78&&int(b)==6)||(3<=int(a)&&int(a)<=77&&int(b)==37)){
Console::SetCursorPosition(40,3);
vidas--;
a=9.0; b=9.0; dx=0.1; dy=0.1;
/*verticales(posY,0);
horizontales(posX,0);*/
//if (vidas==0)
//printf("PERDISTE %c", userIni);
};
if((6<=int(b)&&int(b)<=37&&int(a)==2)||(6<=int(b)&&int(b)<=37&&int(a)==78)){
Console::SetCursorPosition(40,3);
vidas--;
a=9.0; b=9.0; dx=0.1; dy=0.1;
/*verticales(posY,0);
horizontales(posX,0);*/
//if (vidas==0)
//printf("perdiste %c", userIni);
};
a+=dx;
b+=dy;
Console::SetCursorPosition(a,b);
printf("%c",15);
Console::SetCursorPosition(3,2);
printf("Score: %d",score);
Console::SetCursorPosition(33,2);
printf(" ");
Console::SetCursorPosition(25,2);
printf("Vidas: %d",vidas);
Console::SetCursorPosition(60,2);
printf("Hola, %s",userIni);
Sleep(10);
//fin depelota
}
if(vidas>=1 && vidas<=3)
{
Console::SetCursorPosition(60,4);
Console::ForegroundColor=ConsoleColor::Red;
printf("Cuidado te quedan pocas vidas");
}
if(vidas==0)
{
Console::Clear();
Console::BackgroundColor=ConsoleColor::Black;
Console::Clear();
Console::ForegroundColor=ConsoleColor::Yellow;
Console::SetCursorPosition(0,14);
printf("8888888b. 8888888888 8888888b. 888888888 8888888 .d8888b. 88888888888 8888888\n");
printf("888 Y88b 888 888 Y88b 888 YY88b 888 d88P Y88b 888 888 \n");
printf("888 888 888 888 888 888 888 888 Y88b. 888 888 \n");
printf("888 d88P 8888888 888 d88P 888 888 888 YY888b. 888 88888 \n");
printf("8888888PP 888 8888888PR 888 888 888 YY88b. 888 888 \n");
printf("888 888 888 T88b 888 888 888 8888 888 888 \n");
printf("888 888 888 T88b 888 d88P 888 Y88b d88P 888 888 \n");
printf("888 8888888888 888 T88b 8888888PP 8888888 YY8888PP 888 8888888 \n");
}
_getch();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment