1

I was working in a University C project (first sem), in which our group wanted to make a simple pong game, we decided to use raylib, as it seemed easy. But here is the problem, in the following code:

void UpdatePad(Pad* pad)
{
    int height = GetScreenHeight();
    if (IsKeyDown(pad->Scheme.DownButton)) {
        printf("Down =  %d\n", pad->Scheme.DownButton);
        pad->Position.y += GetFrameTime() * pad->Speed;
            if ( pad->Position.y + pad->Size.y/2 > height ) {
                pad->Position.y = height - pad->Size.y /2;
            }
    }
    if (IsKeyDown(pad->Scheme.UpButton)) {
        printf("Up = %d\n", pad->Scheme.UpButton);
        pad->Position.y -= GetFrameTime() * pad -> Speed;
            if (pad->Position.y - pad->Size.y/2 < 0 ) {
                pad->Position.y = pad->Size.y /2;
            }
    }
}

The function IsKeyDown of raylib always returns true, whatever I do. Even if I replace pad->Scheme.DownButton to KEY_UP or something, it always returns true and executes the code block, Is there any solution for this?

Full script is:

#include <raylib.h>
#include <stdio.h> 
#include "pad.h"
#include "main.h"

void DrawPad(Pad* pad);

void UpdatePad(Pad* pad);

void Update();

void DrawUpdate();

void Loop();


int main()
{
    int screenWidth = 800;
    int screenHeight = 450;
    InitWindow(screenWidth, screenHeight, "Table Tennis");
    SetTargetFPS(12);

    Loop();
    return 0;
}



void Loop()
{
        while (!WindowShouldClose()) {
                DrawUpdate();
        }
}


void UpdatePad(Pad* pad)
{
    int height = GetScreenHeight();
    if (IsKeyDown(pad->Scheme.DownButton)) {
        printf("Down =  %d\n", pad->Scheme.DownButton);
        pad->Position.y += GetFrameTime() * pad->Speed;
            if ( pad->Position.y + pad->Size.y/2 > height ) {
                pad->Position.y = height - pad->Size.y /2;
            }
    }
    if (IsKeyDown(pad->Scheme.UpButton)) {
        printf("Up = %d\n", pad->Scheme.UpButton);
        pad->Position.y -= GetFrameTime() * pad -> Speed;
            if (pad->Position.y - pad->Size.y/2 < 0 ) {
                pad->Position.y = pad->Size.y /2;
            }
    }
}

void DrawPad(Pad* pad)
{
    DrawRectangle(pad->Position.x, pad->Position.y - (pad->Size.y /2), pad->Size.x, pad->Size.y, WHITE);
}



void DrawUpdate()
{
    const char* scoreLeft = TextFormat("%d", 10);
    int scoreSizeLeft = MeasureText(scoreLeft, 20);
    InputScheme Input = { .DownButton = KEY_S, .UpButton = KEY_W };
    Vector2 paddySize = { .x = 5, .y = 50 };
    Vector2 paddyPos = { .x = GetScreenWidth() - paddySize.x , .y = GetScreenHeight() - paddySize.y };
    Pad pad = { .Size = paddySize, .Speed = 50, .Scheme = Input ,  .Position = paddyPos };
    Vector2 from = {.x = (GetScreenWidth() / (float) 2), .y = 5};
    Vector2 to = { .x = (GetScreenWidth() / (float) 2), .y = ( GetScreenHeight() - (float) 5 ) };


    UpdatePad(&pad);
    BeginDrawing();
            ClearBackground(BLACK);
            DrawLineEx(from, to, 2, LIGHTGRAY);
            DrawText(scoreLeft, (GetScreenWidth()/2) - 10 -scoreSizeLeft, 10, 20, LIGHTGRAY);
            DrawPad(&pad);
    EndDrawing();
}

The pad is:-

#include <raylib.h>

typedef struct {
    int UpButton;
    int DownButton;
} InputScheme;

typedef struct {
    InputScheme Scheme;
    int Score;
    float Speed;

    Vector2 Position;
    Vector2 Size;
} Pad;

  • Can you show us your main script ? – Adrien G. Aug 02 '21 at 09:44
  • okay, I will show it. – Darshan Koirala Aug 02 '21 at 09:51
  • 1
    Hi, which raylib version do you use? I found in github some issue related to IsKeyDown() function: https://github.com/raysan5/raylib/issues/379 – Slawek Aug 02 '21 at 10:17
  • 1
    You could try to put something simple to check if IsKeyDown() function works without too many structs involved: Like this: `while (!WindowShouldClose()) { if (IsKeyDown(KEY_UP)) //do something here; if (IsKeyDown(KEY_DOWN)) //do something here; BeginDrawning(); //something here; EndDrawning(); }` – Slawek Aug 02 '21 at 10:31
  • Hmm Thank You, It is bug, I think. It didn't work that way either. – Darshan Koirala Aug 02 '21 at 13:02

0 Answers0