Example_-_2D_ShootBullets.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #include "raylib.h"
  2. #include <stdlib.h>
  3. // math.h is needed for cos and sin.
  4. #include <math.h>
  5. #define MAX_BULLETS 100
  6. #define MAX_BATCH_ELEMENTS 8192
  7. typedef struct Bullets{
  8. Vector2 position;
  9. bool active;
  10. float angle;
  11. } Bullets;
  12. int main(void)
  13. {
  14. // Initialization
  15. //--------------------------------------------------------------------------------------
  16. const int screenWidth = 800;
  17. const int screenHeight = 450;
  18. InitWindow(screenWidth, screenHeight, "raylib example.");
  19. Bullets *mybullets = (Bullets *)malloc(MAX_BULLETS*sizeof(Bullets)); // Bullets array
  20. mybullets[0].active=false;
  21. // Create a Image in memory
  22. RenderTexture2D target = LoadRenderTexture(32, 32);
  23. int sprite[8][8] = {
  24. {0,2,1,0,0,1,2,0},
  25. {2,1,0,2,2,0,1,2},
  26. {1,0,2,1,1,2,0,1},
  27. {0,2,1,1,1,1,2,0},
  28. {0,2,1,1,1,1,2,0},
  29. {1,0,2,1,1,2,0,1},
  30. {2,1,0,2,2,0,1,2},
  31. {0,2,1,0,0,1,2,0}};
  32. // Clear our texture(image) before entering the game loop
  33. BeginTextureMode(target);
  34. ClearBackground(BLANK); // Make the entire Sprite Transparent.
  35. EndTextureMode();
  36. // Draw something on it.
  37. for (int y=0;y<8;y++)
  38. {
  39. for (int x=0;x<8; x++)
  40. {
  41. // Our sprite color 1
  42. if (sprite[y][x]==1)
  43. {
  44. BeginTextureMode(target);
  45. DrawRectangle(x*4,y*4,4,4,WHITE);
  46. EndTextureMode();
  47. }
  48. // Our sprite color 2
  49. if (sprite[y][x]==2)
  50. {
  51. BeginTextureMode(target);
  52. DrawRectangle(x*4,y*4,4,4,RED);
  53. EndTextureMode();
  54. }
  55. }
  56. }
  57. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  58. //--------------------------------------------------------------------------------------
  59. // Main game loop
  60. while (!WindowShouldClose()) // Detect window close button or ESC key
  61. {
  62. // Update
  63. //----------------------------------------------------------------------------------
  64. // Update the bullets
  65. // Deactivate if outside window.
  66. for(int i=0;i<MAX_BULLETS;i++){
  67. if(mybullets[i].active){
  68. mybullets[i].position.x+=(float)cos(mybullets[i].angle)*3;
  69. mybullets[i].position.y+=(float)sin(mybullets[i].angle)*3;
  70. if(mybullets[i].position.x<-50 || mybullets[i].position.x>screenWidth+50 || mybullets[i].position.y<-50 || mybullets[i].position.y>screenHeight+50){
  71. mybullets[i].active = false;
  72. }
  73. }
  74. }
  75. // Shoot a bullet
  76. if(IsMouseButtonReleased(0)){
  77. for(int i=0;i<MAX_BULLETS;i++){
  78. if(mybullets[i].active==false){
  79. mybullets[i].active=true;
  80. mybullets[i].angle = GetRandomValue(0,359);
  81. mybullets[i].position = GetMousePosition();
  82. break;
  83. }
  84. }
  85. }
  86. //----------------------------------------------------------------------------------
  87. // Draw
  88. //----------------------------------------------------------------------------------
  89. BeginDrawing();
  90. ClearBackground(RAYWHITE);
  91. DrawText("Press the left Mouse Button to Shoot button from mouse Pos.",0,100,20,BLACK);
  92. for(int i=0;i<MAX_BULLETS;i++){
  93. if(mybullets[i].active){
  94. DrawTexture(target.texture,mybullets[i].position.x,mybullets[i].position.y,WHITE);
  95. }}
  96. EndDrawing();
  97. //----------------------------------------------------------------------------------
  98. }
  99. // De-Initialization
  100. free(mybullets); // Unload points data array
  101. UnloadRenderTexture(target); // Unload render texture
  102. //--------------------------------------------------------------------------------------
  103. CloseWindow(); // Close window and OpenGL context
  104. //--------------------------------------------------------------------------------------
  105. return 0;
  106. }