Movimiento WASD adaptado a la dirección del mouse
This commit is contained in:
1 parent
d74834f0b6
commit
00814b07ad
3 files changed
+179
-27
No files matched your search
File renamed without changes.
@@ -0,0 +1,117 @@
|
|||||||
|
#include "raylib.h"
|
||||||
|
#include <cmath>
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
+62
-27
@@ -3,59 +3,95 @@
|
|||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
// Crear la ventana
|
// Tamaño de la ventana
|
||||||
InitWindow(800, 600, "Attacks for Bombs 1");
|
const int pantallaAncho = 800;
|
||||||
|
const int pantallaAlto = 600;
|
||||||
|
|
||||||
// Variables del jugador
|
// Crear ventana
|
||||||
float x = 400;
|
InitWindow(
|
||||||
float y = 300;
|
pantallaAncho,
|
||||||
float tamaño = 50;
|
pantallaAlto,
|
||||||
float velocidad = 5;
|
"Attacks for Bombs 1"
|
||||||
|
);
|
||||||
|
|
||||||
// 60 cuadros por segundo
|
// Limitar a 60 FPS
|
||||||
SetTargetFPS(60);
|
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())
|
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))
|
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))
|
if (IsKeyDown(KEY_S))
|
||||||
{
|
{
|
||||||
y += velocidad;
|
x -= direccionX * velocidad * deltaTime;
|
||||||
|
y -= direccionY * velocidad * deltaTime;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Movimiento hacia la izquierda
|
// Movimiento hacia la izquierda
|
||||||
if (IsKeyDown(KEY_A))
|
if (IsKeyDown(KEY_A))
|
||||||
{
|
{
|
||||||
x -= velocidad;
|
x += direccionY * velocidad * deltaTime;
|
||||||
|
y -= direccionX * velocidad * deltaTime;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Movimiento hacia la derecha
|
// Movimiento hacia la derecha
|
||||||
if (IsKeyDown(KEY_D))
|
if (IsKeyDown(KEY_D))
|
||||||
{
|
{
|
||||||
x += velocidad;
|
x -= direccionY * velocidad * deltaTime;
|
||||||
|
y += direccionX * velocidad * deltaTime;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Obtener la posición del mouse
|
// Límites de la pantalla
|
||||||
Vector2 mouse = GetMousePosition();
|
float mitad = tamaño / 2.0f;
|
||||||
|
|
||||||
// Calcular el ángulo hacia el mouse
|
if (x < mitad)
|
||||||
float angulo =
|
x = mitad;
|
||||||
atan2(mouse.y - y, mouse.x - x) * RAD2DEG;
|
|
||||||
|
|
||||||
// Empezar a dibujar
|
if (x > pantallaAncho - mitad)
|
||||||
|
x = pantallaAncho - mitad;
|
||||||
|
|
||||||
|
if (y < mitad)
|
||||||
|
y = mitad;
|
||||||
|
|
||||||
|
if (y > pantallaAlto - mitad)
|
||||||
|
y = pantallaAlto - mitad;
|
||||||
|
|
||||||
|
// Dibujar
|
||||||
BeginDrawing();
|
BeginDrawing();
|
||||||
|
|
||||||
// Fondo blanco
|
// Fondo blanco
|
||||||
ClearBackground(WHITE);
|
ClearBackground(WHITE);
|
||||||
|
|
||||||
// Dibujar el cuadrado rojo girado hacia el mouse
|
// Dibujar jugador
|
||||||
DrawRectanglePro(
|
DrawRectanglePro(
|
||||||
{
|
{
|
||||||
x,
|
x,
|
||||||
@@ -64,18 +100,17 @@ int main()
|
|||||||
tamaño
|
tamaño
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
tamaño / 2,
|
mitad,
|
||||||
tamaño / 2
|
mitad
|
||||||
},
|
},
|
||||||
angulo,
|
angulo * RAD2DEG,
|
||||||
RED
|
RED
|
||||||
);
|
);
|
||||||
|
|
||||||
// Terminar de dibujar
|
|
||||||
EndDrawing();
|
EndDrawing();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Cerrar la ventana
|
// Cerrar ventana
|
||||||
CloseWindow();
|
CloseWindow();
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
Reference in new issue
Block a user