learning section 3.15

This commit is contained in:
2026-01-27 08:12:01 -05:00
parent ae78c19cd8
commit 4d9ae2716f

View File

@@ -1,5 +1,15 @@
#include <raylib.h> #include <raylib.h>
struct AnimData
{
Rectangle rec;
Vector2 pos;
int frame;
float updateTime;
float runningTime;
};
int main() { int main() {
// Specified width and height // Specified width and height
@@ -13,6 +23,20 @@ int main() {
// Scarfy setup // Scarfy setup
Texture2D scarfy = LoadTexture("textures/scarfy.png"); // Load the texture Texture2D scarfy = LoadTexture("textures/scarfy.png"); // Load the texture
AnimData scarfyData;
scarfyData.rec.width = scarfy.width / 6;
scarfyData.rec.height = scarfy.height;
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.frame = 0;
scarfyData.updateTime = 1.0 / 12.0;
scarfyData.runningTime = 0.0;
// Scarfy Rectangle
Rectangle scarfyRect; Rectangle scarfyRect;
scarfyRect.width = scarfy.width / 6; scarfyRect.width = scarfy.width / 6;
scarfyRect.height = scarfy.height; scarfyRect.height = scarfy.height;
@@ -24,13 +48,39 @@ int main() {
// Nebula setup // Nebula setup
Texture2D nebula = LoadTexture("textures/12_nebula_spritesheet.png"); // Load the texture Texture2D nebula = LoadTexture("textures/12_nebula_spritesheet.png"); // Load the texture
// Nebula anim
AnimData nebData{
{0.0, 0.0, nebula.width / 8, nebula.height / 8}, // Rectangle rec
{windowWidth, windowHeight - nebula.height / 8}, // Vector2 pos
0, // int frame
1.0 / 12.0, // float updateTime
0.0 // float runningTime
};
AnimData neb2Data{
{0.0, 0.0, nebula.width / 8, nebula.height / 8}, // Rectangle rec
{windowWidth + 300, windowHeight - 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}; Rectangle nebRect{0.0, 0.0, nebula.width / 8, nebula.height / 8};
Vector2 nebulaPos{windowWidth, windowHeight - nebRect.height}; Vector2 nebulaPos{windowWidth, windowHeight - nebRect.height};
Rectangle neb2Rect = nebRect;
Vector2 neb2Pos{windowWidth + 300, windowHeight - neb2Rect.height};
int nebFrame{}; // Nebula animation frame int nebFrame{}; // Nebula animation frame
const float nebUpdateTime{1.0 / 12.0}; // Nebula running time const float nebUpdateTime{1.0 / 12.0}; // Nebula running time
float nebRunningTime{}; // Nebula time since last frame change 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
int nebVel{-200}; // Nebula x velocity (pixels per second) int nebVel{-200}; // Nebula x velocity (pixels per second)
// Animation Frame // Animation Frame
@@ -77,6 +127,9 @@ int main() {
// Nebula movement // Nebula movement
nebulaPos.x += nebVel * dT; nebulaPos.x += nebVel * dT;
// Nebula 2 movement
neb2Pos.x += nebVel * dT;
// Update position // Update position
scarfyPos.y += velocity * dT; scarfyPos.y += velocity * dT;
@@ -112,8 +165,23 @@ int main() {
} }
} }
// Nebula animation frame
neb2RunningTime += dT;
if (neb2RunningTime >= (neb2UpdateTime))
{
neb2RunningTime = 0.0;
neb2Rect.x = neb2Frame * neb2Rect.width;
neb2Frame++;
if (neb2Frame > 7)
{
neb2Frame = 0;
}
}
// draw Nebula // draw Nebula
DrawTextureRec(nebula, nebRect, nebulaPos, WHITE); // Draw the texture DrawTextureRec(nebula, nebRect, nebulaPos, WHITE); // Draw the texture
// draw second Nebula
DrawTextureRec(nebula, neb2Rect, neb2Pos, RED); // Draw the texture
// draw Scarfy // draw Scarfy
DrawTextureRec(scarfy, scarfyRect, scarfyPos, WHITE); // Draw the texture DrawTextureRec(scarfy, scarfyRect, scarfyPos, WHITE); // Draw the texture