[ create a new paste ] login | about

Link: http://codepad.org/fTP76sPe    [ raw code | output | fork ]

C++, pasted on May 6:
#include <iostream>
#include <conio2.h>
#include <ctime>
#include <time.h>

using namespace std;

void retraso(float segundos){ 
	clock_t finretraso; 
	finretraso=clock()+segundos*CLOCKS_PER_SEC; 
	while(clock()<finretraso){} 
} 

int main(int argc, char *argv[]) {
	
	int ax=1, ay=1, bx=2, by=3, cx=1, cy=1, barrax=33;
	char tecla; 
	//While dá inicio al ciclo de repetición.
	
	while (true) {
		
		gotoxy(ax,ay);//borra la posicion 
		cout<<" "; //anterior de la pelota 
	
		gotoxy(bx,by);
		cout<<"O"<<endl;
		retraso (0.05);
/*NOTA: Habrán notado que utilizo los valores 79 (x) y 24 (y) en lugar de 80;25. Esto es porque al utilizar textbackground la pantalla se movía 
cuando la pelota tocaba los bordes. Creo que al usar el fondo, los carácteres ocupan 1 pixel más y por eso lo solucione usando un pixel menos, pero no estoy seguro.*/
		if (bx<=1)
		{
			bx=1;
			cx=-cx;
		}
		if (bx>=79)
		{
			bx=79;
			cx=-cx;	
		}
		if (by<=1)
		{
			by=1;
			cy=-cy;
		}
		if (by>=24)
		{
			by=24;
			cy=-cy;
		}
		//La linea de abajo indica qué hacer si hay colisión con la barra. La barra se encuentra en y=20, la función textbackground, visualmente, agrega un pixel de altura, por lo tanto, la colisión debe ocurrir en y=19.
		if (((bx>=barrax)&&(bx<=barrax+7))&&(by==19)){ 
			cy=-cy; 
		}
		ax=bx, ay=by; 
		bx=bx+cx; 
		by=by+cy;
		//Acá empieza la barra


		gotoxy (barrax, 20);
		textbackground (WHITE);//Color de fondo de la barra (seis espacios)
		cout<<"      "<<endl;
		textbackground (BLACK);//Volvemos a Negro porque sino la 'O' queda coloreada y deja un rastro, prueben haciendo el cambio.
		if(kbhit()){ 
			tecla=getch(); 
			if(tecla==char(75)){ 
				barrax--; 
				gotoxy(barrax+7,20); 
				cout<<" "; 
			} 
			else if (tecla==char(77)){ 
				barrax++; 
				gotoxy(barrax-1,20); 
				cout<<" "; 
			} 
					}
	}
	return 0;
}


Output:
1
2
3
4
5
6
7
Line 19: error: conio2.h: No such file or directory
cc1plus: warnings being treated as errors
In function 'void retraso(float)':
Line 10: warning: converting to 'clock_t' from 'float'
In function 'int main(int, char**)':
Line 22: error: 'gotoxy' was not declared in this scope
compilation terminated due to -Wfatal-errors.


Create a new paste based on this one


Comments: