From 00814b07ad92bf6929cd0f336644b233af2f6fb3 Mon Sep 17 00:00:00 2001 From: TEC-GO Date: Sat, 26 Sep 2026 15:34:32 -0300 Subject: [PATCH] =?UTF-8?q?Movimiento=20WASD=20adaptado=20a=20la=20direcci?= =?UTF-8?q?=C3=B3n=20del=20mouse?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/{Pacman => A4B1}/CMakeLists.txt | 0 source/A4B1/main.cpp | 117 +++++++++++++++++++++++++ source/Pacman/main.cpp | 89 +++++++++++++------ 3 files changed, 179 insertions(+), 27 deletions(-) rename source/{Pacman => A4B1}/CMakeLists.txt (100%) create mode 100644 source/A4B1/main.cpp diff --git a/source/Pacman/CMakeLists.txt b/source/A4B1/CMakeLists.txt similarity index 100% rename from source/Pacman/CMakeLists.txt rename to source/A4B1/CMakeLists.txt diff --git a/source/A4B1/main.cpp b/source/A4B1/main.cpp new file mode 100644 index 0000000..d1690b3 --- /dev/null +++ b/source/A4B1/main.cpp @@ -0,0 +1,117 @@ +#include "raylib.h" +#include + +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 + float x = 400.0f; + float y = 300.0f; + + // Tamaño del jugador + float tamaño = 50.0f; + + // Velocidad del jugador + float velocidad = 300.0f; + + while (!WindowShouldClose()) + { + // Tiempo que pasó desde el frame anterior + float deltaTime = GetFrameTime(); + + // Posición del mouse + Vector2 mouse = GetMousePosition(); + + // Dirección hacia el mouse + float angulo = + atan2(mouse.y - y, mouse.x - x); + + float direccionX = cos(angulo); + float direccionY = sin(angulo); + + // 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 + 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; + + // Dibujar + BeginDrawing(); + + // Fondo blanco + ClearBackground(WHITE); + + // Dibujar jugador + DrawRectanglePro( + { + x, + y, + tamaño, + tamaño + }, + { + mitad, + mitad + }, + angulo * RAD2DEG, + RED + ); + + EndDrawing(); + } + + // Cerrar ventana + CloseWindow(); + + return 0; +} \ No newline at end of file diff --git a/source/Pacman/main.cpp b/source/Pacman/main.cpp index c8ef834..d1690b3 100644 --- a/source/Pacman/main.cpp +++ b/source/Pacman/main.cpp @@ -3,59 +3,95 @@ int main() { - // Crear la ventana - InitWindow(800, 600, "Attacks for Bombs 1"); + // Tamaño de la ventana + const int pantallaAncho = 800; + const int pantallaAlto = 600; - // Variables del jugador - float x = 400; - float y = 300; - float tamaño = 50; - float velocidad = 5; + // Crear ventana + InitWindow( + pantallaAncho, + pantallaAlto, + "Attacks for Bombs 1" + ); - // 60 cuadros por segundo + // Limitar a 60 FPS SetTargetFPS(60); - // Bucle principal del juego + // Posición del 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; + while (!WindowShouldClose()) { - // Movimiento hacia arriba + // Tiempo que pasó desde el frame anterior + float deltaTime = GetFrameTime(); + + // Posición del mouse + Vector2 mouse = GetMousePosition(); + + // Dirección hacia el mouse + float angulo = + atan2(mouse.y - y, mouse.x - x); + + float direccionX = cos(angulo); + float direccionY = sin(angulo); + + // Movimiento hacia adelante if (IsKeyDown(KEY_W)) { - y -= velocidad; + x += direccionX * velocidad * deltaTime; + y += direccionY * velocidad * deltaTime; } - // Movimiento hacia abajo + // Movimiento hacia atrás if (IsKeyDown(KEY_S)) { - y += velocidad; + x -= direccionX * velocidad * deltaTime; + y -= direccionY * velocidad * deltaTime; } // Movimiento hacia la izquierda if (IsKeyDown(KEY_A)) { - x -= velocidad; + x += direccionY * velocidad * deltaTime; + y -= direccionX * velocidad * deltaTime; } // Movimiento hacia la derecha if (IsKeyDown(KEY_D)) { - x += velocidad; + x -= direccionY * velocidad * deltaTime; + y += direccionX * velocidad * deltaTime; } - // Obtener la posición del mouse - Vector2 mouse = GetMousePosition(); + // Límites de la pantalla + float mitad = tamaño / 2.0f; - // Calcular el ángulo hacia el mouse - float angulo = - atan2(mouse.y - y, mouse.x - x) * RAD2DEG; + if (x < mitad) + x = mitad; - // Empezar a dibujar + if (x > pantallaAncho - mitad) + x = pantallaAncho - mitad; + + if (y < mitad) + y = mitad; + + if (y > pantallaAlto - mitad) + y = pantallaAlto - mitad; + + // Dibujar BeginDrawing(); // Fondo blanco ClearBackground(WHITE); - // Dibujar el cuadrado rojo girado hacia el mouse + // Dibujar jugador DrawRectanglePro( { x, @@ -64,18 +100,17 @@ int main() tamaño }, { - tamaño / 2, - tamaño / 2 + mitad, + mitad }, - angulo, + angulo * RAD2DEG, RED ); - // Terminar de dibujar EndDrawing(); } - // Cerrar la ventana + // Cerrar ventana CloseWindow(); return 0;