Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <raylib.h>
- #include <string.h>
- #include <stdlib.h>
- #include <stdio.h>
- #define SCREEN_WIDTH 800
- #define SCREEN_HEIGHT 450
- typedef struct Object
- {
- int x;
- int y;
- int id;
- bool live;
- bool visible;
- char* type;
- struct Object *next;
- }Object;
- Object *head=NULL;
- int count=0;
- Object* CreateObject(int x, int y, const char* type)
- {
- Object * obj = malloc(sizeof (Object));
- obj->live=true;
- obj->visible=true;
- obj->next=NULL;
- obj->x=x;
- obj->y=y;
- obj->type=strdup(type);
- obj->id=count;
- Add(obj);
- return obj;
- }
- void Add(Object *obj)
- {
- if (!head)
- {
- head = obj;
- } else
- {
- Object* tmp = head;
- while(tmp->next)
- {
- tmp=tmp->next;
- }
- tmp->next=obj;
- }
- count++;
- }
- void Delete(Object *obj)
- {
- if (!obj)
- return ;
- printf("Delete %d %s \n",obj->id,obj->type);
- if (obj->type!=NULL)
- free(obj->type);
- free(obj);
- obj=NULL;
- count--;
- }
- void RemoveDeath()
- {
- if (!head)
- return;
- Object* current=head;
- Object* prev=NULL;
- while(current && current->live)
- {
- if (current->next==NULL)
- return ;
- else
- {
- prev= current;
- current=current->next;
- }
- }
- if (current == head)
- {
- head= head->next;
- }
- else
- prev->next=current->next;
- Delete(current);
- current=NULL;
- }
- Object* Collide(const char* type,Object *object)
- {
- if (!head && !object)
- return NULL;
- Object* hit=head;
- while(hit && hit!=object)
- {
- if (strcmp(type,hit->type)==0)
- {
- if (hit->live)
- {
- if (CheckCollisionCircleRec((Vector2){object->x,object->y},5,(Rectangle){hit->x,hit->y,20,60}))
- {
- return hit;
- }
- }
- }
- hit=hit->next;
- }
- return NULL;
- }
- void GameUpdate()
- {
- if (!head)
- return;
- Object* obj=head;
- // RemoveDeath();
- while(obj!=NULL)
- {
- // printf("Update %d %s \n",obj->id,obj->type);
- if (strcmp("bullet",obj->type)==0)
- {
- if (obj->live)
- {
- obj->y -= 2;
- if (obj->y<=10)
- {
- obj->live=false;
- obj->visible=false;
- }
- Object* hit =Collide("enemy",obj);
- if (hit!=NULL)
- {
- obj->live=false;
- obj->visible=false;
- hit->live=false;
- hit->visible=false;
- }
- }
- } else
- if (strcmp("player",obj->type)==0)
- {
- if (IsKeyDown(KEY_LEFT))
- obj->x-=8;
- else
- if (IsKeyDown(KEY_RIGHT))
- obj->x+=8;
- if (obj->x<=20)
- obj->x=20;
- if (obj->x>=800-20)
- obj->x=800-20;
- if (IsKeyReleased(KEY_SPACE))
- {
- CreateObject(obj->x+10,obj->y-20,"bullet");
- }
- }else
- if (strcmp("enemy",obj->type)==0)
- {
- }
- obj=obj->next;
- }
- RemoveDeath();
- }
- void GameRender()
- {
- if (!head)
- return;
- Object* obj=head;
- while(obj!=NULL)
- {
- if (obj->visible)
- {
- if (strcmp("bullet",obj->type)==0)
- {
- DrawCircle(obj->x,obj->y,5,GREEN);
- } else
- if (strcmp("player",obj->type)==0)
- {
- DrawRectangle(obj->x,obj->y,20,60,LIME);
- }else
- if (strcmp("enemy",obj->type)==0)
- {
- DrawRectangle(obj->x,obj->y,20,60,RED);
- }
- }
- obj=obj->next;
- }
- }
- void Clear()
- {
- if (!head)
- return ;
- Object* tmp=head;
- while(tmp)
- {
- head = head->next;
- Delete(tmp);
- tmp = head;
- }
- }
- int main()
- {
- CreateObject(200,100,"enemy");
- CreateObject(600,100,"enemy");
- CreateObject(800/2,360,"player");
- InitWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "bullets ");
- SetTargetFPS(60);
- while (!WindowShouldClose())
- {
- GameUpdate();
- BeginDrawing();
- ClearBackground(BLACK) ;
- GameRender();
- DrawText(TextFormat("Objects %d ",count),20,20,16,RED);
- EndDrawing();
- }
- CloseWindow();
- Clear();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment