#include "stdafx.h"
#include "windows.h"
#include "conio.h"
#include "iostream"
using namespace std;
struct Objetos
{
int x, y, pos;
bool hit;
};
void gotoxy(int x, int y) {
HANDLE hcon;
hcon = GetStdHandle(STD_OUTPUT_HANDLE);
COORD dwPos;
dwPos.X = x;
dwPos.Y = y;
SetConsoleCursorPosition(hcon, dwPos);
}
void Marco()
{
char a = 186, b = 205, c[4]{ 201,187,188,200 };
for (int i = 1; i <= 100; i++)
{
if (i < 30)
{
gotoxy(100, i);
cout << a;
gotoxy(0, i);
cout << a;
}
gotoxy(i, 0);
cout << b;
gotoxy(i, 30);
cout << b;
}
gotoxy(0, 0);
cout << c[0];
gotoxy(100, 0);
cout << c[1];
gotoxy(0, 30);
cout << c[3];
gotoxy(100, 30);
cout << c[2];
}
int Menu(int opc)
{
char tecla;
gotoxy(40, 10);
cout << "BREAKOUT";
gotoxy(40, 12);
cout << " PLAY";
gotoxy(40, 13);
cout << " HIGH SCORES";
gotoxy(40, 14);
cout << " EXIT";
do
{
gotoxy(39, opc);
cout << ">";
gotoxy(39, opc);
tecla = _getch();
gotoxy(39, opc);
cout << " ";
if (tecla == 72 && opc > 12) opc--;
if (tecla == 80 && opc < 14) opc++;
} while (tecla != 13);
return opc;
}
void gameover()
{
system("cls");
Marco();
gotoxy(45, 15);
cout << "GAME OVER";
getchar();
}
class Bloque
{
public:
void Mostrar(Objetos blocks[116])
{
char a = 223;
for (int x = 0; x < 116; x++)
{
gotoxy(blocks[x].x, blocks[x].y);
cout << a;
cout << a;
cout << a;
cout << a;
}
}
Objetos Hit(Objetos block)
{
gotoxy(block.x, block.y);
cout << " ";
block.hit = true;
return block;
}
}Cblock;
class Barra
{
public:
void Mostrar(Objetos bar)
{
char a = 238;
gotoxy(bar.x, 25);
for (int weu = 0; weu < 8; weu++)
{
cout << a;
}
}
void Borrar(Objetos bar)
{
gotoxy(bar.x, 25);
cout << " ";
}
int Mover(int x,char cTecla)
{
if (cTecla == 77 && x < 90) x += 3;
if (cTecla == 75 && x > 3) x -= 3;
return x;
}
}Cbar;
class Bola
{
public:
void Mostrar(Objetos ball)
{
gotoxy(abs(ball.x), abs(ball.y));
cout << "*";
}
void Borrar(Objetos ball)
{
gotoxy(abs(ball.x), abs(ball.y));
cout << " ";
}
Objetos ReboteBloques(Objetos block, Objetos ball)
{
block.pos = 1;
if (block.hit == false)
{
if (block.y == abs(ball.y))
{
for (int dkb = 0; dkb < 5; dkb++)
{
if (block.x + dkb == abs(ball.x))
{
block.hit = true;
block.pos = 0;
Cblock.Hit(block);
}
}
}
}
return block;
}
Objetos ReboteBarra(Objetos bar, Objetos ball)
{
for (int r = 0; r < 8; r++)
{
if (bar.x + r == abs(ball.x))
{
ball.y = ball.y*-1;
if (r > -1 && r < 4 && ball.x>0)
{
ball.x = ball.x*-1;
}
else
{
if (r > 3 && r < 8 && ball.x<0)
{
ball.x = ball.x*-1;
}
}
}
}
return ball;
}
Objetos Rebote(Objetos ball)
{
if (abs(ball.x) == 1 || abs(ball.x) == 99) ball.x = ball.x*-1;
if (abs(ball.y) == 1) ball.y = ball.y*-1;
return ball;
}
}Cball;
int main()
{
int opc = 12, nana, wer, score;
char tecla;
Objetos blocks[116], ball, bar;
do
{
system("cls");
Marco();
opc = Menu(opc);
switch (opc)
{
case 12:
score = 0;
nana = 0;
wer = 9;
for (int qwe = 9; qwe <= 15; qwe++)
{
for (wer; wer <= 90; wer += 5)
{
blocks[nana].hit = false;
blocks[nana].x = wer;
blocks[nana].y = qwe;
blocks[nana].pos = 1;
nana++;
}
if (qwe % 2 != 0)
{
wer = 11;
}
else
{
wer = 9;
}
}
ball.hit = false;
ball.x = 50;
ball.y = 24;
bar.y = 25;
bar.x = 46;
system("cls");
Marco();
gotoxy(90, 31);
cout << "SCORE: ";
cout << score;
Cblock.Mostrar(blocks);
Cbar.Mostrar(bar);
do
{
do
{
Cball.Mostrar(ball);
Sleep(100);
Cball.Borrar(ball);
if (abs(ball.y) == bar.y)
{
ball = Cball.ReboteBarra(bar, ball);
}
else
{
ball = Cball.Rebote(ball);
for (int meh = 0; meh <= 116; meh++)
{
blocks[meh] = Cball.ReboteBloques(blocks[meh], ball);
if (blocks[meh].hit == true && blocks[meh].pos == 0)
{
ball.y = ball.y*-1;
score++;
gotoxy(90, 31);
cout << "SCORE: ";
cout << score;
break;
}
}
}
if (abs(ball.y) == 29)
{
ball.hit = true;
break;
}
ball.y++;
ball.x++;
} while (!_kbhit());
if (ball.hit == false)
{
tecla = _getch();
Cbar.Borrar(bar);
bar.x = Cbar.Mover(bar.x, tecla);
Cbar.Mostrar(bar);
}
} while (ball.hit != true);
gameover();
break;
case 13:
system("cls");
Marco();
gotoxy(49, 15);
cout << "No";
getchar();
break;
}
} while (opc != 14);
return 0;
}
An almost complete breakout, the Atari game. The only thing that I need to add is files to save all the scores!
UPDATE: I made some changes but it works the same!
UPDATE: I made some changes but it works the same!
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.