Second rectangle

HI! I’m taking the C++ for Game Developer course and I just finished the “Axe Game” section. Since I wanted to put into practice what I studied during the lessons, I thought of adding a second rectangle, I even managed to make it move, only I can’t set the collisions. I tried them all, but when I insert the collisions for the second rectangle, those of the first rectangle are also disabled. I share the code here with my latest changes, I would like to know where I’m wrong:

#include "raylib.h"

int main()
{
    // Set the Window
    int Width = 1920;
    int Height = 1080;    
    InitWindow(Width, Height, "Axe Game");

    // Set the Circle and its edges
    int CircleX = 200;
    int CircleY = 200;
    int CircleRadius = 25;
    int RCircle = CircleX + CircleRadius;
    int LCircle = CircleX - CircleRadius;
    int DCircle = CircleY + CircleRadius;
    int UCircle = CircleY - CircleRadius;

    // Set the Axe and its edges
    int AxeX = 400;
    int AxeY = 0;
    int AxeLength = 50;
    int RAxe = AxeX + AxeLength;
    int LAxe = AxeX;
    int DAxe = AxeY + AxeLength;
    int UAxe = AxeY;

    // Set the Axe1 and its edges
    int Axe1X = 1000;
    int Axe1Y = 0;
    int Axe1Length = 50;
    int RAxe1 = Axe1X + Axe1Length;
    int LAxe1 = Axe1X;
    int DAxe1 = Axe1Y + Axe1Length;
    int UAxe1 = Axe1Y;
    
    // Set Actors' movement
    int Dir = 10;
    
    // Set Collisions
    bool Collisions =
        RAxe >= LCircle &&
        LAxe <= RCircle &&
        DAxe >= UCircle &&
        UAxe <= DCircle;
    
    bool Collisions1 =
        RAxe1 >= LCircle &&
        LAxe1 <= RCircle &&
        DAxe1 >= UCircle &&
        UAxe1 <= DCircle;

    // Game Logic
    SetTargetFPS(60);
    while (WindowShouldClose() == false)
    {
        BeginDrawing();

            ClearBackground(BLACK);

            // Game Dinamics
            if (Collisions)
            {
                DrawText("Game Over!", CircleX, CircleY, 100, RED);
            }
            else if (Collisions1)
            {
                DrawText("Game Over!", CircleX, CircleY, 100, RED);
            }
            else
            {
                // Update Edges and Collisions
                RCircle = CircleX + CircleRadius;
                LCircle = CircleX - CircleRadius;
                DCircle = CircleY + CircleRadius;
                UCircle = CircleY - CircleRadius;
                RAxe = AxeX + AxeLength;
                Collisions =
                    RAxe >= LCircle &&
                    LAxe <= RCircle &&
                    DAxe >= UCircle &&
                    UAxe <= DCircle;
                RAxe1 = Axe1X + Axe1Length;
Collisions1 =
                    RAxe1 >= LCircle &&
                    LAxe1 <= RCircle &&
                    DAxe1 >= UCircle &&
                    UAxe1 <= DCircle;
                                                   
                // Draw the Actors
                DrawCircle(CircleX, CircleY, CircleRadius, SKYBLUE);
                DrawRectangle(AxeX, AxeY, AxeLength, AxeLength, MAROON);
                DrawRectangle(Axe1X, Axe1Y, Axe1Length, Axe1Length, GREEN);
                
                // Set Rectangle's Movements
                AxeY += Dir;
                if (AxeY > Height || AxeY < 0)
                {
                    Dir = -Dir;
                }
                Axe1Y += Dir;
                if (Axe1Y > Height || Axe1Y < 0)
                {
                    Dir = -Dir;
                }
               
                // Set Circle's Impunts
                if (IsKeyDown(KEY_D) && CircleX < Width)
                {
                    CircleX += 10;
                }
                if (IsKeyDown(KEY_A) && CircleX > 0)
                {
                    CircleX -= 10;
                }
                if (IsKeyDown(KEY_S) && CircleY < Height)
                {
                    CircleY += 10;
                }
                if (IsKeyDown(KEY_W) && CircleY > 0)
                {
                    CircleY -= 10;
                }                                 
            
            }

        EndDrawing();
    
    }
    
}

You’re only updating one edge for each “Axe” rectangle.

RAxe = AxeX + AxeLength; //Axe #1

RAxe1 = Axe1X + Axe1Length; //Axe #2 

What you need to do is update all edges in your update loop.

RAxe = AxeX + AxeLength; //Axe #1
LAxe = AxeX;
DAxe = AxeY + AxeLength;
UAxe = AxeY;

RAxe1 = Axe1X + AxeLength; //Axe #2
LAxe1 = Axe1X;
DAxe1 = Axe1Y + AxeLength;
UAxe1 = Axe1Y;

This is the updated code:

#include "raylib.h"

int main()
{
    // Set the Window
    int Width = 1920;
    int Height = 1080;    
    InitWindow(Width, Height, "Axe Game");

    // Set the Circle and its edges
    int CircleX = 200;
    int CircleY = 200;
    int CircleRadius = 25;
    int RCircle = CircleX + CircleRadius;
    int LCircle = CircleX - CircleRadius;
    int DCircle = CircleY + CircleRadius;
    int UCircle = CircleY - CircleRadius;

    // Set the Axe and its edges
    int AxeX = 400;
    int AxeY = 0;
    int AxeLength = 50;
    int RAxe = AxeX + AxeLength;
    int LAxe = AxeX;
    int DAxe = AxeY + AxeLength;
    int UAxe = AxeY;

    // Set the Axe1 and its edges
    int Axe1X = 1000;
    int Axe1Y = 0;
    int Axe1Length = 50;
    int RAxe1 = Axe1X + Axe1Length;
    int LAxe1 = Axe1X;
    int DAxe1 = Axe1Y + Axe1Length;
    int UAxe1 = Axe1Y;
    
    // Set Actors' movement
    int Dir = 10;
    
    // Set Collisions
    bool Collisions =
        RAxe >= LCircle &&
        LAxe <= RCircle &&
        DAxe >= UCircle &&
        UAxe <= DCircle;
    
    bool Collisions1 =
        RAxe1 >= LCircle &&
        LAxe1 <= RCircle &&
        DAxe1 >= UCircle &&
        UAxe1 <= DCircle;

    // Game Logic
    SetTargetFPS(60);
    while (WindowShouldClose() == false)
    {
        BeginDrawing();

            ClearBackground(BLACK);

            // Game Dinamics
            if (Collisions)
            {
                DrawText("Game Over!", CircleX, CircleY, 100, RED);
            }
            else if (Collisions1)
            {
                DrawText("Game Over!", CircleX, CircleY, 100, RED);
            }
            else
            {
                // Update Edges and Collisions
                RCircle = CircleX + CircleRadius;
                LCircle = CircleX - CircleRadius;
                DCircle = CircleY + CircleRadius;
                UCircle = CircleY - CircleRadius;
                RAxe = AxeX + AxeLength; //Update collision edges for Axe #1
                LAxe = AxeX;
                DAxe = AxeY + AxeLength;
                UAxe = AxeY;
                Collisions = //Collision Test for Axe #1
                    RAxe >= LCircle &&
                    LAxe <= RCircle &&
                    DAxe >= UCircle &&
                    UAxe <= DCircle;
                RAxe1 = Axe1X + Axe1Length; //Update collision edges for Axe #2
                LAxe1 = Axe1X;
                DAxe1 = Axe1Y + Axe1Length;
                UAxe1 = Axe1Y;
                Collisions1 = //Collision Test for Axe #2
                    RAxe1 >= LCircle &&
                    LAxe1 <= RCircle &&
                    DAxe1 >= UCircle &&
                    UAxe1 <= DCircle;
                                                   
                // Draw the Actors
                DrawCircle(CircleX, CircleY, CircleRadius, SKYBLUE);
                DrawRectangle(AxeX, AxeY, AxeLength, AxeLength, MAROON);
                DrawRectangle(Axe1X, Axe1Y, Axe1Length, Axe1Length, GREEN);
                
                // Set Rectangle's Movements
                AxeY += Dir;
                if (AxeY > Height || AxeY < 0)
                {
                    Dir = -Dir;
                }
                Axe1Y += Dir;
                if (Axe1Y > Height || Axe1Y < 0)
                {
                    Dir = -Dir;
                }
               
                // Set Circle's Impunts
                if (IsKeyDown(KEY_D) && CircleX < Width)
                {
                    CircleX += 10;
                }
                if (IsKeyDown(KEY_A) && CircleX > 0)
                {
                    CircleX -= 10;
                }
                if (IsKeyDown(KEY_S) && CircleY < Height)
                {
                    CircleY += 10;
                }
                if (IsKeyDown(KEY_W) && CircleY > 0)
                {
                    CircleY -= 10;
                }                                 
            
            }

        EndDrawing();
    
    }
    
}
1 Like

That’s why! Thank you so much for your help, now it works! :blush:

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms