Hi,
I added some randomness into the game using GetRandomValue() which provides a random int between the two provided ints.
When I use this function the foreground disappears however when it is excluded it isnt.
Can anyone help find why this is the case?
#include “raylib.h”
struct animData
{
Rectangle rec;
Vector2 pos;
int frame;
float updateTime;
float runningTime;
};
// Create function for ground check
bool isOnGround(animData data, int windowHeight)
{
return data.pos.y >= windowHeight - data.rec.height;
}
// Create function to update animations
animData updateAnimData(animData data, float deltaTime, int frameCount)
{
data.runningTime += deltaTime;
if (data.runningTime >= data.updateTime)
{
data.runningTime = 0.0;
data.rec.x = data.frame * data.rec.width;
data.frame++;
if (data.frame > frameCount - 1)
{
data.frame = 0;
}
}
return data;
}
float backgroundScrolling(float xPos, int scrollSpeed, float deltaTime, Texture2D background)
{
xPos -= scrollSpeed * deltaTime;
if(xPos <= -background.width * 2)
{
xPos = 0;
}
return xPos;
}
int main()
{
// Create constant window dimensions
int windowDimensions[2] = {512,380};
// Initialize the window
InitWindow(windowDimensions[0], windowDimensions[1], "Dapper Dasher");
// Intialize gravity (pixels/second)
const int gravity{750};
// Intialize Nebua textures and variables
Texture2D nebula = LoadTexture("textures/12_nebula_spritesheet.png");
// Initialize the number of nebulae
const int numberOfNebulae = GetRandomValue(5,10);
// Initialize the size of the nebulae array
animData nebulae[numberOfNebulae];
// Populate the array using a for loop
for(int i = 0; i < numberOfNebulae; i++)
{
nebulae[i].rec.x = 0.0;
nebulae[i].rec.y = 0.0;
nebulae[i].rec.width = nebula.width/8;
nebulae[i].rec.height = nebula.height/8;
nebulae[i].pos.x = windowDimensions[0] + i * 300;
nebulae[i].pos.y = windowDimensions[1] - nebula.height/8;
nebulae[i].frame = 0;
nebulae[i].runningTime = 0.0;
nebulae[i].updateTime = 1.0/12.0;
}
int nebulaVelocity{-150}; // Pixels per second
float finishLine{nebulae[numberOfNebulae - 1].pos.x};
// Initialize texture and scarfy variables
Texture2D scarfy = LoadTexture("textures/scarfy.png");
animData scarfyData;
// Initialize sprites rectangle area
scarfyData.rec.width = scarfy.width/6;
scarfyData.rec.height = scarfy.height;
scarfyData.rec.x = 0;
scarfyData.rec.y = 0;
// Initialize pos
scarfyData.pos.x = windowDimensions[0]/2 - scarfyData.rec.width/2;
scarfyData.pos.y = windowDimensions[1] - scarfyData.rec.height;
// Initialize scarfy frame number for animation
scarfyData.frame = 0;
// Initialze updateTime for time between frames
scarfyData.updateTime= 1.0/12.0;
// Initialze runningTime for time between frames
scarfyData.runningTime = 0;
// Initialize air check
bool isInAir{false};
// Initialize velocity and gravity
int velocity{0};
int jumpVelocity{-500}; // Pixels per second
// Load background texture
Texture2D background = LoadTexture("textures/far-buildings.png");
float bgX;
int bgSpeed = 20;
// Load midground texture
Texture2D midground = LoadTexture("textures/back-buildings.png");
float mgX;
int mgSpeed = 40;
// Load foreground texture
Texture2D foreground = LoadTexture("textures/foreground.png");
float fgX;
int fgSpeed = 80;
// Set collision check to false
bool collision{};
// Set target FPS
SetTargetFPS(60);
while (!WindowShouldClose())
{
// Delta time (time since last frame)
const float deltaTime{GetFrameTime()};
// Start drawing
BeginDrawing();
ClearBackground(WHITE);
// Scroll the background
bgX = backgroundScrolling(bgX, bgSpeed, deltaTime, background);
// Draw the background
Vector2 background1Pos{bgX,0.0};
DrawTextureEx(background, background1Pos,0.0,2.0,WHITE);
// Draw second background
Vector2 background2Pos{bgX + background.width * 2};
DrawTextureEx(background, background2Pos, 0.0, 2.0, WHITE);
// Scroll the midground
mgX = backgroundScrolling(mgX, mgSpeed, deltaTime, midground);
// Draw the midground
Vector2 midground1Pos{mgX,0.0};
DrawTextureEx(midground, midground1Pos,0.0,2.0,WHITE);
// Draw second midground
Vector2 midground2Pos{mgX + midground.width * 2};
DrawTextureEx(midground, midground2Pos, 0.0, 2.0, WHITE);
// Scroll the foreground
fgX = backgroundScrolling(fgX, fgSpeed, deltaTime, foreground);
// Draw the foreground
Vector2 foreground1Pos{fgX,0.0};
DrawTextureEx(foreground, foreground1Pos,0.0,2.0,WHITE);
// Draw second foreground
Vector2 foreground2Pos{fgX + foreground.width * 2};
DrawTextureEx(foreground, foreground2Pos, 0.0, 2.0, WHITE);
if (isOnGround(scarfyData, windowDimensions[1]))
{
// Ground check
velocity = 0;
isInAir = false;
}
else
{
// Rectangle is in the air
// Apply gravity
velocity += gravity * deltaTime;
}
// Check if the Space key is pressed to start jump
if(IsKeyPressed(KEY_SPACE) && !isInAir)
{
velocity += jumpVelocity;
isInAir = true;
}
// Update nebulae positions in for loop
for(int i = 0; i < numberOfNebulae; i++)
{
nebulae[i].pos.x += nebulaVelocity * deltaTime;
}
// Update finsh line
finishLine += nebulaVelocity * deltaTime;
// Update scarfy position
scarfyData.pos.y += velocity * deltaTime;
if(!isInAir)
{
scarfyData = updateAnimData(scarfyData, deltaTime, 6);
}
// Update nebulae animatons in a for loop
for(int i = 0; i < numberOfNebulae; i++)
{
nebulae[i] = updateAnimData(nebulae[i], deltaTime, 8);
}
for (animData Nebula : nebulae)
{
float pad{50};
Rectangle nebRec
{
Nebula.pos.x + pad,
Nebula.pos.y + pad,
Nebula.rec.width - 2 * pad,
Nebula.rec.height - 2 * pad
};
Rectangle scarfyRec
{
scarfyData.pos.x,
scarfyData.pos.y,
scarfyData.rec.width,
scarfyData.rec.height
};
if (CheckCollisionRecs(nebRec, scarfyRec))
{
collision = true;
}
}
// Check for collision
if (!collision && finishLine > 0)
{
// Draw nebula in a for loop
for(int i = 0; i < numberOfNebulae; i++)
{
DrawTextureRec(nebula, nebulae[i].rec, nebulae[i].pos, BLUE);
}
// Draw scarfy
DrawTextureRec(scarfy, scarfyData.rec, scarfyData.pos, WHITE);
}
else if(collision && finishLine > 0)
{
// Lose the game
DrawText("GAME OVER!", 100, 150, 50, RED);
}
else
{
// Win the game
DrawText("YOU WON!", 150, 150, 50, WHITE);
}
DrawFPS(10,10);
// Stop drawing
EndDrawing();
}
UnloadTexture(nebula);
UnloadTexture(scarfy);
UnloadTexture(background);
UnloadTexture(midground);
UnloadTexture(foreground);
CloseWindow();
}