Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "raylib.h"
- #include <string>
- using namespace std;
- int main()
- {
- // window dimensions
- const int windowWidth{512};
- const int windowHeight{380};
- // initialize window
- InitWindow(windowWidth, windowHeight, "Dapper Dasher!");
- // gravity
- const int gravity{1000};
- const int jumpVel{-600};
- // Scarfy sprites
- Texture2D scarfy = LoadTexture("textures/scarfy.png");
- Rectangle scarfyRec{0.0, 0.0, scarfy.width / 6, scarfy.height};
- Vector2 scarfyPos{windowWidth/2 - scarfyRec.width/2, windowHeight - scarfyRec.height};
- // Nebula sprites
- Texture2D nebula = LoadTexture("textures\12_nebula_spritesheet.png");
- Rectangle nebulaRec{0.0, 0.0, nebulaRec.width / 8, nebulaRec.height / 8};
- Vector2 nebulaPos{windowWidth, windowHeight - nebulaRec.height};
- // nebula X velocity (pixels/second)
- const int nebulaVel{-2};
- // rectangle dimensions
- int velocity{};
- bool isGrounded{true};
- //Scarfy animation
- int frame{};
- const float updateTime{1.0/12.0};
- float runningTime{};
- //Nebula animation
- int nebFrame{};
- const float nebUpdateTime{1.0/12.0};
- float nebRunningTime{};
- SetTargetFPS(60);
- while(!WindowShouldClose())
- {
- //Delta Time
- const float dT{GetFrameTime()};
- // start drawing
- BeginDrawing();
- ClearBackground(WHITE);
- isGrounded = scarfyPos.y >= windowHeight - scarfyRec.height;
- // Ground check
- if(isGrounded)
- {
- scarfyPos.y = windowHeight - scarfyRec.height;
- velocity = 0;
- }
- else velocity += gravity * dT;
- //Jump check
- if(IsKeyPressed(KEY_SPACE) && isGrounded) velocity += jumpVel;
- // update nebula position
- nebulaPos.x += nebulaVel * dT;
- // update scarfy position
- scarfyPos.y += velocity * dT;
- // update scarfy animation
- runningTime += dT;
- if(isGrounded)
- {
- if(runningTime >= updateTime)
- {
- runningTime = 0;
- scarfyRec.x = frame * (scarfy.width / 6);
- frame++;
- if(frame > 5) frame = 0;
- }
- }
- // update nebula animation
- nebRunningTime += dT;
- if(isGrounded)
- {
- if(nebRunningTime >= nebUpdateTime)
- {
- nebRunningTime = 0;
- nebulaRec.x = nebFrame * (nebula.width / 8);
- nebFrame++;
- if(nebFrame > 7)
- {
- nebFrame = 0;
- }
- }
- }
- // draw nebula
- DrawTextureRec(nebula, nebulaRec, nebulaPos, WHITE);
- // draw scarfy
- DrawTextureRec(scarfy, scarfyRec, scarfyPos, WHITE);
- // stop drawing
- EndDrawing();
- }
- UnloadTexture(scarfy);
- UnloadTexture(nebula);
- CloseWindow();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement