Axe Game Results, Thoughts & Things

Hi Folks,

Just sharing what I ended up with while doing the Axe Game section of the C++ Fundamentals course. Room for refactoring and some redundant stuff - was just aiming to nail into my head all these foundational concepts and see what I could break and fix along the way. My knowledge of C++ is rough and shallow, hoping to follow through with the course and keep going beyond it with a more solidified understanding of syntax and concepts. Thank You!

On another note - VSCode keeps not being able to find raylib.h. I do have it in another directory and have updated c_cpp_properties.json etc, yet the red squiggles keep popping up intermittently. Everything still compiles and runs as expected, yet oddly - changing the line that the #include is on seems to work for a little while. Something with the prediction settings maybe? Not sure.

Code
#include "raylib.h"
int main()
{
    // program window size
    int widthGameWindow{800};
    int heightGameWindow{450};

    // start window and set title, set fps
    InitWindow(widthGameWindow, heightGameWindow, "Death By Square by TPSCZ");
    SetTargetFPS(60);

    // circle pos x, pos y, size, speed
    int circleX{200};
    int circleY{200};
    int circleR{25};
    int moveSpeed{2};
    // circle bounds
    int lcircleX{circleX - circleR};
    int rcircleX{circleX + circleR};
    int ucircleY{circleY - circleR};
    int bcircleY{circleY + circleR};

    // rectangle pos x, pos y, size, speed
    int recX{300};
    int recY{0};
    int recSize{50};
    int recDirectionX{2};
    int recDirectionY{-2};
    // rectangle bounds (l eft, r ight, u pper, b ottom)
    int lrecX{recX};
    int rrecX{recX + recSize};
    int urecY{recY};
    int brecY{recY + recSize};

    // collision bounds logic
    bool collisionWithRec = (brecY >= ucircleY)
                         && (urecY <= bcircleY)
                         && (lrecX <= rcircleX)
                         && (rrecX >= lcircleX);

    // disallow moving off edge of screen if at limit
    bool edgeXpos{true};
    bool edgeXneg{true};
    bool edgeYpos{true};
    bool edgeYneg{true};

    // move more makes square go faster and display level change
    int moved{0};
    bool faster1{false};
    bool faster2{false};

    // main program
    while (!WindowShouldClose())
    {
        // DRAW FIRST SCEENE
        BeginDrawing();
        ClearBackground(WHITE); // bg color

        
        if (collisionWithRec) {
            DrawText("Game Over!\n Press HOME to Restart!", 50, 50, 20, RED);
            if (IsKeyPressed(KEY_HOME)) {
                recX = 300; recY = 0; recDirectionX = 2; recDirectionY = -2;
                circleX = 200; circleY = 200; moved = 0;
                faster1 = false; faster2 = false;
                collisionWithRec = false;
            }
        }
        else {
            // update collision edges
            lcircleX = circleX - circleR; rcircleX = circleX + circleR;
            ucircleY = circleY - circleR; bcircleY = circleY + circleR;
            lrecX = recX; rrecX = recX + recSize;
            urecY = recY; brecY = recY + recSize;
            // check collision
            collisionWithRec = (brecY >= ucircleY)
                            && (urecY <= bcircleY)
                            && (lrecX <= rcircleX)
                            && (rrecX >= lcircleX);

            // show game pieces
            DrawCircle(circleX, circleY, circleR, BLUE); // circle color
            DrawRectangle(recX, recY, recSize, recSize, GREEN); // rec color

            // increase speed the more the player moves
            // FIXED with !IsKeyDown @ // WASD movement
            // FIXED side effect: moved increases even if not moving while oposites pressed (ie A & D or W & S or all)
            if (!faster1) {DrawText("Level 0", 50, 50, 20, GREEN);}
            if (moved > 500 && !faster1) {
                recDirectionX *= 2;
                recDirectionY *= 2;
                faster1 = true;
            }
            if (faster1 && !faster2) {DrawText("Level 1", 50, 50, 20, GREEN);}
            if (moved > 1000 && !faster2) {
                recDirectionX *= 2;
                recDirectionY *= 2;
                faster2 = true;
            }
            if (faster2) {DrawText("Level 2", 50, 50, 20, GREEN);}
            // auto move rec, reverse at edge
            recY += recDirectionY;
            if (recY > heightGameWindow || recY < 0) { recDirectionY = -recDirectionY; }
            recX += recDirectionX;
            if (recX > widthGameWindow || recX < 0) { recDirectionX = -recDirectionX; }

            // WASD movement for circle, allowed if not at respective edge then set edge bools(improve?)
            // up, decrease Y
            if (IsKeyDown(KEY_W) && edgeYneg && !IsKeyDown(KEY_S)) { circleY -= moveSpeed; moved += 1; }
            edgeYneg = circleY > 0;
            // down, increase Y
            if (IsKeyDown(KEY_S) && edgeYpos && !IsKeyDown(KEY_W)) { circleY += moveSpeed; moved += 1; }
            edgeYpos = circleY < heightGameWindow;
            // left, decrease X
            if (IsKeyDown(KEY_A) && edgeXneg && !IsKeyDown(KEY_D)) { circleX -= moveSpeed; moved += 1; }
            edgeXneg = circleX > 0;
            // right, increase X
            if (IsKeyDown(KEY_D) && edgeXpos && !IsKeyDown(KEY_A)) { circleX += moveSpeed; moved += 1; }
            edgeXpos = circleX < widthGameWindow;
            
        }
        EndDrawing();
    }
}

So long as the code compiles, you shouldn’t worry about the squiggles (as much as this is off-putting). Intellisense is notoriously bad at keeping track of stuff. Even with the default install location of raylib.

Privacy & Terms