block_part6.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. #include "raylib.h"
  2. #include <math.h>
  3. int focus=0;
  4. int map[100][100] = {0};
  5. int tileHeight = 32;
  6. int tileWidth = 32;
  7. int mapWidth = 100;
  8. int mapHeight = 100;
  9. // Get our angle between two points.
  10. static float getangle(float x1,float y1,float x2,float y2);
  11. // Our rectsoverlap function. Returns true/false.
  12. static bool rectsoverlap(int x1,int y1,int w1,int h1,int x2,int y2,int w2,int h2);
  13. //Unit collide with solid blocks true/false
  14. bool recttilecollide(int x, int y, int w, int h, int offsetx,int offsety);
  15. void score();
  16. int main(void)
  17. {
  18. // Initialization
  19. //--------------------------------------------------------------------------------------
  20. const int screenWidth = 800;
  21. const int screenHeight = 450;
  22. InitWindow(screenWidth, screenHeight, "raylib example.");
  23. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  24. //--------------------------------------------------------------------------------------
  25. for(int i=0;i<25;i++){
  26. map[i][0]=GetRandomValue(1,3);
  27. }
  28. map[5][1]=1;
  29. float posx = 320;
  30. float posy = 400;
  31. float animcolor = 1;
  32. float animx=posx;
  33. float animy=posy;
  34. float animtargetx;
  35. float animtargety;
  36. bool startanim = false;
  37. int slidedowntimer = 0;
  38. int slidedowntimermax = 20;
  39. bool actualgameover=false;
  40. // Main game loop
  41. while (!WindowShouldClose()) // Detect window close button or ESC key
  42. {
  43. // Update
  44. //----------------------------------------------------------------------------------
  45. if(IsMouseButtonDown(1)){
  46. for(int x=0;x<25;x++){
  47. tileWidth=16;
  48. }
  49. }
  50. slidedowntimer++;
  51. if(slidedowntimer>slidedowntimermax && startanim==false){
  52. slidedowntimer=0;
  53. actualgameover=false;
  54. for(int x=0;x<25;x++){
  55. if(map[x][7]>0){
  56. actualgameover=true;
  57. }
  58. }
  59. if(actualgameover==false){
  60. for(int y=20;y>0;y--){
  61. for(int x=0;x<25;x++){
  62. map[x][y]=map[x][y-1];
  63. }}
  64. for(int i=0;i<25;i++){
  65. map[i][0]=GetRandomValue(1,3);
  66. }
  67. }
  68. }
  69. score();
  70. if(startanim==false){
  71. if(IsMouseButtonDown(0)){
  72. float x = GetMouseX()/32;
  73. float y = GetMouseY()/32;
  74. if(map[(int)x][(int)y]==0){
  75. if( map[(int)x][(int)y-1]>0 || map[(int)x-1][(int)y]>0 || map[(int)x+1][(int)y]>0){
  76. //DrawRectangle(x*32,y*32,32,32,DARKGRAY);
  77. startanim=true;
  78. animtargetx = x*32;
  79. animtargety = y*32;
  80. animx = posx;
  81. animy = posy;
  82. bool banana=true;
  83. while(banana){
  84. float an = getangle(animx,animy,animtargetx,animtargety);
  85. animx += cos(an)*1;
  86. animy += sin(an)*1;
  87. if(recttilecollide(animx,animy,32,32,0,0)==true){
  88. banana=false;
  89. startanim=false;
  90. animx = posx;
  91. animy = posy;
  92. focus=1;
  93. }
  94. if(rectsoverlap(animx-1,animy-1,32+2,32+2,animtargetx-1,animtargety-1,1,1)){
  95. banana=false;
  96. animtargetx = x*32;
  97. animtargety = y*32;
  98. animx = posx;
  99. animy = posy;
  100. focus=0;
  101. }
  102. }
  103. }}
  104. }
  105. }
  106. if(startanim){
  107. float an = getangle(animx,animy,animtargetx,animtargety);
  108. animx += cos(an)*8;
  109. animy += sin(an)*8;
  110. if(rectsoverlap(animx-1,animy-1,32+2,32+2,animtargetx-1,animtargety-1,32+2,32+2)){
  111. startanim=false;
  112. animx = posx;
  113. animy = posy;
  114. map[(int)animtargetx/32][(int)animtargety/32]=animcolor;
  115. animcolor = GetRandomValue(1,3);
  116. }
  117. }
  118. //----------------------------------------------------------------------------------
  119. // Draw
  120. //----------------------------------------------------------------------------------
  121. BeginDrawing();
  122. ClearBackground(RAYWHITE);
  123. for(int y=0;y<10;y++){
  124. for(int x=0;x<25;x++){
  125. if(map[x][y]==1){
  126. DrawRectangle(x*tileWidth,y*32,tileWidth,32,RED);
  127. DrawRectangle(x*tileWidth+1,y*32+1,tileWidth-2,32-2,RED);
  128. }
  129. if(map[x][y]==2){
  130. DrawRectangle(x*tileWidth,y*32,tileWidth,32,RED);
  131. DrawRectangle(x*tileWidth+1,y*32+1,tileWidth-2,32-2,GREEN);
  132. }
  133. if(map[x][y]==3){
  134. DrawRectangle(x*tileWidth,y*32,tileWidth,32,RED);
  135. DrawRectangle(x*tileWidth+1,y*32+1,tileWidth-2,32-2,BLUE);
  136. }
  137. }}
  138. if(animcolor==1){
  139. DrawRectangle(animx,animy,32,32,RED);
  140. DrawRectangle(animx+1,animy+1,32-2,32-2,RED);
  141. }
  142. if(animcolor==2){
  143. DrawRectangle(animx,animy,32,32,RED);
  144. DrawRectangle(animx+1,animy+1,32-2,32-2,GREEN);
  145. }
  146. if(animcolor==3){
  147. DrawRectangle(animx,animy,32,32,RED);
  148. DrawRectangle(animx+1,animy+1,32-2,32-2,BLUE);
  149. }
  150. float x = GetMouseX()/32;
  151. float y = GetMouseY()/32;
  152. if(map[(int)x][(int)y]==0){
  153. if( map[(int)x][(int)y-1]>0 || map[(int)x-1][(int)y]>0 || map[(int)x+1][(int)y]>0){
  154. DrawRectangle(x*32,y*32,32,32,DARKGRAY);
  155. }}
  156. float an = getangle(posx,posy,x*32,y*32);
  157. DrawLine(posx,posy,posx+cos(an)*32,posy+sin(an)*32,DARKGRAY);
  158. EndDrawing();
  159. //----------------------------------------------------------------------------------
  160. }
  161. // De-Initialization
  162. //--------------------------------------------------------------------------------------
  163. CloseWindow(); // Close window and OpenGL context
  164. //--------------------------------------------------------------------------------------
  165. return 0;
  166. }
  167. // Return the angle from - to in float
  168. float getangle(float x1,float y1,float x2,float y2){
  169. return (float)atan2(y2-y1, x2-x1);
  170. }
  171. // Rectangles overlap
  172. bool rectsoverlap(int x1,int y1,int w1,int h1,int x2,int y2,int w2,int h2){
  173. if(x1 >= (x2 + w2) || (x1 + w1) <= x2) return false;
  174. if(y1 >= (y2 + h2) || (y1 + h1) <= y2) return false;
  175. return true;
  176. }
  177. //Unit collide with solid blocks true/false
  178. bool recttilecollide(int x, int y,int w, int h, int offsetx,int offsety){
  179. int cx = (x+offsetx)/tileWidth;
  180. int cy = (y+offsety)/tileHeight;
  181. for(int y2=cy-1; y2<cy+2;y2++){//Note that the - and + are to be set differently with differently sized players
  182. for(int x2=cx-1; x2<cx+2;x2++){
  183. if(x2>=0 && x2<mapWidth && y2>=0 && y2<mapHeight){
  184. if(map[x2][y2] > 0){
  185. int x3 = (x2)*tileWidth;
  186. int y3 = (y2)*tileHeight;
  187. if(rectsoverlap(x+offsetx,y+offsety,w,h,x3,y3,tileWidth,tileHeight)){
  188. return true;
  189. }
  190. }
  191. }
  192. }}
  193. return false;
  194. }
  195. void score(){
  196. // Here we check if any of the same block types are in a certain shape.
  197. for(int y=1;y<20;y++){
  198. for(int x=0;x<100;x++){
  199. if(map[x][y]>0){
  200. // 2 width / 2 height
  201. if(map[x][y]==map[x][y] && map[x][y+1]==map[x][y]&& map[x+1][y]==map[x][y] && map[x+1][y+1]==map[x][y]){
  202. map[x][y]=0;
  203. map[x+1][y]=0;
  204. map[x][y+1]=0;
  205. map[x+1][y+1]=0;
  206. }
  207. // 3 width
  208. if(map[x][y]==map[x][y] && map[x+1][y]==map[x][y] && map[x+2][y]==map[x][y]){
  209. map[x][y]=0;
  210. map[x+1][y]=0;
  211. map[x+2][y]=0;
  212. }
  213. //
  214. // 3 height
  215. if(map[x][y]==map[x][y] && map[x][y+1]==map[x][y] && map[x][y+2]==map[x][y]){
  216. map[x][y]=0;
  217. map[x][y+1]=0;
  218. map[x][y+2]=0;
  219. }
  220. }
  221. }}
  222. }