Example_-_PlayerJumping.c 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #include "raylib.h"
  2. #include "math.h"
  3. // our player struct
  4. typedef struct player{
  5. Vector2 position;
  6. int width;
  7. int height;
  8. float jumpforce;
  9. bool canjump;
  10. }player;
  11. int main(void)
  12. {
  13. // Initialization
  14. //--------------------------------------------------------------------------------------
  15. const int screenWidth = 800;
  16. const int screenHeight = 450;
  17. InitWindow(screenWidth, screenHeight, "raylib example.");
  18. const float ground = 200.0f;
  19. player myplayer = {0};
  20. myplayer.position = (Vector2){400,200};
  21. myplayer.canjump = true;
  22. myplayer.width = 32;
  23. myplayer.height = 48;
  24. myplayer.jumpforce = 0.0f;
  25. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  26. //--------------------------------------------------------------------------------------
  27. // Main game loop
  28. while (!WindowShouldClose()) // Detect window close button or ESC key
  29. {
  30. // Update
  31. //----------------------------------------------------------------------------------
  32. // If we are currently not jumping and if the player presses space.
  33. if(IsKeyDown(KEY_SPACE) && myplayer.canjump){
  34. myplayer.canjump = false; // We are now jumping.
  35. myplayer.jumpforce = -4.0f; // How high we jump.
  36. }
  37. // If we are currently in a jump.
  38. if(myplayer.canjump==false){
  39. // Here we update the player y position.
  40. // We want to move 1 pixel at a time. This for collision detection(Not going through tiles.)
  41. for(int i=0;i<abs((int)myplayer.jumpforce);i++){
  42. // If we are still jumping then update the position.
  43. if(myplayer.canjump==false)myplayer.position.y += myplayer.jumpforce;
  44. // if we touch the ground then reset out position and set canjump to true.
  45. if(myplayer.position.y>ground){
  46. myplayer.position.y = ground;
  47. myplayer.canjump = true;
  48. break;
  49. }
  50. }
  51. // Here we add to the jumpforce. It will go from - to +.
  52. myplayer.jumpforce += 0.2f;
  53. }
  54. //----------------------------------------------------------------------------------
  55. // Draw
  56. //----------------------------------------------------------------------------------
  57. BeginDrawing();
  58. ClearBackground(RAYWHITE);
  59. DrawText("Press space to Jump.",200,240,40,DARKGRAY);
  60. // Draw our player.
  61. DrawRectangle(myplayer.position.x,myplayer.position.y,myplayer.width,myplayer.height,BLACK);
  62. DrawCircle(myplayer.position.x+4,myplayer.position.y+6,4,WHITE);
  63. DrawCircle(myplayer.position.x+myplayer.width-4,myplayer.position.y+6,4,WHITE);
  64. EndDrawing();
  65. //----------------------------------------------------------------------------------
  66. }
  67. // De-Initialization
  68. //--------------------------------------------------------------------------------------
  69. CloseWindow(); // Close window and OpenGL context
  70. //--------------------------------------------------------------------------------------
  71. return 0;
  72. }