ian--

 avatar
unknown
plain_text
2 years ago
4.7 kB
2
Indexable
#include "pch.h"
#include "iostream"
#include "conio.h"
#include <Windows.h>

#define ESCAPE 27 

using namespace std;
using namespace System;
void color(int c)
{
    switch (c)
    {
    case 1: Console::ForegroundColor = ConsoleColor::Black; break;
    case 2: Console::ForegroundColor = ConsoleColor::DarkBlue; break;
    case 3: Console::ForegroundColor = ConsoleColor::DarkGreen; break;
    case 4: Console::ForegroundColor = ConsoleColor::DarkRed; break;
    case 5: Console::ForegroundColor = ConsoleColor::DarkMagenta; break;
    case 6: Console::ForegroundColor = ConsoleColor::DarkYellow; break;
    case 7: Console::ForegroundColor = ConsoleColor::Gray; break;
    case 8: Console::ForegroundColor = ConsoleColor::DarkGray; break;
    case 9: Console::ForegroundColor = ConsoleColor::Blue; break;
    case 10: Console::ForegroundColor = ConsoleColor::Green; break;
    case 11: Console::ForegroundColor = ConsoleColor::Cyan; break;
    case 12: Console::ForegroundColor = ConsoleColor::Red; break;
    case 13: Console::ForegroundColor = ConsoleColor::Magenta; break;
    case 14: Console::ForegroundColor = ConsoleColor::Yellow; break;
    case 15: Console::ForegroundColor = ConsoleColor::White; break;
    }
}

void crea_ficha(int* ficha, int tam)
{
    for (int i = 0; i < tam; i++)
    {
        ficha[i] = 219;
    }
}

void imprime_ficha(int* ficha, int tam)
{
    for (int i = 0; i < tam; i++)
    {
        cout << char(ficha[i]);

    }
}

void borra_ficha(int* ficha, int tam)
{
    for (int i = 0; i < tam; i++)
        cout << "  ";
}

void cursor(int x, int y)
{
    Console::SetCursorPosition(x, y);
}

void configura_consola()
{
    //Console::SetWindowSize(80, 40);
    Console::CursorVisible = false;
}

double retorna_rand() {
    return 0.1 + (double)rand() / (RAND_MAX + 4.0);
}

void menu()
{
    cout << "**** MENU ***" << endl;
    cout << "1. Nueva partida" << endl;
    cout << "2. Resultados" << endl;
    cout << "3. Creditos" << endl;
    cout << "4. Salir" << endl;
    cout << "Elegir una opcion: ";
}

void Resultados()
{

}
void Creditos()
{

}

void Dibujar_meta()
{
    for (int y = 0; y < 78; y++)
    {
        char meta = 219;
        cursor(75, y);
        cout << meta;
    }
}
void Dibujar_salida() 
{
    for (int y = 0; y < 78; y++)
    {
        char salida = 219;
        cursor(2, y);
        cout << salida;
    }

}
void Nueva_partida()
{
    int* ficha1, * ficha2, * ficha3;
    int tam1, tam2, tam3;
    float x[3] = {3,3,3 };
    float y[3] = { 5,10,15 };
    //velocidad
    float dx[3] = { 1,1,1 }; //inicializando arreglo X
    float dy[3] = { 0,0,0 }; //inicializando arreglo Y
    dx[0] = retorna_rand(); // velocidad aleatoria
    dx[1] = retorna_rand(); // velocidad aleatoria
    dx[2] = retorna_rand(); // velocidad aleatoria

    tam1 = 1;
    ficha1 = new int[tam1];
    crea_ficha(ficha1, tam1);
    
    tam2 = 1;
    ficha2 = new int[tam2];
    crea_ficha(ficha2, tam2);

    tam3 = 1;
    ficha3 = new int[tam3];
    crea_ficha(ficha3, tam3);
    if (true) { color(4); Dibujar_meta(); }
    if (true) { color(11); Dibujar_salida(); }

    int i = 0;
    while (true)
    {
        if (kbhit())
        {
            char tecla = getch();
            if (tecla == ESCAPE) break;
        }
        //borrar
        cursor(x[i], y[i]);
        if (i == 0) borra_ficha(ficha1, tam1);
        if (i == 1) borra_ficha(ficha2, tam2);
        if (i == 2) borra_ficha(ficha3, tam3);

        //mover
        if (y[i] > 38 || y[i] < 1) dy[i] *= -1.0;
        if (x[i] < 1) dx[i] *= -1.0;

        x[i] += dx[i];
        y[i] += dy[i];

        if (x[i] > 75) cout << "La ficha [" << i + 1 << ": Es la ganadora!!! ";

        //dibujar
        cursor(x[i], y[i]);
        if (i == 0) { color(13); imprime_ficha(ficha1, tam1); }
        if (i == 1) { color(10); imprime_ficha(ficha2, tam2); }
        if (i == 2) { color(14); imprime_ficha(ficha3, tam3); }


        i++;
        if (i > 2) i = 0;
        _sleep(100);
    }
}


int main()
{
    configura_consola();
    srand(time(NULL));
    int opcion;

    while (true)
    {
        menu();
        cin >> opcion;
        switch (opcion)
        {
        case 1:
            system("cls");
            if (true) { color(15); Nueva_partida(); }
            break;
        case 2:
            Resultados();
            break;
        case 3:
            Creditos();
            break;
        case 4: exit(0);
        }
        //system("pause>0");
        system("cls");
    }
    return 0;
}