Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #include <allegro.h>
- #include <allegro_primitives.h>
- const int W = 500;
- const int H = 500;
- const float FPS = 15.0;
- const int recSize = 10;
- int gameOver = 1;
- int draw = 1;
- int moveSpeed = 10;
- enum Direction { DOWN, UP, LEFT, RIGHT };
- enum Direction dir = DOWN;
- struct Node
- {
- int x, y;
- };
- struct Snake
- {
- int length;
- struct Node *head;
- struct Node *nodes;
- };
- void logic (struct Snake **snake, struct Node *fruit, struct Node **prev);
- void updatePrevPos(const struct Snake *snake, struct Node *previous);
- void updatePos (struct Snake *snake, const struct Node *previous);
- void drawNodes (const struct Snake *snake);
- int main()
- {
- if(!al_init())
- {
- fprintf(stderr, "Nie udalo sie zainicjalizowac Allegro!\n");
- return -1;
- }
- //SETUP
- ALLEGRO_DISPLAY *display = al_create_display(W, H);
- al_set_window_position(display, 700, 200);
- al_set_window_title(display, "SNAKE");
- al_init_primitives_addon();
- al_install_keyboard();
- ALLEGRO_TIMER *timer = al_create_timer(1.0 / FPS);
- ALLEGRO_EVENT_QUEUE *event_q = al_create_event_queue();
- al_register_event_source(event_q, al_get_keyboard_event_source());
- al_register_event_source(event_q, al_get_timer_event_source(timer));
- al_register_event_source(event_q, al_get_display_event_source(display));
- al_start_timer(timer);
- if(!display)
- {
- fprintf(stderr, "Nie udalo sie stworzyc okna!\n");
- return -1;
- }
- ALLEGRO_COLOR black = al_map_rgb(0, 0, 0);
- ALLEGRO_COLOR white = al_map_rgb(255,255,255);
- ALLEGRO_COLOR rand_color;
- struct Snake *snake = malloc(sizeof(struct Snake));
- snake->length = 1;
- snake->head = (struct Node *)malloc(sizeof(struct Node));
- snake->nodes = (struct Node *)malloc(sizeof(struct Node));
- snake->head->x = W / 2;
- snake->head->y = H / 2;
- struct Node *prev_positions = (struct Node *)malloc(sizeof(struct Node));
- struct Node *fruit = (struct Node *)malloc(sizeof(struct Node));
- srand(time(NULL));
- fruit->x = (rand() % (W / recSize - 1) + 1) * recSize;
- fruit->y = (rand() % (H / recSize - 1) + 1) * recSize;
- while(gameOver)
- {
- ALLEGRO_EVENT events;
- al_wait_for_event(event_q, &events);
- updatePrevPos(snake, prev_positions); //aktualizacja pozycji ogona z poprzedniej klatki
- //OBSLUGA KLAWIATURY I OKNA
- if (events.type == ALLEGRO_EVENT_KEY_DOWN)
- {
- switch(events.keyboard.keycode)
- {
- case ALLEGRO_KEY_UP:
- if (dir != DOWN && snake->length > 1)
- dir = UP;
- else if (snake->length <= 1)
- dir = UP;
- break;
- case ALLEGRO_KEY_DOWN:
- if (dir != UP && snake->length > 1)
- dir = DOWN;
- else if (snake->length <= 1)
- dir = DOWN;
- break;
- case ALLEGRO_KEY_LEFT:
- if (dir != RIGHT && snake->length > 1)
- dir = LEFT;
- else if (snake->length <= 1)
- dir = LEFT;
- break;
- case ALLEGRO_KEY_RIGHT:
- if (dir != LEFT && snake->length > 1)
- dir = RIGHT;
- else if (snake->length <= 1)
- dir = RIGHT;
- break;
- case ALLEGRO_KEY_ESCAPE:
- gameOver = 0;
- break;
- }
- }
- else if (events.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
- gameOver = 0;
- if (events.type == ALLEGRO_EVENT_TIMER)
- {
- switch(dir)
- {
- case UP:
- snake->head->y -= moveSpeed;
- break;
- case DOWN:
- snake->head->y += moveSpeed;
- break;
- case LEFT:
- snake->head->x -= moveSpeed;
- break;
- case RIGHT:
- snake->head->x += moveSpeed;
- break;
- }
- draw = 1;
- }
- if (draw && gameOver != 0)
- {
- draw = 0;
- if (ALLEGRO_EVENT_TIMER % 15 == 0)
- rand_color = al_map_rgb(rand() % 255 + 1, rand() % 255 + 1, rand() % 255 + 1);
- al_draw_filled_rectangle(fruit->x, fruit->y, fruit->x + recSize, fruit->y + recSize, rand_color);
- updatePos(snake, prev_positions); //aktualizacja pozycji ogona z aktualnej klatki
- drawNodes(snake); //rysowanie weza na ekran
- al_flip_display();
- al_clear_to_color(black);
- }
- logic(&snake, fruit, &prev_positions); //logika gry
- }
- free(snake);
- free(prev_positions);
- free(fruit);
- al_destroy_event_queue(event_q);
- al_destroy_timer(timer);
- al_destroy_display(display);
- return 0;
- }
- void logic(struct Snake **snake, struct Node *fruit, struct Node **prev)
- {
- //KOLIZJA Z GRANICAMI PLANSZY
- if ((*snake)->head->y < 0)
- gameOver = 0;
- if ((*snake)->head->y > H - recSize)
- gameOver = 0;
- if ((*snake)->head->x < 0)
- gameOver = 0;
- if ((*snake)->head->x > W - recSize)
- gameOver = 0;
- //ZJEDZENIE OWOCU
- if ((*snake)->head->x == fruit->x && (*snake)->head->y == fruit->y)
- {
- (*snake)->length++;
- (*snake)->nodes = (struct Node *)realloc((*snake)->nodes, (*snake)->length * sizeof(struct Node));
- *prev = (struct Node *)realloc(*prev, (*snake)->length * sizeof(struct Node));
- fruit->x = (rand() % (W / recSize - 1) + 1) * recSize;
- fruit->y = (rand() % (H / recSize - 1) + 1) * recSize;
- }
- //KOLIZJA Z OGONEM
- for (int i = 0; i < (*snake)->length; i++)
- if ((*snake)->head->x == (*snake)->nodes[i].x && (*snake)->head->y == (*snake)->nodes[i].y)
- gameOver = 0;
- }
- void updatePrevPos(const struct Snake *snake, struct Node *previous)
- {
- previous[0].x = snake->head->x;
- previous[0].y = snake->head->y;
- for (int i = 1; i < snake->length; i++)
- {
- previous[i].x = snake->nodes[i - 1].x;
- previous[i].y = snake->nodes[i - 1].y;
- }
- }
- void updatePos(struct Snake *snake, const struct Node *previous)
- {
- for (int i = 0; i < snake->length; i++)
- {
- snake->nodes[i].x = previous[i].x;
- snake->nodes[i].y = previous[i].y;
- }
- }
- void drawNodes(const struct Snake *snake)
- {
- 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);
- for (int i = 0; i < snake->length; i++)
- 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);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement