#include <iostream>
#include "raylib.h"
using namespace std;
float dt; //Delta Time
//Rect
float velocity{0};
float nebVel{-200};
float gravity = 1000;
int jumpVel = -600;
bool isInAir = false;
struct AnimData
{
Rectangle rec;
Vector2 pos;
int frame;
float updateTime;
float runningTime;
};
int windowDimentions[2]
{
900, 400
};
int main()
{
InitWindow(windowDimentions[0], windowDimentions[1], "Dasher");
SetTargetFPS(60);
//Nebula
Texture2D nebula = LoadTexture("textures/12_nebula_spritesheet.png");
//AnimData for nebula
const int sizeOfNebulae = 6;
AnimData nebulae[sizeOfNebulae]{};
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.width = nebula.height/8;
nebulae[i].pos.y = windowDimentions[1] - nebula.height/8;
nebulae[i].pos.x = windowDimentions[0] + i * 300;
nebulae[i].frame = 0;
nebulae[i].runningTime = 0.0;
nebulae[i].updateTime = 0.0;
}
//Scarfy
Texture2D scarfy = LoadTexture("textures/scarfy.png");
AnimData scarfyData
{
{0.0, 0.0, scarfy.width/6, scarfy.height},
{windowDimentions[0]/2 - scarfyData.rec.width/2, windowDimentions[1] - scarfyData.rec.height},
0,
1.0/12.0,
0.0
};
/*
//////////Main Loop///////////
*/
while (!WindowShouldClose())
{
//Widnow
BeginDrawing();
ClearBackground(WHITE);
//Game Logic
dt = GetFrameTime();
//Jump Check
if (scarfyData.pos.y >= windowDimentions[1] - scarfyData.rec.height)
{
velocity = 0;
isInAir = false;
}
else
{
// rec is in air
//apply gravity
velocity += gravity*dt;
isInAir = true;
}
//check for jumping
if (IsKeyPressed(KEY_SPACE) && !isInAir)
{
velocity += jumpVel;
}
for (int i = 0; i < sizeOfNebulae; i++)
{
//Update Nebula Pos
nebulae[i].pos.x += nebVel*dt;
}
//Update Scarfy
scarfyData.pos.y += velocity*dt;
//Animation scarfy sprites
scarfyData.runningTime += dt;
if (scarfyData.runningTime >= scarfyData.updateTime && !isInAir)
{
scarfyData.runningTime = 0.0;
scarfyData.rec.x = scarfyData.frame * scarfyData.rec.width;
scarfyData.frame++;
if (scarfyData.frame > 5)
{
scarfyData.frame = 0;
}
}
for (int i = 0; i < sizeOfNebulae; i++)
{
//Nebula animation
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;
}
}
}
for (int i = 0; i < sizeOfNebulae; i++)
{
//Draw Nebula
DrawTextureRec(nebula, nebulae[i].rec, nebulae[i].pos, WHITE);
}
// Draw Scafy
DrawTextureRec(scarfy, scarfyData.rec, scarfyData.pos, WHITE);
EndDrawing();
}
UnloadTexture(scarfy);
UnloadTexture(nebula);
CloseWindow();
}
I solved it
I did put width two times, took me like hour to find. Thanks for showing breakpoints tool!
Good job finding the solution!
This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.