Advertisement
Guest User

Untitled

a guest
Dec 24th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.11 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <allegro.h>
  5. #include <allegro_primitives.h>
  6.  
  7. const int W = 500;
  8. const int H = 500;
  9. const float FPS = 15.0;
  10. const int recSize = 10;
  11. int gameOver = 1;
  12. int draw = 1;
  13. int moveSpeed = 10;
  14.  
  15. enum Direction { DOWN, UP, LEFT, RIGHT };
  16. enum Direction dir = DOWN;
  17.  
  18. struct Node
  19. {
  20.     int x, y;
  21. };
  22.  
  23. struct Snake
  24. {
  25.     int length;
  26.     struct Node *head;
  27.     struct Node *nodes;
  28. };
  29.  
  30. void logic        (struct Snake **snake, struct Node *fruit, struct Node **prev);
  31. void updatePrevPos(const struct Snake *snake, struct Node *previous);
  32. void updatePos    (struct Snake *snake, const struct Node *previous);
  33. void drawNodes    (const struct Snake *snake);
  34.  
  35. int main()
  36. {
  37.    if(!al_init())
  38.    {
  39.       fprintf(stderr, "Nie udalo sie zainicjalizowac Allegro!\n");
  40.       return -1;
  41.    }
  42.  
  43.    //SETUP
  44.    ALLEGRO_DISPLAY *display = al_create_display(W, H);
  45.    al_set_window_position(display, 700, 200);
  46.    al_set_window_title(display, "SNAKE");
  47.  
  48.    al_init_primitives_addon();
  49.    al_install_keyboard();
  50.  
  51.    ALLEGRO_TIMER *timer = al_create_timer(1.0 / FPS);
  52.    ALLEGRO_EVENT_QUEUE *event_q = al_create_event_queue();
  53.    al_register_event_source(event_q, al_get_keyboard_event_source());
  54.    al_register_event_source(event_q, al_get_timer_event_source(timer));
  55.    al_register_event_source(event_q, al_get_display_event_source(display));
  56.  
  57.    al_start_timer(timer);
  58.  
  59.    if(!display)
  60.    {
  61.       fprintf(stderr, "Nie udalo sie stworzyc okna!\n");
  62.       return -1;
  63.    }
  64.  
  65.    ALLEGRO_COLOR black = al_map_rgb(0, 0, 0);
  66.    ALLEGRO_COLOR white = al_map_rgb(255,255,255);
  67.    ALLEGRO_COLOR rand_color;
  68.  
  69.    struct Snake *snake = malloc(sizeof(struct Snake));
  70.    snake->length   = 1;
  71.    snake->head     = (struct Node *)malloc(sizeof(struct Node));
  72.    snake->nodes    = (struct Node *)malloc(sizeof(struct Node));
  73.    snake->head->x  = W / 2;
  74.    snake->head->y  = H / 2;
  75.  
  76.    struct Node *prev_positions = (struct Node *)malloc(sizeof(struct Node));
  77.  
  78.    struct Node *fruit = (struct Node *)malloc(sizeof(struct Node));
  79.    srand(time(NULL));
  80.    fruit->x = (rand() % (W / recSize - 1) + 1) * recSize;
  81.    fruit->y = (rand() % (H / recSize - 1) + 1) * recSize;
  82.  
  83.    while(gameOver)
  84.    {
  85.         ALLEGRO_EVENT events;
  86.         al_wait_for_event(event_q, &events);
  87.  
  88.         updatePrevPos(snake, prev_positions);           //aktualizacja pozycji ogona z poprzedniej klatki
  89.  
  90.         //OBSLUGA KLAWIATURY I OKNA
  91.         if (events.type == ALLEGRO_EVENT_KEY_DOWN)
  92.         {
  93.             switch(events.keyboard.keycode)
  94.             {
  95.                 case ALLEGRO_KEY_UP:
  96.                     if (dir != DOWN && snake->length > 1)
  97.                         dir = UP;
  98.                     else if (snake->length <= 1)
  99.                         dir = UP;
  100.                     break;
  101.  
  102.                 case ALLEGRO_KEY_DOWN:
  103.                     if (dir != UP && snake->length > 1)
  104.                         dir = DOWN;
  105.                     else if (snake->length <= 1)
  106.                         dir = DOWN;
  107.                     break;
  108.  
  109.                 case ALLEGRO_KEY_LEFT:
  110.                     if (dir != RIGHT && snake->length > 1)
  111.                         dir = LEFT;
  112.                     else if (snake->length <= 1)
  113.                         dir = LEFT;
  114.                     break;
  115.  
  116.                 case ALLEGRO_KEY_RIGHT:
  117.                     if (dir != LEFT && snake->length > 1)
  118.                         dir = RIGHT;
  119.                     else if (snake->length <= 1)
  120.                         dir = RIGHT;
  121.                     break;
  122.  
  123.                 case ALLEGRO_KEY_ESCAPE:
  124.                     gameOver = 0;
  125.                     break;
  126.             }
  127.         }
  128.         else if (events.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
  129.             gameOver = 0;
  130.  
  131.         if (events.type == ALLEGRO_EVENT_TIMER)
  132.         {
  133.             switch(dir)
  134.             {
  135.                 case UP:
  136.                     snake->head->y -= moveSpeed;
  137.                     break;
  138.  
  139.                 case DOWN:
  140.                     snake->head->y += moveSpeed;
  141.                     break;
  142.  
  143.                 case LEFT:
  144.                     snake->head->x -= moveSpeed;
  145.                     break;
  146.  
  147.                 case RIGHT:
  148.                     snake->head->x += moveSpeed;
  149.                     break;
  150.             }
  151.             draw = 1;
  152.         }
  153.  
  154.         if (draw && gameOver != 0)
  155.         {
  156.             draw = 0;
  157.  
  158.             if (ALLEGRO_EVENT_TIMER % 15 == 0)
  159.                 rand_color = al_map_rgb(rand() % 255 + 1, rand() % 255 + 1, rand() % 255 + 1);
  160.             al_draw_filled_rectangle(fruit->x, fruit->y, fruit->x + recSize, fruit->y + recSize, rand_color);
  161.  
  162.             updatePos(snake, prev_positions);           //aktualizacja pozycji ogona z aktualnej klatki
  163.             drawNodes(snake);                           //rysowanie weza na ekran
  164.  
  165.             al_flip_display();
  166.             al_clear_to_color(black);
  167.         }
  168.  
  169.         logic(&snake, fruit, &prev_positions);          //logika gry
  170.    }
  171.  
  172.    free(snake);
  173.    free(prev_positions);
  174.    free(fruit);
  175.  
  176.    al_destroy_event_queue(event_q);
  177.    al_destroy_timer(timer);
  178.    al_destroy_display(display);
  179.  
  180.    return 0;
  181. }
  182.  
  183. void logic(struct Snake **snake, struct Node *fruit, struct Node **prev)
  184. {
  185.     //KOLIZJA Z GRANICAMI PLANSZY
  186.     if ((*snake)->head->y < 0)
  187.         gameOver = 0;
  188.  
  189.     if ((*snake)->head->y > H - recSize)
  190.         gameOver = 0;
  191.  
  192.     if ((*snake)->head->x < 0)
  193.         gameOver = 0;
  194.  
  195.     if ((*snake)->head->x > W - recSize)
  196.         gameOver = 0;
  197.  
  198.     //ZJEDZENIE OWOCU
  199.     if ((*snake)->head->x == fruit->x && (*snake)->head->y == fruit->y)
  200.     {
  201.         (*snake)->length++;
  202.         (*snake)->nodes = (struct Node *)realloc((*snake)->nodes, (*snake)->length * sizeof(struct Node));
  203.         *prev = (struct Node *)realloc(*prev, (*snake)->length * sizeof(struct Node));
  204.  
  205.         fruit->x = (rand() % (W / recSize - 1) + 1) * recSize;
  206.         fruit->y = (rand() % (H / recSize - 1) + 1) * recSize;
  207.     }
  208.  
  209.     //KOLIZJA Z OGONEM
  210.     for (int i = 0; i < (*snake)->length; i++)
  211.         if ((*snake)->head->x == (*snake)->nodes[i].x && (*snake)->head->y == (*snake)->nodes[i].y)
  212.             gameOver = 0;
  213. }
  214.  
  215. void updatePrevPos(const struct Snake *snake, struct Node *previous)
  216. {
  217.      previous[0].x = snake->head->x;
  218.      previous[0].y = snake->head->y;
  219.  
  220.      for (int i = 1; i < snake->length; i++)
  221.      {
  222.         previous[i].x = snake->nodes[i - 1].x;
  223.         previous[i].y = snake->nodes[i - 1].y;
  224.      }
  225. }
  226.  
  227. void updatePos(struct Snake *snake, const struct Node *previous)
  228. {
  229.     for (int i = 0; i < snake->length; i++)
  230.     {
  231.         snake->nodes[i].x = previous[i].x;
  232.         snake->nodes[i].y = previous[i].y;
  233.     }
  234. }
  235.  
  236. void drawNodes(const struct Snake *snake)
  237. {
  238.     al_draw_rectangle(snake->head->x + 1, snake->head->y + 1, snake->head->x + recSize, snake->head->y + recSize, al_map_rgb(255,255,255), 1.0);
  239.  
  240.     for (int i = 0; i < snake->length; i++)
  241.         al_draw_rectangle(snake->nodes[i].x + 1, snake->nodes[i].y + 1, snake->nodes[i].x + recSize, snake->nodes[i].y + recSize, al_map_rgb(255,255,255), 1.0);
  242. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement