-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinput-keys.c
64 lines (52 loc) · 2.26 KB
/
input-keys.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*******************************************************************************
* raylib [core] example - Gamepad input moves the ball
******************************************************************************/
#include <raylib.h>
int main()
{
// Initialization
const int screenWidth = 800;
const int screenHeight = 450;
// Establece que la pantalla utilice anti-aliasing para el suavizado de
// los bordes de los objetos.
SetConfigFlags(FLAG_MSAA_4X_HINT);
InitWindow(screenWidth, screenHeight, "Gamepad moves ball");
SetTargetFPS(60);
// Variables
// Creamos una variable que contenga las coordenadas de nuestro player y
// lo pocicionamos al centro de la pantalla.
Vector2 ballPosition = { (float)screenWidth/2, (float)screenHeight/2 };
// Main game loop
while(!WindowShouldClose())
{
// Update
// Detecta la entrada de las flechas de dirección del teclado y actualiza
// la posición del jugagor en base a las flecha presionadas
if(IsKeyDown(KEY_RIGHT)) ballPosition.x += 3.0f;
if(IsKeyDown(KEY_LEFT)) ballPosition.x -= 3.0f;
if(IsKeyDown(KEY_UP)) ballPosition.y -= 3.0f;
if(IsKeyDown(KEY_DOWN)) ballPosition.y += 3.0f;
// Valida si hay un mando (joystick) conectado y si lo hay detecta su
// entrada y cambia las coordenadas del jugagor en base al movimiento
// de sus flechas.
if(IsGamepadAvailable(0))
{
if(IsGamepadButtonDown(0, GAMEPAD_BUTTON_LEFT_FACE_RIGHT)) ballPosition.x += 3.0f;
if(IsGamepadButtonDown(0, GAMEPAD_BUTTON_LEFT_FACE_LEFT)) ballPosition.x -= 3.0f;
if(IsGamepadButtonDown(0, GAMEPAD_BUTTON_LEFT_FACE_UP)) ballPosition.y -= 3.0f;
if(IsGamepadButtonDown(0, GAMEPAD_BUTTON_LEFT_FACE_DOWN)) ballPosition.y += 3.0f;
}
// Draw
BeginDrawing();
// Establece en fondo de pantalla blanco hueso
ClearBackground(RAYWHITE);
// Dibuja al jugador en la posición que se esta actualizando
// en Update constantemente.
DrawCircleV(ballPosition, 50, MAROON);
EndDrawing();
}
// De-Initialization
CloseWindow();
// Finish the program
return 0;
}