akadjoker

bullets

Jul 29th, 2023
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.80 KB | None | 0 0
  1. #include <raylib.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5.  
  6. #define SCREEN_WIDTH 800
  7. #define SCREEN_HEIGHT 450
  8.  
  9. typedef struct Object
  10. {
  11. int x;
  12. int y;
  13. int id;
  14. bool live;
  15. bool visible;
  16. char* type;
  17. struct Object *next;
  18. }Object;
  19.  
  20.  
  21. Object *head=NULL;
  22.  
  23. int count=0;
  24.  
  25.  
  26. Object* CreateObject(int x, int y, const char* type)
  27. {
  28. Object * obj = malloc(sizeof (Object));
  29. obj->live=true;
  30. obj->visible=true;
  31. obj->next=NULL;
  32. obj->x=x;
  33. obj->y=y;
  34. obj->type=strdup(type);
  35. obj->id=count;
  36. Add(obj);
  37. return obj;
  38. }
  39.  
  40.  
  41. void Add(Object *obj)
  42. {
  43.  
  44. if (!head)
  45. {
  46. head = obj;
  47. } else
  48. {
  49. Object* tmp = head;
  50. while(tmp->next)
  51. {
  52. tmp=tmp->next;
  53. }
  54. tmp->next=obj;
  55. }
  56.  
  57. count++;
  58. }
  59.  
  60. void Delete(Object *obj)
  61. {
  62. if (!obj)
  63. return ;
  64. printf("Delete %d %s \n",obj->id,obj->type);
  65. if (obj->type!=NULL)
  66. free(obj->type);
  67. free(obj);
  68. obj=NULL;
  69. count--;
  70.  
  71. }
  72.  
  73. void RemoveDeath()
  74. {
  75. if (!head)
  76. return;
  77.  
  78.  
  79. Object* current=head;
  80. Object* prev=NULL;
  81.  
  82.  
  83. while(current && current->live)
  84. {
  85. if (current->next==NULL)
  86. return ;
  87. else
  88. {
  89. prev= current;
  90. current=current->next;
  91. }
  92. }
  93. if (current == head)
  94. {
  95. head= head->next;
  96. }
  97. else
  98. prev->next=current->next;
  99.  
  100.  
  101. Delete(current);
  102. current=NULL;
  103. }
  104.  
  105. Object* Collide(const char* type,Object *object)
  106. {
  107. if (!head && !object)
  108. return NULL;
  109. Object* hit=head;
  110.  
  111.  
  112. while(hit && hit!=object)
  113. {
  114.  
  115.  
  116. if (strcmp(type,hit->type)==0)
  117. {
  118. if (hit->live)
  119. {
  120. if (CheckCollisionCircleRec((Vector2){object->x,object->y},5,(Rectangle){hit->x,hit->y,20,60}))
  121. {
  122. return hit;
  123. }
  124. }
  125.  
  126.  
  127. }
  128. hit=hit->next;
  129. }
  130. return NULL;
  131. }
  132.  
  133. void GameUpdate()
  134. {
  135. if (!head)
  136. return;
  137. Object* obj=head;
  138.  
  139. // RemoveDeath();
  140. while(obj!=NULL)
  141. {
  142. // printf("Update %d %s \n",obj->id,obj->type);
  143. if (strcmp("bullet",obj->type)==0)
  144. {
  145. if (obj->live)
  146. {
  147. obj->y -= 2;
  148. if (obj->y<=10)
  149. {
  150. obj->live=false;
  151. obj->visible=false;
  152. }
  153. Object* hit =Collide("enemy",obj);
  154. if (hit!=NULL)
  155. {
  156. obj->live=false;
  157. obj->visible=false;
  158. hit->live=false;
  159. hit->visible=false;
  160. }
  161. }
  162.  
  163.  
  164. } else
  165. if (strcmp("player",obj->type)==0)
  166. {
  167. if (IsKeyDown(KEY_LEFT))
  168. obj->x-=8;
  169. else
  170. if (IsKeyDown(KEY_RIGHT))
  171. obj->x+=8;
  172.  
  173. if (obj->x<=20)
  174. obj->x=20;
  175. if (obj->x>=800-20)
  176. obj->x=800-20;
  177. if (IsKeyReleased(KEY_SPACE))
  178. {
  179. CreateObject(obj->x+10,obj->y-20,"bullet");
  180. }
  181.  
  182.  
  183. }else
  184. if (strcmp("enemy",obj->type)==0)
  185. {
  186.  
  187. }
  188.  
  189. obj=obj->next;
  190. }
  191. RemoveDeath();
  192.  
  193.  
  194. }
  195.  
  196.  
  197. void GameRender()
  198. {
  199. if (!head)
  200. return;
  201. Object* obj=head;
  202.  
  203.  
  204. while(obj!=NULL)
  205. {
  206. if (obj->visible)
  207. {
  208. if (strcmp("bullet",obj->type)==0)
  209. {
  210.  
  211. DrawCircle(obj->x,obj->y,5,GREEN);
  212.  
  213. } else
  214. if (strcmp("player",obj->type)==0)
  215. {
  216. DrawRectangle(obj->x,obj->y,20,60,LIME);
  217. }else
  218. if (strcmp("enemy",obj->type)==0)
  219. {
  220. DrawRectangle(obj->x,obj->y,20,60,RED);
  221. }
  222. }
  223. obj=obj->next;
  224. }
  225.  
  226. }
  227.  
  228. void Clear()
  229. {
  230. if (!head)
  231. return ;
  232. Object* tmp=head;
  233. while(tmp)
  234. {
  235. head = head->next;
  236. Delete(tmp);
  237. tmp = head;
  238. }
  239. }
  240.  
  241.  
  242.  
  243.  
  244. int main()
  245. {
  246.  
  247. CreateObject(200,100,"enemy");
  248. CreateObject(600,100,"enemy");
  249.  
  250. CreateObject(800/2,360,"player");
  251.  
  252.  
  253.  
  254.  
  255.  
  256.  
  257. InitWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "bullets ");
  258. SetTargetFPS(60);
  259.  
  260. while (!WindowShouldClose())
  261. {
  262. GameUpdate();
  263.  
  264. BeginDrawing();
  265. ClearBackground(BLACK) ;
  266. GameRender();
  267. DrawText(TextFormat("Objects %d ",count),20,20,16,RED);
  268. EndDrawing();
  269.  
  270. }
  271. CloseWindow();
  272.  
  273.  
  274. Clear();
  275. return 0;
  276. }
  277.  
Advertisement
Add Comment
Please, Sign In to add comment