Advertisement
Pedro_M_Marangon

dasher_moving_hazard.cpp

Jun 18th, 2022
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.98 KB | None | 0 0
  1. #include "raylib.h"
  2. #include <string>
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7.     // window dimensions
  8.     const int windowWidth{512};
  9.     const int windowHeight{380};
  10.     // initialize window
  11.     InitWindow(windowWidth, windowHeight, "Dapper Dasher!");
  12.  
  13.     // gravity
  14.     const int gravity{1000};
  15.     const int jumpVel{-600};
  16.  
  17.     // Scarfy sprites
  18.     Texture2D scarfy = LoadTexture("textures/scarfy.png");
  19.     Rectangle scarfyRec{0.0, 0.0, scarfy.width / 6, scarfy.height};
  20.     Vector2 scarfyPos{windowWidth/2 - scarfyRec.width/2, windowHeight - scarfyRec.height};
  21.  
  22.     // Nebula sprites
  23.     Texture2D nebula = LoadTexture("textures\12_nebula_spritesheet.png");
  24.     Rectangle nebulaRec{0.0, 0.0, nebulaRec.width / 8, nebulaRec.height / 8};
  25.     Vector2 nebulaPos{windowWidth, windowHeight - nebulaRec.height};
  26.     // nebula X velocity (pixels/second)
  27.     const int nebulaVel{-2};
  28.  
  29.     // rectangle dimensions
  30.     int velocity{};
  31.     bool isGrounded{true};
  32.  
  33.     //Scarfy animation
  34.     int frame{};
  35.     const float updateTime{1.0/12.0};
  36.     float runningTime{};
  37.  
  38.     //Nebula animation
  39.     int nebFrame{};
  40.     const float nebUpdateTime{1.0/12.0};
  41.     float nebRunningTime{};
  42.  
  43.     SetTargetFPS(60);
  44.     while(!WindowShouldClose())
  45.     {
  46.         //Delta Time
  47.         const float dT{GetFrameTime()};
  48.         // start drawing
  49.         BeginDrawing();
  50.         ClearBackground(WHITE);
  51.  
  52.         isGrounded = scarfyPos.y >= windowHeight - scarfyRec.height;
  53.        
  54.         // Ground check
  55.         if(isGrounded)
  56.         {
  57.             scarfyPos.y = windowHeight - scarfyRec.height;
  58.             velocity = 0;
  59.         }
  60.         else velocity += gravity * dT;
  61.                
  62.         //Jump check
  63.         if(IsKeyPressed(KEY_SPACE) && isGrounded) velocity += jumpVel;
  64.  
  65.         // update nebula position
  66.         nebulaPos.x += nebulaVel * dT;
  67.  
  68.         // update scarfy position
  69.         scarfyPos.y += velocity * dT;
  70.  
  71.         // update scarfy animation
  72.         runningTime += dT;
  73.         if(isGrounded)
  74.         {
  75.             if(runningTime >= updateTime)
  76.             {
  77.                 runningTime = 0;
  78.                 scarfyRec.x = frame * (scarfy.width / 6);
  79.                 frame++;
  80.                 if(frame > 5) frame = 0;
  81.             }
  82.         }
  83.  
  84.         // update nebula animation
  85.         nebRunningTime += dT;
  86.         if(isGrounded)
  87.         {
  88.             if(nebRunningTime >= nebUpdateTime)
  89.             {
  90.                 nebRunningTime = 0;
  91.                 nebulaRec.x = nebFrame * (nebula.width / 8);
  92.                 nebFrame++;
  93.                 if(nebFrame > 7)
  94.                 {
  95.                     nebFrame = 0;
  96.                 }
  97.             }
  98.         }
  99.  
  100.         // draw nebula
  101.         DrawTextureRec(nebula, nebulaRec, nebulaPos, WHITE);
  102.  
  103.         // draw scarfy
  104.         DrawTextureRec(scarfy, scarfyRec, scarfyPos, WHITE);
  105.  
  106.         // stop drawing
  107.         EndDrawing();
  108.     }
  109.     UnloadTexture(scarfy);
  110.     UnloadTexture(nebula);
  111.     CloseWindow();
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement