learning for loops

This commit is contained in:
2026-02-02 05:31:38 -05:00
parent 13d12ff3fe
commit c052921fb1

View File

@@ -45,37 +45,49 @@ int main() {
// Nebula anim
AnimData nebData{
/*AnimData nebData{
{0.0, 0.0, nebula.width / 8, nebula.height / 8}, // Rectangle rec
{windowArray[0], windowArray[1] - nebula.height / 8}, // Vector2 pos
0, // int frame
1.0 / 12.0, // float updateTime
0.0 // float runningTime
};
};*/
AnimData neb2Data{
/*AnimData neb2Data{
{0.0, 0.0, nebula.width / 8, nebula.height / 8}, // Rectangle rec
{windowArray[0] + 300, windowArray[1] - nebula.height / 8}, // Vector2 pos
0, // int frame
1.0 / 16.0, // float updateTime
0.0 // float runningTime
};
};*/
const int sizeofNebulae{6};
// Nebulea
AnimData nebulae[2]{};
AnimData nebulae[sizeofNebulae]{};
for (int i = 0; i < 2; i++)
for (int i = 0; i < sizeofNebulae; 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].rec.x = 0;
nebulae[i].rec.y = 0;
nebulae[i].pos.y = windowArray[1] - nebulae[i].rec.height;
nebulae[i].frame = 0;
nebulae[i].runningTime = 0.0;
nebulae[i].updateTime = 1.0 / 16.0;
nebulae[i].pos.x = windowArray[0] + i * 300;
}
/* Neblua Positions
nebulae[0].pos.x = windowArray[0];
nebulae[1].pos.x = windowArray[0] + 300;
nebulae[2].pos.x = windowArray[0] + 600;
nebulae[3].pos.x = windowArray[0] + 900;
nebulae[4].pos.x = windowArray[0] + 1200;
nebulae[5].pos.x = windowArray[0] + 1500;
*/
int nebVel{-200}; // Nebula x velocity (pixels per second)
// Animation Frame
@@ -119,11 +131,19 @@ int main() {
velocity += jumpVelocity; // Jump velocity
}
// Nebula movement
nebData.pos.x += nebVel * dT;
/*// Nebula movement
nebulae[0].pos.x += nebVel * dT;
// Nebula 2 movement
neb2Data.pos.x += nebVel * dT;
nebulae[1].pos.x += nebVel * dT;
*/
// Nebula movement loop
for (int i = 0; i < sizeofNebulae; i++)
{
// Nebula update position
nebulae[i].pos.x += nebVel * dT;
}
// Update position
scarfyData.pos.y += velocity * dT;
@@ -147,9 +167,25 @@ int main() {
}
}
// Nebula animation frame
for (int i = 0; i < sizeofNebulae; i++)
{
// Update running time
nebulae[i].runningTime += dT;
if (nebulae[i].runningTime >= (nebulae[i].updateTime))
{
nebulae[i].runningTime = 0.0;
nebulae[i].rec.x = nebulae[i].frame * nebulae[i].rec.width;
nebulae[i].frame++;
if (nebulae[i].frame > 7)
{
nebulae[i].frame = 0;
}
}
}
/* // Nebula animation frame
nebulae[0].runningTime += dT;
if (nebulae[0].runningTime >= (nebData.updateTime))
if (nebulae[0].runningTime >= (nebulae[0].updateTime))
{
nebulae[0].runningTime = 0.0;
nebulae[0].rec.x = nebulae[0].frame * nebulae[0].rec.width;
@@ -172,20 +208,20 @@ int main() {
nebulae[1].frame = 0;
}
}
*/
for (int i = 0; i < 2; i++)
// Nebula Animation Frame and Respawn Logic
for (int i = 0; i < sizeofNebulae; i++)
{
// Respawn nebula if it goes off screen
if (nebulae[i].pos.x <= -nebulae[i].rec.width)
{
nebulae[i].pos.x = windowArray[0];
}
nebulae[i].pos.x += nebVel * dT;
}
// draw Nebula
DrawTextureRec(nebula, nebulae[0].rec, nebulae[0].pos, WHITE); // Draw the texture
// draw second Nebula
DrawTextureRec(nebula, nebulae[1].rec, nebulae[1].pos, RED); // Draw the texture
for (int i = 0; i < sizeofNebulae; i++)
{
// draw nebula
DrawTextureRec(nebula, nebulae[i].rec, nebulae[i].pos, WHITE);
}
// draw Scarfy
DrawTextureRec(scarfy, scarfyData.rec, scarfyData.pos, WHITE); // Draw the texture