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 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.