Example_-_Breadcrumb.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #define MAX_PATH 32
  2. #include "raylib.h"
  3. #include <math.h>
  4. static int mapWidth = 20;
  5. static int mapHeight = 10;
  6. static float tileWidth;
  7. static float tileHeight;
  8. // This is our tile map. Note that [y][x]!!
  9. int map[10][20] = { {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
  10. {1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,1,1,1,1},
  11. {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1},
  12. {1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,1,1,1,1},
  13. {1,1,1,1,1,1,1,1,0,1,1,1,1,0,0,1,1,1,1,1},
  14. {1,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,1},
  15. {1,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,1},
  16. {1,0,0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,1},
  17. {1,0,0,0,0,0,0,1,0,0,0,1,1,1,1,0,0,0,0,1},
  18. {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}};
  19. typedef struct player{
  20. bool active;
  21. int x;
  22. int y;
  23. int pathx[MAX_PATH];
  24. int pathy[MAX_PATH];
  25. }player;
  26. static player p;
  27. static void dropbreadcrumb(void);
  28. int main(void)
  29. {
  30. // Initialization
  31. //--------------------------------------------------------------------------------------
  32. const int screenWidth = 800;
  33. const int screenHeight = 450;
  34. InitWindow(screenWidth, screenHeight, "raylib example.");
  35. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  36. //--------------------------------------------------------------------------------------
  37. p.active = true;
  38. p.x = 5;
  39. p.y = 5;
  40. for(int i=MAX_PATH-1;i>-1;i-=1){
  41. p.pathx[i]=-1;
  42. p.pathy[i]=-1;
  43. }
  44. dropbreadcrumb();
  45. tileWidth = abs((float)screenWidth/(float)mapWidth);
  46. tileHeight = abs((float)screenHeight/(float)mapHeight);
  47. // Main game loop
  48. while (!WindowShouldClose()) // Detect window close button or ESC key
  49. {
  50. // Update
  51. //----------------------------------------------------------------------------------
  52. if(IsKeyReleased(KEY_UP) && map[p.y-1][p.x]==0){
  53. p.y-=1;
  54. dropbreadcrumb();
  55. }
  56. if(IsKeyReleased(KEY_DOWN) && map[p.y+1][p.x]==0){
  57. p.y+=1;
  58. dropbreadcrumb();
  59. }
  60. if(IsKeyReleased(KEY_LEFT) && map[p.y][p.x-1]==0){
  61. p.x-=1;
  62. dropbreadcrumb();
  63. }
  64. if(IsKeyReleased(KEY_RIGHT) && map[p.y][p.x+1]==0){
  65. p.x+=1;
  66. dropbreadcrumb();
  67. }
  68. //----------------------------------------------------------------------------------
  69. // Draw
  70. //----------------------------------------------------------------------------------
  71. BeginDrawing();
  72. ClearBackground(RAYWHITE);
  73. //
  74. // Draw our tilemap.
  75. for(int y=0;y<10;y++){
  76. for(int x=0;x<20;x++){
  77. if(map[y][x]==1){
  78. DrawRectangle(x*tileWidth,y*tileHeight,tileWidth,tileHeight,BLACK);
  79. }
  80. }
  81. }
  82. // Draw our breadcrumb
  83. for(int i=0;i<MAX_PATH;i++){
  84. if(p.pathx[i]>-1)
  85. DrawRectangle( p.pathx[i]*tileWidth+tileWidth/4,p.pathy[i]*tileHeight+tileHeight/4,
  86. tileWidth-tileWidth/2,tileHeight-tileHeight/2,(Color){50,50,50,255-(255.0f/32.0f)*i});
  87. }
  88. // Draw our player
  89. DrawRectangle(p.x*tileWidth,p.y*tileHeight,tileWidth,tileHeight,BLUE);
  90. //
  91. //
  92. DrawText("BreadCrumb Example. Use cursor keys to move around.",0,0,20,GRAY);
  93. EndDrawing();
  94. //----------------------------------------------------------------------------------
  95. }
  96. // De-Initialization
  97. //--------------------------------------------------------------------------------------
  98. CloseWindow(); // Close window and OpenGL context
  99. //--------------------------------------------------------------------------------------
  100. return 0;
  101. }
  102. void dropbreadcrumb(){
  103. // first shift the path one step down.
  104. for(int i=MAX_PATH-1;i>0;i--){
  105. p.pathx[i] = p.pathx[i-1];
  106. p.pathy[i] = p.pathy[i-1];
  107. }
  108. // insert the current position to the top of the path array.
  109. p.pathx[0] = p.x;
  110. p.pathy[0] = p.y;
  111. }