Shoot Cadence

edited December 2015 in raylib: core
Hi, i have a couple of questions, if someone can help me...

Well. First of all i'm working in a project where my player have to shoot, but i have problems whit the cadence of the shoot.
and my other question is: if i stop to update(the checkCollisions and the speed) and draw the variable this disappear from my windows but the program continuos take into account that this variable was in the point where i send to update?

My actual code:

#include "raylib.h"

Rectangle Bullet(Rectangle player);

int main()
{
// Initialization
//--------------------------------------------------------------------------------------
int screenWidth = 800;
int screenHeight = 450;

InitWindow(screenWidth, screenHeight, "raylib [core] example - basic window");

int numBullets = 0;
Rectangle player = { 10, screenHeight/2, 20, 20};
Rectangle enemy = { 700, 0, 20, 450};
Rectangle bullets[100];
bool shoot[100];
int framesCounter = 0;

int numBulletsShoot = 0;

for (int i = 0; i < numBullets; i++) shoot[i] = false;

SetTargetFPS(60);
//--------------------------------------------------------------------------------------

// Main game loop
while (!WindowShouldClose())
{
// Update
//----------------------------------------------------------------------------------
framesCounter ++;

if (IsKeyDown('X'))
{
numBullets ++;

for (int i = numBulletsShoot; i < numBullets; i++)
{
if ((int)((float)framesCounter/1)%2)bullets[i] = Bullet(player);
if ((int)((float)framesCounter/1)%2)shoot[i] = true;
if ((int)((float)framesCounter/1)%2)numBulletsShoot ++;

/*if ((int)((float)framesCounter/1)%2)shoot[i] = false;
if ((int)((float)framesCounter/1)%2)shoot[i] = false;*/
}
}

for (int i = 0; i < numBullets; i++)
{
bullets[i].x +=5;
}

for (int i = 0; i < numBullets; i++)
{
if (CheckCollisionRecs(bullets[i], enemy)) shoot[i] = false;
}
//----------------------------------------------------------------------------------

// Draw
//----------------------------------------------------------------------------------
BeginDrawing();

ClearBackground(RAYWHITE);

DrawRectangleRec( player, RED);
DrawRectangleRec( enemy, GREEN);

for (int i = 0; i < numBullets; i++)
{
if(shoot[i]) DrawRectangleRec( bullets[i], BLUE);
}

EndDrawing();
//----------------------------------------------------------------------------------
}

// De-Initialization
//--------------------------------------------------------------------------------------
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------

return 0;
}

Rectangle Bullet( Rectangle player)
{
Rectangle bullet = { player.x, player.y + player.height/2, 5, 5};

return bullet;
}

Comments

  • edited December 2015
    Hi DtBraulio,

    I just edited your post to add the latest version (instead of two posts) and to add code formating.

    To format code just use the tag < pre > at code beginning and </ pre > at code end. Just check the post with edit to see it!
  • edited December 2015
    About your question: your code looks a bit complicated to me... Here it is a better way to do it:

    #include "raylib.h"

    #define MAX_BULLETS 20

    int main()
    {
    // Initialization
    //--------------------------------------------------------------------------------------
    int screenWidth = 800;
    int screenHeight = 450;

    InitWindow(screenWidth, screenHeight, "raylib example - bullets shoot");

    Rectangle player = { 10, screenHeight/2, 20, 20 };
    Rectangle enemy = { 700, 0, 20, screenHeight };

    Rectangle bullets[MAX_BULLETS];
    bool bulletActive[MAX_BULLETS];

    for (int i = 0; i < MAX_BULLETS; i++)
    {
    bullets[i].x = 0;
    bullets[i].y = 0;
    bullets[i].width = 5;
    bullets[i].height = 5;

    bulletActive[i] = false;
    }

    int framesCounter = 0;

    SetTargetFPS(60);
    //--------------------------------------------------------------------------------------

    // Main game loop
    while (!WindowShouldClose())
    {
    // Update
    //----------------------------------------------------------------------------------
    // Shooting logic
    if (IsKeyDown(KEY_SPACE))
    {
    framesCounter++;

    if (framesCounter > 15) // Shooting cadence
    {
    // Every 15 frames we activate a shoot from the array
    // We activate the first un-active shoot
    for (int i = 0; i < MAX_BULLETS; i++)
    {
    if (!bulletActive[i])
    {
    bullets[i].x = player.x + player.width/2;
    bullets[i].y = player.y + player.height/2;

    bulletActive[i] = true;
    break;
    }
    }

    framesCounter = 0;
    }
    }
    else framesCounter = 14;

    // Bullets logic
    for (int i = 0; i < MAX_BULLETS; i++)
    {
    if (bulletActive[i])
    {
    // Update bullets movement
    bullets[i].x += 6;

    // Check bullet collision with enemy or out of screen to deactivate
    if (CheckCollisionRecs(bullets[i], enemy) || (bullets[i].x > screenWidth))
    {
    bulletActive[i] = false;
    }
    }
    }

    // Player movement logic
    if (IsKeyDown(KEY_UP)) player.y -= 8;
    else if (IsKeyDown(KEY_DOWN)) player.y += 8;

    if (player.y <= 0) player.y = 0;
    else if ((player.y + player.height) >= screenHeight) player.y = screenHeight - player.height;
    //----------------------------------------------------------------------------------

    // Draw
    //----------------------------------------------------------------------------------
    BeginDrawing();

    ClearBackground(RAYWHITE);

    DrawRectangleRec(player, RED);
    DrawRectangleRec(enemy, GREEN);

    for (int i = 0; i < MAX_BULLETS; i++)
    {
    if (bulletActive[i]) DrawRectangleRec(bullets[i], BLUE);
    }

    EndDrawing();
    //----------------------------------------------------------------------------------
    }

    // De-Initialization
    //--------------------------------------------------------------------------------------
    CloseWindow(); // Close window and OpenGL context
    //--------------------------------------------------------------------------------------

    return 0;
    }
Sign In or Register to comment.