3.19 Arrays

This commit is contained in:
2026-01-28 09:22:40 -05:00
parent 4d9ae2716f
commit 1bf0663091

View File

@@ -12,11 +12,16 @@ struct AnimData
int main() {
// Specified width and height
/* // Specified width and height
const int windowWidth{800};
const int windowHeight{600};
*/
// windows array dimensions
float windowArray[2] = { 800.0, 600.0 };
// Initialize the window
InitWindow(windowWidth, windowHeight, "ECS Dapper Dasher");
InitWindow(windowArray[0], windowArray[1], "ECS Dapper Dasher");
// Acceleration due to gravity (pixels per frame/frame)
const int gravity{ 1'000 };
@@ -29,23 +34,12 @@ int main() {
scarfyData.rec.x = 0;
scarfyData.rec.y = 0;
scarfyData.pos.x = windowWidth / 2 - scarfyData.rec.width / 2;
scarfyData.pos.y = windowHeight - scarfyData.rec.height;
scarfyData.pos.x = windowArray[0] / 2 - scarfyData.rec.width / 2;
scarfyData.pos.y = windowArray[1] - scarfyData.rec.height;
scarfyData.frame = 0;
scarfyData.updateTime = 1.0 / 12.0;
scarfyData.runningTime = 0.0;
// Scarfy Rectangle
Rectangle scarfyRect;
scarfyRect.width = scarfy.width / 6;
scarfyRect.height = scarfy.height;
scarfyRect.x = 0;
scarfyRect.y = 0;
Vector2 scarfyPos;
scarfyPos.x = windowWidth / 2 - scarfyRect.width / 2;
scarfyPos.y = windowHeight - scarfyRect.height;
// Nebula setup
Texture2D nebula = LoadTexture("textures/12_nebula_spritesheet.png"); // Load the texture
@@ -53,7 +47,7 @@ int main() {
AnimData nebData{
{0.0, 0.0, nebula.width / 8, nebula.height / 8}, // Rectangle rec
{windowWidth, windowHeight - nebula.height / 8}, // Vector2 pos
{windowArray[0], windowArray[1] - nebula.height / 8}, // Vector2 pos
0, // int frame
1.0 / 12.0, // float updateTime
0.0 // float runningTime
@@ -61,25 +55,18 @@ int main() {
AnimData neb2Data{
{0.0, 0.0, nebula.width / 8, nebula.height / 8}, // Rectangle rec
{windowWidth + 300, windowHeight - nebula.height / 8}, // Vector2 pos
{windowArray[0] + 300, windowArray[1] - nebula.height / 8}, // Vector2 pos
0, // int frame
1.0 / 16.0, // float updateTime
0.0 // float runningTime
};
Rectangle nebRect{0.0, 0.0, nebula.width / 8, nebula.height / 8};
Vector2 nebulaPos{windowWidth, windowHeight - nebRect.height};
Rectangle neb2Rect = nebRect;
Vector2 neb2Pos{windowWidth + 300, windowHeight - neb2Rect.height};
int nebFrame{}; // Nebula animation frame
const float nebUpdateTime{1.0 / 12.0}; // Nebula running time
float nebRunningTime{}; // Nebula time since last frame change
int neb2Frame{}; // Nebula animation frame
const float neb2UpdateTime{1.0 / 16.0}; // Nebula running time
float neb2RunningTime{}; // Nebula time since last frame change
// Nebulea
AnimData nebulae[2] =
{
nebData,
neb2Data
};
int nebVel{-200}; // Nebula x velocity (pixels per second)
@@ -109,7 +96,7 @@ int main() {
ClearBackground(WHITE);
// perform ground check
if (scarfyPos.y >= windowHeight - scarfyRect.height) {
if (scarfyData.pos.y >= windowArray[1] - scarfyData.rec.height) {
velocity = 0; // Reset velocity when on the ground
isInAir = false;
}
@@ -125,13 +112,13 @@ int main() {
}
// Nebula movement
nebulaPos.x += nebVel * dT;
nebData.pos.x += nebVel * dT;
// Nebula 2 movement
neb2Pos.x += nebVel * dT;
neb2Data.pos.x += nebVel * dT;
// Update position
scarfyPos.y += velocity * dT;
scarfyData.pos.y += velocity * dT;
// Scarfy Animation Logic
if (!isInAir)
@@ -143,7 +130,7 @@ int main() {
runningTime = 0.0;
// Update animation frame
scarfyRect.x = frame * scarfyRect.width;
scarfyData.rec.x = frame * scarfyData.rec.width;
frame++;
if (frame > 5)
{
@@ -153,38 +140,47 @@ int main() {
}
// Nebula animation frame
nebRunningTime += dT;
if (nebRunningTime >= (nebUpdateTime))
nebulae[0].runningTime += dT;
if (nebulae[0].runningTime >= (nebData.updateTime))
{
nebRunningTime = 0.0;
nebRect.x = nebFrame * nebRect.width;
nebFrame++;
if (nebFrame > 7)
nebulae[0].runningTime = 0.0;
nebulae[0].rec.x = nebulae[0].frame * nebulae[0].rec.width;
nebulae[0].frame++;
if (nebulae[0].frame > 7)
{
nebFrame = 0;
nebulae[0].frame = 0;
}
}
// Nebula animation frame
neb2RunningTime += dT;
if (neb2RunningTime >= (neb2UpdateTime))
nebulae[1].runningTime += dT;
if (nebulae[1].runningTime >= (nebulae[1].updateTime))
{
neb2RunningTime = 0.0;
neb2Rect.x = neb2Frame * neb2Rect.width;
neb2Frame++;
if (neb2Frame > 7)
nebulae[1].runningTime = 0.0;
nebulae[1].rec.x = nebulae[1].frame * nebulae[1].rec.width;
nebulae[1].frame++;
if (nebulae[1].frame > 7)
{
neb2Frame = 0;
nebulae[1].frame = 0;
}
}
for (int i = 0; i < 2; i++)
{
// Respawn nebula if it goes off screen
if (nebulae[i].pos.x <= -nebulae[i].rec.width)
{
nebulae[i].pos.x = windowArray[0];
}
}
// draw Nebula
DrawTextureRec(nebula, nebRect, nebulaPos, WHITE); // Draw the texture
DrawTextureRec(nebula, nebulae[0].rec, nebulae[0].pos, WHITE); // Draw the texture
// draw second Nebula
DrawTextureRec(nebula, neb2Rect, neb2Pos, RED); // Draw the texture
DrawTextureRec(nebula, nebulae[1].rec, nebulae[1].pos, RED); // Draw the texture
// draw Scarfy
DrawTextureRec(scarfy, scarfyRect, scarfyPos, WHITE); // Draw the texture
DrawTextureRec(scarfy, scarfyData.rec, scarfyData.pos, WHITE); // Draw the texture
// End Game Logic
EndDrawing();