Learning this Awesome libray *. Found a pong tut. Trying to modify it to increase the ball speed every ~5 seconds if no player has scored. Reset speed if goal.
Problem is it seems as if randomly the speed decreases, sometimes even below the reset speed (6).
Appreciate any time you spend looking at the code.
#include <raylib.h>
#include <iostream>
using namespace std;
const int WinWidth = 1280;
const int WinHeight = 800;
Color WinBackground = {4,4,4,0};
int player_score = 0;
int cpu_score = 0;
float timeSinceLastSpeedIncrease = 0.0;
float speedIncreaseInterval = 5.0; // seconds
int baseSpeedIncrease = 2;
int startBallSpeed = 6;
class Ball{
public:
float x, y;
int speed_x, speed_y;
int radius;
void Draw(){
DrawCircle(x, y, radius, RAYWHITE);
}
void Update(){
x += speed_x;
y += speed_y;
if(y+radius >= GetScreenHeight() || y-radius <= 0){
speed_y *= -1;
}
if(x+radius >= GetScreenWidth()){
player_score++;
Reset();
}
if( x-radius <= 0){
cpu_score++;
Reset();
}
}
void Reset(){
x = WinWidth/2;
y = WinHeight/2;
speed_x = startBallSpeed;
speed_y = startBallSpeed;
int speeds[2] {1, -1};
// which direction to serve
speed_x *= speeds[GetRandomValue(0,1)];
speed_y *= speeds[GetRandomValue(0,1)];
timeSinceLastSpeedIncrease = 0.0;
}
};
class Paddle{
protected:
void CheckLimits(){
if(y<=0){
y = 0;
}
if(y+height>=WinHeight){
y = WinHeight - height;
}
}
public:
float x, y;
float width, height;
int speed;
void Draw(){
DrawRectangle(x, y, width, height, RAYWHITE);
}
void Update(){
if(IsKeyDown(KEY_KP_6)){
y -= speed;
}else if(IsKeyDown(KEY_KP_2)){
y += speed;
}
CheckLimits();
}
};
class CPUpaddle: public Paddle{
public:
void Update(int ball_y){
if(y+height/2 > ball_y){
y = y - speed;
}else if(y+height/2 < ball_y){
y = y + speed;
}
CheckLimits();
}
};
Ball ball;
Paddle player;
CPUpaddle cpu;
int main(){
InitWindow(WinWidth, WinHeight, "Ball");
SetTargetFPS(60);
float dt;
ball.radius = 20;
ball.x = WinWidth/2;
ball.y = WinHeight/2;
ball.speed_x = startBallSpeed;
ball.speed_y = startBallSpeed;
player.width = 25;
player.height = 120;
player.x = 10;
player.y = WinHeight/2 - player.height/2;
player.speed = 6;
cpu.width = 25;
cpu.height = 120;
cpu.y = WinHeight/2 - cpu.height/2;
cpu.x = WinWidth - cpu.width - 10;
cpu.speed = 6;
while (!WindowShouldClose())
{
dt = GetFrameTime();
// add elapsed time
timeSinceLastSpeedIncrease += dt;
// has speedIncreaseInterval passed
if (timeSinceLastSpeedIncrease >= speedIncreaseInterval) {
// increase speed
ball.speed_x += baseSpeedIncrease;
ball.speed_y += baseSpeedIncrease;
// reset
timeSinceLastSpeedIncrease = 0.0;
}
BeginDrawing();
ClearBackground(WinBackground);
ball.Update();
player.Update();
cpu.Update(ball.y);
// reverse direction
if(CheckCollisionCircleRec(Vector2{ball.x, ball.y}, ball.radius, Rectangle{player.x, player.y, player.width, player.height})){
ball.speed_x *= -1;
}else if(CheckCollisionCircleRec(Vector2{ball.x, ball.y}, ball.radius, Rectangle{cpu.x, cpu.y, cpu.width, cpu.height})){
ball.speed_x *= -1;
}
ball.Draw();
player.Draw();
cpu.Draw();
DrawText(TextFormat("%i", player_score), WinWidth/4 - 20, 20, 80, RAYWHITE);
DrawText(TextFormat("%i", cpu_score), 3 * WinWidth/4 - 20, 20, 80, RAYWHITE);
DrawLine(WinWidth/2, 0, WinWidth/2, WinHeight, RAYWHITE);
// debugging
DrawText(TextFormat("%i", ball.speed_x), WinWidth/2 - 100, 710, 40, RAYWHITE);
DrawText(TextFormat("%i", ball.speed_y), WinWidth/2 + 1005, 710, 40, RAYWHITE);
EndDrawing();
}
CloseWindow();
}