scarfy added with jump and velocity
This commit is contained in:
62
dasher.cpp
62
dasher.cpp
@@ -2,14 +2,70 @@
|
|||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
|
|
||||||
InitWindow(800, 600, "Dasher Example");
|
// 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 };
|
||||||
|
|
||||||
|
// 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;
|
||||||
|
|
||||||
|
// Is the rectangle in the air
|
||||||
|
bool isInAir{};
|
||||||
|
|
||||||
|
int velocity{}; // Current velocity
|
||||||
|
|
||||||
|
// Jump velocity
|
||||||
|
const int jumpVelocity{-22};
|
||||||
|
|
||||||
|
// Main game loop
|
||||||
SetTargetFPS(60);
|
SetTargetFPS(60);
|
||||||
while (!WindowShouldClose())
|
while (!WindowShouldClose())
|
||||||
{
|
{
|
||||||
|
// Start Game Logic
|
||||||
BeginDrawing();
|
BeginDrawing();
|
||||||
ClearBackground(RAYWHITE);
|
ClearBackground(WHITE);
|
||||||
|
|
||||||
|
// perform ground check
|
||||||
|
if (scarfyPos.y >= windowHeight - scarfyRect.height) {
|
||||||
|
velocity = 0; // Reset velocity when on the ground
|
||||||
|
isInAir = false;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
velocity += gravity; // 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;
|
||||||
|
|
||||||
|
DrawTextureRec(scarfy, scarfyRect, scarfyPos, WHITE); // Draw the texture
|
||||||
|
|
||||||
|
// End Game Logic
|
||||||
EndDrawing();
|
EndDrawing();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
// Unload texture
|
||||||
|
UnloadTexture(scarfy);
|
||||||
|
|
||||||
|
// Close the window properly
|
||||||
|
CloseWindow();
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user