Files
Attacks-for-Bombs-1/source/A4B1/main.cpp
T

297 lines
6.6 KiB
C++

#include "raylib.h"
#include <cmath>
int main()
{
const int pantallaAncho = 800;
const int pantallaAlto = 600;
InitWindow(
pantallaAncho,
pantallaAlto,
"Attacks for Bombs 1"
);
SetTargetFPS(60);
// =========================
// JUGADOR
// =========================
float x = 400.0f;
float y = 300.0f;
float tamaño = 50.0f;
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())
{
float deltaTime = GetFrameTime();
// =========================
// MOUSE
// =========================
Vector2 mouse = GetMousePosition();
// Dirección del jugador hacia el mouse
float angulo =
atan2(mouse.y - y, mouse.x - x);
direccionX = cos(angulo);
direccionY = sin(angulo);
// =========================
// MOVIMIENTO
// =========================
if (IsKeyDown(KEY_W))
{
x += direccionX * velocidad * deltaTime;
y += direccionY * velocidad * deltaTime;
}
if (IsKeyDown(KEY_S))
{
x -= direccionX * velocidad * deltaTime;
y -= direccionY * velocidad * deltaTime;
}
if (IsKeyDown(KEY_A))
{
x += direccionY * velocidad * deltaTime;
y -= direccionX * velocidad * deltaTime;
}
if (IsKeyDown(KEY_D))
{
x -= direccionY * velocidad * deltaTime;
y += direccionX * velocidad * deltaTime;
}
// =========================
// LÍMITES DEL JUGADOR
// =========================
float mitad = tamaño / 2.0f;
if (x < mitad)
x = mitad;
if (x > pantallaAncho - mitad)
x = pantallaAncho - mitad;
if (y < mitad)
y = mitad;
if (y > pantallaAlto - mitad)
y = pantallaAlto - mitad;
// =========================
// 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();
ClearBackground(WHITE);
// =========================
// 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,
y,
tamaño,
tamaño
},
{
mitad,
mitad
},
angulo * RAD2DEG,
RED
);
EndDrawing();
}
CloseWindow();
return 0;
}