Skip to content

Instantly share code, notes, and snippets.

@jorarmarfin
Created November 17, 2024 20:50
Show Gist options
  • Save jorarmarfin/7ab09fd003723f6f3d541e459de8a6a2 to your computer and use it in GitHub Desktop.
Save jorarmarfin/7ab09fd003723f6f3d541e459de8a6a2 to your computer and use it in GitHub Desktop.
#include "miniwin.h"
#include <sstream>
#include <cstdlib>
#include <ctime>
using namespace miniwin;
using namespace std;
inline string toString(int num) {
ostringstream buffer;
buffer << num;
return buffer.str();
}
int main() {
int rX = 0, rY = 0;
int clickCounter = 0;
int appearanceCounter = 0; // Contador de apariciones del cuadrado
int aciertos = 0; // Contador de aciertos sobre los topos
int t = tecla();
vredimensiona(500, 500);
srand(static_cast<unsigned int>(time(0)));
// Variables para manejar la posición aleatoria de los cuatro topos
int ancho = 50;
int x1 = rand() % (500 - ancho);
int y1 = rand() % (500 - ancho);
int x2 = rand() % (500 - ancho);
int y2 = rand() % (500 - ancho);
int x3 = rand() % (500 - ancho);
int y3 = rand() % (500 - ancho);
int x4 = rand() % (500 - ancho);
int y4 = rand() % (500 - ancho);
// Tiempo que los topos permanecerán en una posición antes de moverse (en ms)
int tiempoCambio = 700; // Puedes modificar este valor para ajustar la velocidad de movimiento
int cambioTiempo = 0;
bool juegoTerminado = false;
string mensajeResultado = "";
while (t != ESCAPE) {
borra();
rX = raton_x();
rY = raton_y();
// Actualizamos el tiempo
if (!juegoTerminado) {
cambioTiempo += 30;
// Cada `tiempoCambio` ms cambiamos la posición de los topos
if (cambioTiempo >= tiempoCambio) {
x1 = rand() % (500 - ancho); // Posición aleatoria para topo 1
y1 = rand() % (500 - ancho);
x2 = rand() % (500 - ancho); // Posición aleatoria para topo 2
y2 = rand() % (500 - ancho);
x3 = rand() % (500 - ancho); // Posición aleatoria para topo 3
y3 = rand() % (500 - ancho);
x4 = rand() % (500 - ancho); // Posición aleatoria para topo 4
y4 = rand() % (500 - ancho);
cambioTiempo = 0; // Reiniciar el contador de tiempo
appearanceCounter++; // Incrementar el contador de apariciones
}
color(BLANCO);
texto(10, 10, "X = " + toString(rX));
texto(10, 30, "Y = " + toString(rY));
texto(10, 50, "Count = " + toString(clickCounter));
texto(10, 70, "Appearances = " + toString(appearanceCounter)); // Mostrar el contador de apariciones
texto(10, 90, "Hits = " + toString(aciertos)); // Mostrar el contador de aciertos
if (raton_boton_izq()) {
color(ROJO);
circulo_lleno(rX, rY, 30);
clickCounter++;
// Detección de clic sobre el topo 1
if (rX >= x1 && rX <= x1 + ancho && rY >= y1 && rY <= y1 + ancho) {
aciertos++;
}
// Detección de clic sobre el topo 2
if (rX >= x2 && rX <= x2 + ancho && rY >= y2 && rY <= y2 + ancho) {
aciertos++;
}
// Detección de clic sobre el topo 3
if (rX >= x3 && rX <= x3 + ancho && rY >= y3 && rY <= y3 + ancho) {
aciertos++;
}
// Detección de clic sobre el topo 4
if (rX >= x4 && rX <= x4 + ancho && rY >= y4 && rY <= y4 + ancho) {
aciertos++;
}
}
// Si el contador de apariciones ha llegado a 5, finalizamos el juego
if (appearanceCounter >= 5) {
juegoTerminado = true;
if (aciertos >= 3) {
mensajeResultado = "GANASTE";
} else {
mensajeResultado = "PERDISTE";
}
}
}
// Dibuja los cuatro topos en sus posiciones aleatorias
if (!juegoTerminado) {
color(VERDE);
rectangulo_lleno(x1, y1, x1 + ancho, y1 + ancho);
rectangulo_lleno(x2, y2, x2 + ancho, y2 + ancho);
rectangulo_lleno(x3, y3, x3 + ancho, y3 + ancho);
rectangulo_lleno(x4, y4, x4 + ancho, y4 + ancho);
} else {
// Mostrar mensaje de resultado
color(BLANCO);
texto(200, 250, mensajeResultado);
}
refresca();
espera(30);
t = tecla();
}
vcierra();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment