jag

snake_c

public

Small snake game in C

Star0
Watch0
96e537edae6d1395366a3373a36ff008ef48c423
2
ssh://git@git.jaglekon.de:30/jag/snake_c.git
source/animation/animation.cC166 lines3.1 KB
1
#include "../snake.h"
2
#include <jag_parse.h>
3
#include <poll.h>
4
5
#define DELAY 10
6
7
#define KEY_PROTECTION 2
8
9
bool start_animation() {  
10
11
  struct winsize current_size;
12
  if ((ioctl(STDOUT_FILENO, TIOCGWINSZ, &current_size)) == -1)
13
    return false;
14
15
16
  char *snake[3][7] = {
17
    {
18
      "           ________  ",
19
      "         _/ _      \\_",
20
      "________/  (_)       ",
21
      "\\       \\            ",
22
      "/_______/   _        ",
23
      "        \\_ (_)      _",
24
      "          \\________/ "
25
    },
26
    {
27
      "        ",
28
      "________",
29
      "\\      /",
30
      " \\    / ",
31
      "  \\  /  ",
32
      "___\\/___",
33
      "        ",
34
    },
35
    {
36
      "         ",
37
      "__        ",
38
      "  \\_____ ",
39
      "        \\",
40
      "   _____/",
41
      "__/      ",
42
      "         "
43
    }
44
  };
45
46
  uint8_t width_snake[3] = {};
47
48
  {
49
    uint8_t i;
50
51
    for (i = 0; i < 3; i++)
52
      width_snake[i] = (uint8_t) strlen(snake[i][0]);
53
  }
54
55
  uint8_t middle;
56
57
  uint8_t height = 7;
58
  uint8_t width = width_snake[0] + width_snake[1] + width_snake[2];
59
60
  for (middle = 1; width < current_size.ws_col; middle++)
61
    width += width_snake[1];
62
63
  int32_t end = current_size.ws_col + width;
64
65
66
  struct pollfd polling[1];
67
68
  polling[0].fd = STDIN_FILENO;
69
  polling[0].events = POLLIN;
70
71
  uint8_t wait_incomplete = 0;
72
  bool key_lock = false;
73
74
  while (end >= 0) {
75
76
    ioctl(STDOUT_FILENO, TIOCGWINSZ, &current_size);
77
78
    int32_t start = end - width; 
79
80
    int32_t i, j;
81
  
82
    printf("\033[2J\033[H");
83
84
    for (j = 0; j < ((height > current_size.ws_row) ? current_size.ws_row : height) ; j++) {
85
86
    for (i = 0; i < current_size.ws_col; i++) {
87
      int32_t index = i - start;
88
89
      if (index >= 0 && index < width) {
90
        if (index < width_snake[0]) {
91
          printf("%c", snake[0][j][index]);
92
        } else if (index < width_snake[0] + width_snake[1] * middle) {
93
          printf("%c", snake[1][j][(index - width_snake[0]) % width_snake[1]]);
94
        } else {
95
          printf("%c", snake[2][j][index - (width_snake[0] + width_snake[1] * middle)]);
96
        }
97
      } else {
98
        printf(" ");
99
      }
100
    }      
101
    
102
      printf("\n");
103
104
    }
105
    fflush(stdout);
106
    
107
    end--;
108
109
    keypress input[20] = {};
110
    size_t input_len = 0;
111
112
    if (key_lock)
113
      wait_incomplete++;
114
115
    if (wait_incomplete >= KEY_PROTECTION) {
116
      input[0] = ESCAPE_KEY;
117
      input_len++;
118
119
      key_lock = false;
120
      wait_incomplete = 0;
121
122
      break_unfinished();
123
    }
124
125
    int is_poll = poll(polling, 1, DELAY);
126
127
    char string[256] = {};
128
    ssize_t length_string = read(STDIN_FILENO, string, sizeof(string) - 1);
129
 
130
    add_to_buf(string, length_string);
131
132
        
133
    size_t k = input_len;
134
135
    input[k] = UNKNOWN;
136
137
    while (input[k] != NO_KEY) {
138
139
      input[k] = parse_input();
140
141
      if (input[k] == INCOMPLETE) {
142
        key_lock = true;
143
        break;
144
      } 
145
146
      k++;
147
    }
148
149
    input_len = k;
150
151
    for (k = 0; k < input_len; k++) {
152
      switch (input[k]) {
153
        case Q_KEY:
154
        case ESCAPE_KEY:
155
          return false;
156
          break;
157
        case ENTER_KEY:
158
          end = -1; 
159
          break;
160
        default:
161
          break;
162
      } 
163
    }
164
  }
165
  return true;
166
}