Files
cpp-dapper-dasher/dasher.cpp

111 lines
3.0 KiB
C++

#include <raylib.h>
int main() {
// Specified width and height
const int windowWidth{800};
const int windowHeight{600};
// Initialize the window
InitWindow(windowWidth, windowHeight, "ECS Dapper Dasher");
// Acceleration due to gravity (pixels per frame/frame)
const int gravity{ 1'000 };
// Scarfy setup
Texture2D scarfy = LoadTexture("textures/scarfy.png"); // Load the texture
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
Rectangle nebulaRect{0.0, 0.0, nebula.width / 8, nebula.height / 8};
Vector2 nebulaPos{windowWidth, windowHeight - nebulaRect.height};
int nevVel{-600}; // Nebula x velocity (pixels per second)
// Animation Frame
int frame{};
const float updateTime{1.0 / 12.0}; // Time between frames
float runningTime{}; // Time since last frame change
// Is the rectangle in the air
bool isInAir{};
int velocity{}; // Current velocity
// Jump velocity (pixels per second)
const int jumpVelocity{-600};
// Main game loop
SetTargetFPS(60);
while (!WindowShouldClose())
{
// Delta time
const float dT = GetFrameTime(); // Delta time (time since last frame)
// Start Game Logic
BeginDrawing();
ClearBackground(WHITE);
// perform ground check
if (scarfyPos.y >= windowHeight - scarfyRect.height) {
velocity = 0; // Reset velocity when on the ground
isInAir = false;
}
else
{
velocity += gravity * dT; // Apply gravity when in the air
isInAir = true;
}
// Jump logic / jump check
if (IsKeyPressed(KEY_SPACE) && !isInAir) {
velocity += jumpVelocity; // Jump velocity
}
// Update position
scarfyPos.y += velocity * dT;
if (!isInAir)
{
// Update running time
runningTime += dT;
if (runningTime >= updateTime)
{
runningTime = 0.0;
// Update animation frame
scarfyRect.x = frame * scarfyRect.width;
frame++;
if (frame > 5)
{
frame = 0;
}
}
}
// draw Nebula
DrawTextureRec(nebula, nebulaRect, nebulaPos, WHITE); // Draw the texture
// draw Scarfy
DrawTextureRec(scarfy, scarfyRect, scarfyPos, WHITE); // Draw the texture
// End Game Logic
EndDrawing();
}
// Unload texture
UnloadTexture(scarfy);
UnloadTexture(nebula);
// Close the window properly
CloseWindow();
}