-2

I am making pong in c++ using raylib but when I debug it, it throws this error The preLaunchTask 'build debug' terminated with exit code 3221225477.How do I fix this.

This is my workspace

This is the code:

#include <iostream>
#include<vector>
#include <raylib.h>

using namespace std;

int p1_score, p2_score = 0;
const int max_score = 3;
const int font_size = 80;

class Paddle{

    public:
        float x_pos, y_pos;
        float width, height;
        int speed;
        Paddle(float x, float y, float wth, float hgt, int spd){
            x_pos = x;
            y_pos = y;
            width = wth;
            height = hgt;
            speed = spd;
        }
        void draw(){
            DrawRectangle(x_pos, y_pos, width, height, WHITE);
        }
        void move(int key_up, int key_down){
            if (IsKeyDown(key_up) && y_pos>=0)
            {
                y_pos -= speed;
            }
            if(IsKeyDown(key_down) && y_pos+height<=GetScreenHeight())
            {
                y_pos += speed;
            }
            
        }

};

class Ball{

    public:
        const int max_vel = 7;
        float x_pos, y_pos, original_x, original_y;
        int speed_x,speed_y;
        int radius;
        Ball(int x, int y, int radi){
            x_pos = original_x = x;
            y_pos = original_y = y;
            radius = radi;
            speed_x = max_vel;
            speed_y = 0;
        }
        void draw(){
            DrawCircle(x_pos, y_pos, radius, WHITE);
        }
        void move(int screen_width, int screen_height, Paddle left_paddle, Paddle right_paddle){
            x_pos+=speed_x;
            y_pos+=speed_y;
            if (y_pos + radius >= screen_height || y_pos - radius <= 0)
            {
                speed_y*=-1;
            }
            // if (x_pos + radius >= screen_width || x_pos - radius <= 0)
            // {
            //     speed_x*=-1;
            // }
            if (speed_x < 0){
                if(y_pos >= left_paddle.y_pos && y_pos <= left_paddle.y_pos + left_paddle.height ){
                    if (x_pos - radius <= left_paddle.x_pos + left_paddle.width ){
                        speed_x *= -1;
                        float middle_y = left_paddle.y_pos+left_paddle.height/2;
                        float difference_in_y = middle_y-y_pos;
                        float reduction_factor = (left_paddle.height/2)/max_vel;
                        float y_vel = difference_in_y/reduction_factor;
                        speed_y = -1*y_vel;
                    }
                }
            }else
            {
                if(y_pos >= right_paddle.y_pos && y_pos <= right_paddle.y_pos + right_paddle.height ){
                    if (x_pos + radius >= right_paddle.x_pos){
                        speed_x *= -1;
                        float middle_y = right_paddle.y_pos+right_paddle.height/2;
                        float difference_in_y = middle_y-y_pos;
                        float reduction_factor = (right_paddle.height/2)/max_vel;
                        float y_vel = difference_in_y/reduction_factor;
                        speed_y = -1*y_vel;
                    }
                }
            }
            
        }
        void reset(){
            x_pos=original_x;
            y_pos=original_y;
            speed_y=0;
            speed_x*=-1;
        }
};

void blit_score(int score, int x, int y){

    DrawText(TextFormat("%i", score), x, y, font_size, WHITE);
}

void is_won(int x, int y){
    
    if (p1_score == max_score)
    {
        DrawText("PLAYER 1 WON!", x, y, font_size-20, WHITE);
    }else if (p2_score == max_score)
    {
        DrawText("PLAYER 2 WON!", x, y, font_size, WHITE);
    }
    
    
}

int main () {

    const int screenWidth = 800;
    const int screenHeight = 600;

    Ball ball(screenWidth/2, screenHeight/2, 15);
    Paddle p1(10, screenHeight/2-60, 20, 120, 7),p2(screenWidth-30, screenHeight/2-60, 20, 120, 7);

    cout << "Hello World" << endl;

    InitWindow(screenWidth, screenHeight, "Pong");
    SetTargetFPS(60);

    while (WindowShouldClose() == false){
        BeginDrawing();

        ball.move(screenWidth, screenHeight, p1, p2);
        p1.move(KEY_W, KEY_S);
        p2.move(KEY_UP, KEY_DOWN);

        if (ball.x_pos < 0)
        {
            p2_score++;
            ball.reset();
        }else if (ball.x_pos > screenWidth)
        {
            p1_score++;
            ball.reset();
        }
        blit_score(p1_score, screenWidth/2-100, screenHeight/2);
        blit_score(p2_score, screenWidth/2+100, screenHeight/2);
        is_won(screenWidth/2-100, 20);
        

        ClearBackground(BLACK);
        DrawLine(screenWidth/2, 0, screenWidth/2, screenHeight, WHITE);
        ball.draw();
        p1.draw();
        p2.draw();
        EndDrawing();
    }

    CloseWindow();
    return 0;
}

It gives me option of show errors.But when I click show errors it says 'No problems have been detected in the workspace'.I figured out that when I restart my computer or when I switch on my computer it debugs for the first time after restarting but after the first time it again shows this error.I searched it online but found nothing.

  • I doubt that your code is the reason, rather check the configuration of your IDE/Project – πάντα ῥεῖ Aug 26 '23 at 16:04
  • And when you used your debugger to run this code, what did you see? This is what a debugger is for and if you don't know how to use it, this is a good opportunity to learn running your program one line at a time in a debugger, monitoring all variables and their values as they change and analysing your program's logic and execution. It should be possible for you to use your debugger to find all simple problems in this and all future programs you write, all by yourself. Do you know how to use a debugger? Knowing how to effectively use a debugger is a required skill for every C++ developer. – Sam Varshavchik Aug 26 '23 at 16:05
  • If you try to just build, does it fail? Then it's either an error in your code or in your `tasks.json` file. If building works, but launching/running does not then it's likely your `launch.json` file that wrong. If the program starts, but crashes, then it's back you a problem in your code. – Some programmer dude Aug 26 '23 at 16:06
  • 1
    3221225477 is 0xC0000005 in hexadecimal, which is NTSTATUS code `STATUS_ACCESS_VIOLATION` – 273K Aug 26 '23 at 16:06
  • @273K So, how can I fix this – Sarthak Yadav Aug 26 '23 at 16:25
  • 2
    Use a more friendly IDE Visual Studio 2022 Community. – 273K Aug 26 '23 at 16:28
  • For crashes in your own programs, use a [*debugger'](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) to catch them, and locate when and where in your code it happens. Examine variables and their values at the time and location of the crash, to see if they all make sense. – Some programmer dude Aug 26 '23 at 16:34
  • @273K I have windows 7 which does not support newer versions of vs code.I can't do anything I guess until I buy a new laptop but thanks – Sarthak Yadav Aug 26 '23 at 16:36
  • 1
    VS Code? I talked about VS, not code. Use Visual Studio 2019 Community on Windows 7. https://learn.microsoft.com/en-us/visualstudio/releases/2019/system-requirements – 273K Aug 26 '23 at 16:43
  • what is the prelaunch task that is crashing? Is there any terminal output? – Alan Birtles Aug 26 '23 at 18:49

0 Answers0