Example_-_Collision_map.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #include "raylib.h"
  2. typedef struct player{
  3. Vector2 position;
  4. }player;
  5. int main(void)
  6. {
  7. // Initialization
  8. //--------------------------------------------------------------------------------------
  9. const int screenWidth = 800;
  10. const int screenHeight = 450;
  11. InitWindow(screenWidth, screenHeight, "raylib example.");
  12. // Our player setup
  13. player myplayer = {0};
  14. myplayer.position = (Vector2){5,5};
  15. // Our tile width and height. We use the screen dimension and the map dimension to
  16. // get tile dimensions that fill up the entire screen.
  17. float tilewidth = (float)screenWidth/20.0f;
  18. float tileheight = (float)screenHeight/10.0f;
  19. // This is out collision map.
  20. bool colmap[10][20] = {false};
  21. // This is our tile map.
  22. int map[10][20] = { {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
  23. {1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,1,1,1,1},
  24. {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1},
  25. {1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,1,1,1,1},
  26. {1,1,1,1,1,1,1,1,0,1,1,1,1,0,0,1,1,1,1,1},
  27. {1,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,1},
  28. {1,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,1},
  29. {1,0,0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,1},
  30. {1,0,0,0,0,0,0,1,0,0,0,1,1,1,1,0,0,0,0,1},
  31. {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}};
  32. // Copy the contents of the map into the collision map.
  33. for(int y=0;y<10;y++){
  34. for(int x=0;x<20;x++){
  35. if(map[y][x]>0)colmap[y][x]=true;
  36. }
  37. }
  38. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  39. //--------------------------------------------------------------------------------------
  40. // Main game loop
  41. while (!WindowShouldClose()) // Detect window close button or ESC key
  42. {
  43. // Update
  44. //----------------------------------------------------------------------------------
  45. // Below here we read the key. If the l/r/u/d cursor is pressed then we check the new position is inside
  46. // the array bounds and then we check if the collision map on the new position is not set to flag true on that tile.
  47. if(IsKeyPressed(KEY_RIGHT)&& myplayer.position.x+1<20 && colmap[(int)myplayer.position.y][(int)myplayer.position.x+1]==false) {
  48. myplayer.position.x+=1;
  49. }
  50. if(IsKeyPressed(KEY_LEFT) && myplayer.position.x-1>-1 && colmap[(int)myplayer.position.y][(int)myplayer.position.x-1]==false) {
  51. myplayer.position.x-=1;
  52. }
  53. if(IsKeyPressed(KEY_UP)&& myplayer.position.y-1>-1 && colmap[(int)myplayer.position.y-1][(int)myplayer.position.x]==false) {
  54. myplayer.position.y-=1;
  55. }
  56. if(IsKeyPressed(KEY_DOWN) && myplayer.position.y+1<20 && colmap[(int)myplayer.position.y+1][(int)myplayer.position.x]==false) {
  57. myplayer.position.y+=1;
  58. }
  59. //----------------------------------------------------------------------------------
  60. // Draw
  61. //----------------------------------------------------------------------------------
  62. BeginDrawing();
  63. ClearBackground(RAYWHITE);
  64. // Draw our tilemap.
  65. for(int y=0;y<10;y++){
  66. for(int x=0;x<20;x++){
  67. if(map[y][x]==1){
  68. DrawRectangle(x*tilewidth,y*tileheight,tilewidth,tileheight,BLACK);
  69. }
  70. }
  71. }
  72. // Draw our player
  73. DrawRectangle(myplayer.position.x*tilewidth,myplayer.position.y*tileheight,tilewidth,tileheight,RED);
  74. DrawText("Use Cursor Keys to move around..",0,0,20,WHITE);
  75. EndDrawing();
  76. //----------------------------------------------------------------------------------
  77. }
  78. // De-Initialization
  79. //--------------------------------------------------------------------------------------
  80. CloseWindow(); // Close window and OpenGL context
  81. //--------------------------------------------------------------------------------------
  82. return 0;
  83. }