Hi, I have this problem, When I set the collision to false in the main function. They are drawing game over but if I put the collision in the loop, the scarfy and nebula are running but when it reaches the collision , it’s working. Any tips?
1 Like
Hi Christopher,
Can you show us your code? Will help us debug the issue together.
#include “raylib.h”
struct AnimData
{
Rectangle rect;
Vector2 pos;
int frame;
float updateTime;
float runningTime;
};
bool isonGround (AnimData data , int windowHeight)
{
return (data.pos.y >= windowHeight - data.rect.height);
}
AnimData updateAnimData (AnimData data , float deltatime , int maxFrame)
{
data.runningTime += deltatime;
if(data.runningTime >= data.updateTime)
{
data.runningTime = 0.0;
data.rect.x = data.frame * data.rect.width;
data.frame++;
if (data.frame > maxFrame)
{
data.frame = 0;
}
}
return data;
}
int main ()
{
int windowDimensions[2];
windowDimensions[0]= 512;
windowDimensions[1] = 380;
InitWindow(windowDimensions[0],windowDimensions[1], "Dapper-dasher");
int velocity{0};
bool isInAir{};
int jumpvel{-600};
Texture scarfy = LoadTexture("textures/scarfy.png");
Texture2D nebula = LoadTexture("textures/12_nebula_spritesheet.png");
Texture2D midground = LoadTexture("textures/back-buildings.png");
Texture2D foreground = LoadTexture("textures/foreground.png");
float mgX{};
float fgX{};
// scarfy Anim Data
AnimData scarfyAnimData;
scarfyAnimData.rect.height = scarfy.height;
scarfyAnimData.rect.width = scarfy.width/6;
scarfyAnimData.rect.x = 0;
scarfyAnimData.rect.y = 0;
scarfyAnimData.pos.x = windowDimensions[0]/2 - scarfyAnimData.rect.width/2;
scarfyAnimData.pos.y = windowDimensions[1]- scarfyAnimData.rect.height;
scarfyAnimData.frame = 0;
scarfyAnimData.updateTime = 1.0/12.0;
scarfyAnimData.runningTime = 0.0;
//nebula anim variable
const int sizeofnebulae{6};
AnimData nebulae[sizeofnebulae]{};
for (int i = 0; i<sizeofnebulae ; i++)
{
nebulae[i].rect.x = 0.0;
nebulae[i].rect.y = 0.0;
nebulae[i].rect.width = nebula.width/8;
nebulae[i].rect.height = nebula.height/8;
nebulae[i].pos.x = windowDimensions[0]*i+300;
nebulae[i].pos.y = windowDimensions[1]- nebula.height/8;
nebulae[i].runningTime = 0.0;
nebulae[i].updateTime = 0.0;
}
float finishLine {nebulae[sizeofnebulae-1].pos.x};
int nebVel {-200};
bool collision{};
SetTargetFPS(60);
const int gravity{1000};
while(WindowShouldClose() != true)
{
BeginDrawing();
ClearBackground(WHITE);
float dT{GetFrameTime()};
mgX -= 40*dT;
fgX -= 80*dT;
if(mgX <= -midground.width*2)
{
mgX = 0.0;
}
if(fgX <= -foreground.width*2)
{
fgX= 0.0;
}
Vector2 mg1Pos {mgX,0.0};
Vector2 mg2Pos {mgX + midground.width*2.0,0.0};
Vector2 fg1Pos {fgX,0.0};
Vector2 fg2Pos{fgX+foreground.width*2.0,0.0};
DrawTextureEx(midground, mg1Pos, 0.0, 2.0, WHITE);
DrawTextureEx(midground, mg2Pos, 0.0, 2.0, WHITE);
DrawTextureEx(foreground, fg1Pos, 0.0, 2.0, WHITE);
DrawTextureEx(foreground, fg2Pos, 0.0, 2.0, WHITE);
if(isonGround(scarfyAnimData, windowDimensions[1]))
{
velocity = 0;
isInAir = false;
}
else
{
velocity += gravity *dT;
isInAir = true;
}
if (IsKeyPressed(KEY_SPACE) && !isInAir)
{
velocity += jumpvel;
}
for (int i = 0; i<sizeofnebulae; i++ )
{
nebulae[i].pos.x += nebVel* dT;
}
finishLine+= nebVel*dT;
scarfyAnimData.pos.y += velocity *dT;
if(!isInAir)
{
scarfyAnimData = updateAnimData(scarfyAnimData , dT , 5);
}
//nebula update animation
for(int i =0 ; i<sizeofnebulae; i++)
{
nebulae[i] = updateAnimData(nebulae[i], dT, 7);
}
for (AnimData nebula : nebulae)
{
float pad{50};
Rectangle nebRec{
nebula.pos.x + pad,
nebula.pos.y + pad,
nebula.rect.width - 2*pad,
nebula.rect.height - 2*pad
};
Rectangle scarfyRec{
scarfyAnimData.pos.x,
scarfyAnimData.pos.y,
scarfyAnimData.rect.width,
scarfyAnimData.rect.height
};
if (CheckCollisionRecs(nebRec, scarfyRec))
{
collision = true;
}
}
if (collision)
{
// lose the game
DrawText("Game Over!", windowDimensions[0]/4, windowDimensions[1]/2, 40, RED);
}
else if (scarfyAnimData.pos.x >= finishLine)
{
// win the game
DrawText("You Win!", windowDimensions[0]/4, windowDimensions[1]/2, 40, RED);
}
else
{
for (int i = 0; i < sizeofnebulae; i++)
{
// draw nebula
DrawTextureRec(nebula, nebulae[i].rect, nebulae[i].pos, WHITE);
}
// draw scarfy
DrawTextureRec(scarfy, scarfyAnimData.rect, scarfyAnimData.pos, WHITE);
}
It looks like the issue is with the positioning of your Nebulae.
nebulae[i].pos.x = windowDimensions[0]*i+300;
Places the first nebula only 44 pixels away from Scarfy, giving the player less than 1 sec to react and jump over the nebula.
However, with this, which swaps the + and *.
nebulae[i].pos.x = windowDimensions[0]+i*300;
Then the first nebula is at 512 (256 away), which gives much more time for the player to react.
This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.