Añadido disparo con espacio, proyectiles con dirección fija, objetivo de prueba y contador de impactos

This commit is contained in:
Clinv committed 2026-09-26 15:40:49 -03:00
1 parent 00814b07ad
commit dd1284fca0
1 file changed
+201 -21
+201 -21
View File
@@ -3,74 +3,120 @@
int main()
{
// Tamaño de la ventana
const int pantallaAncho = 800;
const int pantallaAlto = 600;
// Crear ventana
InitWindow(
pantallaAncho,
pantallaAlto,
"Attacks for Bombs 1"
);
// Limitar a 60 FPS
SetTargetFPS(60);
// Posición del jugador
// =========================
// JUGADOR
// =========================
float x = 400.0f;
float y = 300.0f;
// Tamaño del jugador
float tamaño = 50.0f;
// Velocidad del jugador
float velocidad = 300.0f;
// =========================
// DIRECCIÓN DEL JUGADOR
// =========================
float direccionX = 1.0f;
float direccionY = 0.0f;
// =========================
// PROYECTIL
// =========================
float proyectilX = 0.0f;
float proyectilY = 0.0f;
float proyectilAncho = 20.0f;
float proyectilAlto = 8.0f;
float proyectilVelocidad = 600.0f;
bool proyectilActivo = false;
// Dirección congelada del proyectil
float proyectilDireccionX = 0.0f;
float proyectilDireccionY = 0.0f;
// =========================
// ENEMIGO
// =========================
float enemigoX = 600.0f;
float enemigoY = 300.0f;
float enemigoTamaño = 50.0f;
// =========================
// CONTADOR
// =========================
int impactos = 0;
// =========================
// BUCLE PRINCIPAL
// =========================
while (!WindowShouldClose())
{
// Tiempo que pasó desde el frame anterior
float deltaTime = GetFrameTime();
// Posición del mouse
// =========================
// MOUSE
// =========================
Vector2 mouse = GetMousePosition();
// Dirección hacia el mouse
// Dirección del jugador hacia el mouse
float angulo =
atan2(mouse.y - y, mouse.x - x);
float direccionX = cos(angulo);
float direccionY = sin(angulo);
direccionX = cos(angulo);
direccionY = sin(angulo);
// =========================
// MOVIMIENTO
// =========================
// Movimiento hacia adelante
if (IsKeyDown(KEY_W))
{
x += direccionX * velocidad * deltaTime;
y += direccionY * velocidad * deltaTime;
}
// Movimiento hacia atrás
if (IsKeyDown(KEY_S))
{
x -= direccionX * velocidad * deltaTime;
y -= direccionY * velocidad * deltaTime;
}
// Movimiento hacia la izquierda
if (IsKeyDown(KEY_A))
{
x += direccionY * velocidad * deltaTime;
y -= direccionX * velocidad * deltaTime;
}
// Movimiento hacia la derecha
if (IsKeyDown(KEY_D))
{
x -= direccionY * velocidad * deltaTime;
y += direccionX * velocidad * deltaTime;
}
// Límites de la pantalla
// =========================
// LÍMITES DEL JUGADOR
// =========================
float mitad = tamaño / 2.0f;
if (x < mitad)
@@ -85,13 +131,148 @@ int main()
if (y > pantallaAlto - mitad)
y = pantallaAlto - mitad;
// Dibujar
// =========================
// DISPARAR
// =========================
if (IsKeyPressed(KEY_SPACE) && !proyectilActivo)
{
proyectilActivo = true;
// El proyectil aparece en el jugador
proyectilX = x;
proyectilY = y;
// Guardamos la dirección EXACTA
// que tenía el jugador al disparar
proyectilDireccionX = direccionX;
proyectilDireccionY = direccionY;
}
// =========================
// MOVIMIENTO DEL PROYECTIL
// =========================
if (proyectilActivo)
{
proyectilX +=
proyectilDireccionX *
proyectilVelocidad *
deltaTime;
proyectilY +=
proyectilDireccionY *
proyectilVelocidad *
deltaTime;
// =========================
// COLISIÓN CON ENEMIGO
// =========================
Rectangle proyectilRect =
{
proyectilX - proyectilAncho / 2.0f,
proyectilY - proyectilAlto / 2.0f,
proyectilAncho,
proyectilAlto
};
Rectangle enemigoRect =
{
enemigoX - enemigoTamaño / 2.0f,
enemigoY - enemigoTamaño / 2.0f,
enemigoTamaño,
enemigoTamaño
};
if (CheckCollisionRecs(
proyectilRect,
enemigoRect))
{
impactos++;
proyectilActivo = false;
}
// =========================
// SALIR DE LA PANTALLA
// =========================
if (
proyectilX < 0 ||
proyectilX > pantallaAncho ||
proyectilY < 0 ||
proyectilY > pantallaAlto
)
{
proyectilActivo = false;
}
}
// =========================
// DIBUJAR
// =========================
BeginDrawing();
// Fondo blanco
ClearBackground(WHITE);
// Dibujar jugador
// =========================
// CONTADOR
// =========================
DrawText(
TextFormat("Impactos: %d", impactos),
10,
10,
25,
BLACK
);
// =========================
// ENEMIGO
// =========================
DrawRectangle(
enemigoX - enemigoTamaño / 2.0f,
enemigoY - enemigoTamaño / 2.0f,
enemigoTamaño,
enemigoTamaño,
BLUE
);
// =========================
// PROYECTIL
// =========================
if (proyectilActivo)
{
float anguloProyectil =
atan2(
proyectilDireccionY,
proyectilDireccionX
) * RAD2DEG;
DrawRectanglePro(
{
proyectilX,
proyectilY,
proyectilAncho,
proyectilAlto
},
{
proyectilAncho / 2.0f,
proyectilAlto / 2.0f
},
anguloProyectil,
BLACK
);
}
// =========================
// JUGADOR
// =========================
DrawRectanglePro(
{
x,
@@ -110,7 +291,6 @@ int main()
EndDrawing();
}
// Cerrar ventana
CloseWindow();
return 0;