Created
June 16, 2025 20:14
-
-
Save corlaez/0efa82078a55f066448086893fbef827 to your computer and use it in GitHub Desktop.
comentado carrera c++
This file contains hidden or 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 "pch.h" | |
#include <iostream> | |
#include <conio.h> | |
#include <Windows.h> | |
#include <ctime> | |
#include <vector> | |
using namespace System; | |
using namespace std; | |
const int ANCHO = 90; // columnas | |
const int ALTO = 34; // filas | |
const int numerocarros = 6; | |
const int carros_alto = 2; | |
const int carros_ancho = 14; | |
// En la direccion de memoria X tengo un array de 6 elementos cada uno de 4 bytes | |
// Lista generica de ConsoleColor | |
const ConsoleColor paleta[numerocarros] = | |
{ | |
ConsoleColor::Red, | |
ConsoleColor::Blue, | |
ConsoleColor::Green, | |
ConsoleColor::Cyan, | |
ConsoleColor::Magenta, | |
ConsoleColor::Yellow | |
}; | |
// Lista generica de colores string | |
const char paletaTexto[6][8] = | |
{ | |
"Rojo ", | |
"Azul ", | |
"Verde ", | |
"Celeste",//Cian | |
"Magenta", | |
"Yellow " | |
}; | |
// Lista que determina cual es color de cada carro | |
vector<string> colorTexto = | |
{ | |
"",// El color del carro 1 | |
"",// El color del carro 2 | |
"",// El color del carro 3 | |
"",// El color del carro 4 | |
"",// El color del carro 5 | |
""// El color del carro 6 | |
}; | |
ConsoleColor color[numerocarros]; | |
int posX[numerocarros]; | |
int posY[numerocarros]; | |
int vel[numerocarros];// 0 es para el carro cero y almacena el indice de paso asignado al carro 0 | |
// luego de una seleccion random vel puede quedar como { 2,1,5,0,3,4 } | |
// der indica la orientacion en la que se debe dibujar el carro. | |
bool der[numerocarros]; | |
int limIzq[numerocarros];// determina donde inician los carros | |
int limDer[numerocarros];// determina cuando giran los carros | |
int etapa[numerocarros];// 3 etapas por carro determina direccion de movimiento, der, abajo, izq | |
const int filaMeta[6] = { 20,22,24,26,28,30 };// determina donde dejan de bajar los carros | |
const int metaFinalX = 0;// determina cuando ganan | |
// paso son las velocidades posibles (mas chico mas veloz). El orden no implica a que carro va. | |
const int paso[6] = { 5, 5, 6, 6, 7, 8 }; | |
// indices de paso: 0 1 2 3 4 5 | |
int numeroDeCarreras = 0; | |
// estado que cuenta de carreras ganadas por carro. | |
int carrerasGanadas[8] = { 0, 0, 0, 0, 0, 0, 0, 0 }; | |
/* Bloquea la ejecucion del codigo hasta que detecta que una de las teclas ha sido presionada y retorna su indice */ | |
int esperarTeclas(std::initializer_list<char> teclas) { | |
while (true) { | |
if (kbhit) | |
{ | |
char teclaPresionada = getch(); | |
for (size_t i = 0; i < teclas.size(); ++i) { | |
if (teclaPresionada == teclas.begin()[i]) | |
{ | |
return i; | |
} | |
} | |
} | |
} | |
} | |
void moverCursor(int x, int y) { | |
if (x >= 0 && x < ANCHO && y >= 0 && y < ALTO) | |
Console::SetCursorPosition(x, y); | |
} | |
// 6 // 2 | |
const char* carroDer[numerocarros][carros_alto] = {// arreglo con 6 elementos, uno x carro | |
{// Arreglo de 2 elementos char* (direccion de memoria del primer caracter) | |
"_/¯¯¯¯¯¯¯¯\\__", | |
"\\--O-----O--/" | |
}, | |
{ | |
"---------- ", | |
"-@-[O_O]-@-- " | |
}, | |
{ | |
" #-------->> ", | |
"--[]--------[]" | |
}, | |
{ | |
" >>═══════ ", | |
"¯¯(_)¯¯¯(_)¯¯ " | |
}, | |
{ | |
" /¯¯¯¯\\___ ", | |
"/\\(o¯¯o)__/ " | |
}, | |
{ | |
"_________ ", | |
"\\(_)---(_)-/ " | |
} | |
}; | |
const char* carroIzq[numerocarros][carros_alto] = { | |
{ | |
"__/¯¯¯¯¯¯¯¯\\_", | |
"\\--O-----O--/" | |
}, | |
{ | |
" ----------", | |
" --@-[O_O]-@-" | |
}, | |
{ | |
" <<--------# ", | |
"[]--------[]--" | |
}, | |
{ | |
" ═══════<< ", | |
" ¯¯(_)¯¯¯(_)¯¯" | |
}, | |
{ | |
" ___/¯¯¯¯¯\\ ", | |
" \\__(o¯¯o)/\\" | |
}, | |
{ | |
" _________", | |
" \\-(_)---(_)/" | |
} | |
}; | |
void asignarVelocidades() | |
{ | |
for (int i = 0; i < numerocarros; ++i) | |
{ | |
int v; | |
bool repetido; | |
do { | |
v = rand() % 6; // ahora 0..5 | |
repetido = false; | |
for (int j = 0; j < i; j++) { | |
if (vel[j] == v) { // ¿está tomado? | |
repetido = true; | |
break; | |
} | |
} | |
} while (repetido); // repite hasta ser único | |
vel[i] = v; // guarda indice del paso (q determina velocidad) del auto i | |
} | |
} | |
void configurarVentana() { | |
Console::SetWindowSize(ANCHO, ALTO); | |
Console::CursorVisible = false; // con esto cambias la ventana ddf | |
} | |
void opciones() { | |
moverCursor(24, 17); cout << "[1] Carrera"; | |
moverCursor(38, 17); cout << "[2] Creditos"; | |
moverCursor(53, 17); cout << "[3] Salir"; | |
} | |
void nombreJuego() { | |
moverCursor(10, 5); cout << " ____ _ ____ ____ _____ ____ ___ ____ _ _ _____ ____"; | |
moverCursor(10, 6); cout << "/ ___| / \\ | _ \\ | _ \\| ____/ ___|_ _/ ___| \\ | | ____| _ \\."; | |
moverCursor(10, 7); cout << "| | / _ \\ | |_) | | | | | _ |\\___\\ | | | _| \\| | _| | |_) |"; | |
moverCursor(10, 8); cout << "| |___/ ___ \\| _ < | |_| | |___ ___) | | |_| | |\\ | |___| _ <"; | |
moverCursor(10, 9); cout << "\\____/_/ \\_\\_| \\_\\ |____/|_____|____/___\\____|_| \\_|_____|_| \\_\\."; | |
} | |
void creditos() { | |
Console::SetCursorPosition(10, 10); | |
cout << "Hecho por Andres Espejo, Piero Burgos y Fabio Silva"; | |
} | |
void asignarColores() | |
{ | |
for (int i = 0; i < numerocarros; i++) { | |
int paletacarros; | |
bool repetido; | |
do { | |
paletacarros = rand() % 6; // elige un color de 0..5 | |
repetido = false; | |
for (int j = 0; j < i; j++) { // ¿ya se usó? | |
if (color[j] == paleta[paletacarros]) { | |
repetido = true; | |
break; | |
} | |
} | |
} while (repetido); // repite hasta que no se repita | |
color[i] = paleta[paletacarros]; // guarda el color único | |
colorTexto[i] = paletaTexto[paletacarros]; | |
} | |
} | |
void dibujarAuto(int id, int x, int y, bool derecha) | |
{ | |
Console::ForegroundColor = color[id]; // color único | |
const char** linea = derecha ? carroDer[id] : carroIzq[id]; | |
for (int r = 0; r < carros_alto; ++r) { | |
moverCursor(x, y + r); | |
cout << linea[r]; | |
} | |
Console::ForegroundColor = ConsoleColor::Gray; | |
} | |
void colocarAutosInicio() { | |
/* límites personalizados – ajusta si lo deseas */ | |
int izq[6] = { 1, 16, 30, 1, 16, 31 }; | |
int der[6] = { 49, 60, 65, 51, 55, 61 }; | |
for (int id = 0; id < numerocarros; ++id) { | |
limIzq[id] = izq[id]; | |
limDer[id] = der[id]; | |
posX[id] = limIzq[id]; // arranca pegado al límite izq. | |
posY[id] = 2 + (id / 3) * (carros_alto + 2); | |
der[id] = true; | |
etapa[id] = 0; | |
dibujarAuto(id, posX[id], posY[id], true); | |
} | |
} | |
void borrarCarro(int id) { | |
for (int r = 0; r < carros_alto; r++) { | |
moverCursor(posX[id], posY[id] + r); | |
cout << string(carros_ancho, ' '); | |
} | |
} | |
void dibujarmeta(int x, int y) { | |
char b = 219; | |
Console::SetCursorPosition(x, y + 20); | |
cout << " " << b << " " << b << " " << b << endl; | |
cout << b << " " << b << " " << b << " " << endl; | |
cout << " " << b << " " << b << " " << b << endl; | |
cout << b << " " << b << " " << b << " " << endl; | |
cout << " " << b << " " << b << " " << b << endl; | |
cout << b << " " << b << " " << b << " " << endl; | |
cout << " " << b << " " << b << " " << b << endl; | |
cout << b << " " << b << " " << b << " " << endl; | |
cout << " " << b << " " << b << " " << b << endl; | |
cout << b << " " << b << " " << b << " " << endl; | |
cout << " " << b << " " << b << " " << b << endl; | |
cout << b << " " << b << " " << b << " " << endl; | |
cout << " " << b << " " << b << " " << b << endl; | |
} | |
void iniciarcarrera() {}; | |
void dibujarpista(int x, int y) { | |
char g = 238; | |
Console::SetCursorPosition(x, y + 10); | |
cout << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g; | |
Console::SetCursorPosition(x + 48, y + 10); | |
cout << "|"; | |
Console::SetCursorPosition(x + 48, y + 11); | |
cout << "|"; | |
Console::SetCursorPosition(x + 48, y + 12); | |
cout << "|"; | |
Console::SetCursorPosition(x + 48, y + 13); | |
cout << "|"; | |
Console::SetCursorPosition(x + 48, y + 14); | |
cout << "|"; | |
Console::SetCursorPosition(x + 48, y + 15); | |
cout << "|"; | |
Console::SetCursorPosition(x + 48, y + 16); | |
cout << "|"; | |
Console::SetCursorPosition(x + 48, y + 17); | |
cout << "|"; | |
Console::SetCursorPosition(x + 48, y + 18); | |
cout << "|"; | |
Console::SetCursorPosition(x, y + 19); | |
cout << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g << g; | |
Console::SetCursorPosition(4, y + 11); | |
cout << "Carreras Ganadas:\n"; | |
cout << colorTexto[0] << ": " << carrerasGanadas[0] << "\n"; | |
cout << colorTexto[1] << ": " << carrerasGanadas[1] << "\n"; | |
cout << colorTexto[2] << ": " << carrerasGanadas[2] << "\n"; | |
cout << colorTexto[3] << ": " << carrerasGanadas[3] << "\n"; | |
cout << colorTexto[4] << ": " << carrerasGanadas[4] << "\n"; | |
cout << colorTexto[5] << ": " << carrerasGanadas[5] << "\n"; | |
} | |
void bucleCarrera() { | |
int frame = 0; | |
bool hayGanador = false; | |
while (!hayGanador) { | |
for (int id = 0; id < numerocarros; ++id) { | |
/* Avanza solo cada vel[id] frames */ | |
if (frame % paso[vel[id]] == 0) { | |
borrarCarro(id); | |
// etapa 0, inicio, carro va a la derecha | |
if (etapa[id] == 0) { | |
if (posX[id] < limDer[id]) { | |
++posX[id]; | |
der[id] = true; | |
} | |
else { | |
etapa[id] = 1; | |
} | |
} | |
// etapa 1, la curva, hacia abajo | |
else if (etapa[id] == 1) { | |
der[id] = false; | |
if (posY[id] < filaMeta[id]) ++posY[id]; | |
else etapa[id] = 2; | |
} | |
// etapa 2, tramo final, hacia la izquierda | |
else { | |
if (posX[id] > metaFinalX) { | |
--posX[id]; | |
} | |
else { | |
hayGanador = true; | |
carrerasGanadas[id] += 1; | |
// Dibuja la pista de nuevo, por que esto refresca el ranking | |
dibujarpista(0, 0); | |
} | |
} | |
dibujarAuto(id, posX[id], posY[id], der[id]); | |
} | |
} | |
Sleep(1); | |
++frame; | |
} | |
} | |
void ranking() {}; | |
void simulacion_carrera() { | |
asignarColores();// inicializa color y colorTexto. | |
dibujarpista(0, 0); | |
dibujarmeta(0, 0); | |
colocarAutosInicio(); // pinta autos y pone límites | |
_getch(); | |
for (int i = 0; i < numeroDeCarreras; i++) { | |
Console::Clear(); | |
dibujarpista(0, 0); | |
dibujarmeta(0, 0); | |
asignarVelocidades(); | |
colocarAutosInicio(); // pinta autos y pone límites | |
bucleCarrera(); // ← ¡ahora sí se mueven! | |
} | |
cout << "\nPulsa una tecla..."; | |
_getch(); | |
} | |
void inicio() { | |
Console::Clear(); | |
int de0a4 = rand() % 5; | |
numeroDeCarreras = 4 + de0a4; | |
nombreJuego(); | |
opciones(); | |
int i = esperarTeclas({ '1', '2', '3' }); | |
if (i == 0) { | |
Console::Clear(); | |
simulacion_carrera(); | |
} | |
if (i == 1) { | |
Console::Clear(); | |
creditos(); | |
// Espera tecla Escape | |
esperarTeclas({ 27 });// se ignora el return pq siempre es 0 | |
inicio(); | |
} | |
if (i == 2) { | |
std::exit(0); | |
} | |
} | |
/* ─── Programa principal ─── */ | |
int main() { | |
srand(static_cast<unsigned int>(time(NULL))); | |
configurarVentana(); | |
Console::Clear(); | |
inicio(); | |
//moverCursor(0, ALTO - 1); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment