jag

snake_c

public

Small snake game in C

Star0
Watch0
a227a4ff11148393a58b772715926bef8ced0783
2
ssh://git@git.jaglekon.de:30/jag/snake_c.git
source/animation/animation.cC151 lines2.8 KB
1
#include "animation.h"
2
3
#include "../read_input/read_input.h"
4
5
#include <jag_parse.h>
6
7
#define DELAY 10
8
9
#define KEY_PROTECTION 2
10
11
12
bool start_animation() {  
13
14
  struct winsize current_size;
15
  if ((ioctl(STDOUT_FILENO, TIOCGWINSZ, &current_size)) == -1)
16
    return false;
17
18
19
  char *snake[3][7] = {
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
    }
47
  };
48
49
  uint8_t width_snake[3] = {};
50
51
  {
52
    uint8_t i;
53
54
    for (i = 0; i < 3; i++)
55
      width_snake[i] = (uint8_t) strlen(snake[i][0]);
56
  }
57
58
  uint8_t middle;
59
60
  uint8_t height = 7;
61
  uint8_t width = width_snake[0] + width_snake[1] + width_snake[2];
62
63
  for (middle = 1; width < current_size.ws_col; middle++)
64
    width += width_snake[1];
65
66
  int32_t end = current_size.ws_col + width;
67
68
  uint8_t wait_incomplete = 0;
69
  bool key_lock = false;
70
71
  bool exit = false;
72
73
  while (end >= 0 && !exit) {
74
75
    ioctl(STDOUT_FILENO, TIOCGWINSZ, &current_size);
76
77
    int32_t start = end - width; 
78
79
    int32_t i, j;
80
  
81
    printf("\033[2J\033[H");
82
83
    for (j = 0; j < ((height > current_size.ws_row) ? current_size.ws_row : height) ; j++) {
84
85
    for (i = 0; i < current_size.ws_col; i++) {
86
      int32_t index = i - start;
87
88
      if (index >= 0 && index < width) {
89
        if (index < width_snake[0]) {
90
          printf("%c", snake[0][j][index]);
91
        } else if (index < width_snake[0] + width_snake[1] * middle) {
92
          printf("%c", snake[1][j][(index - width_snake[0]) % width_snake[1]]);
93
        } else {
94
          printf("%c", snake[2][j][index - (width_snake[0] + width_snake[1] * middle)]);
95
        }
96
      } else {
97
        printf(" ");
98
      }
99
    }      
100
    
101
      printf("\n");
102
103
    }
104
    fflush(stdout);
105
    
106
    end--;
107
108
109
    bool is_broken = false;
110
 
111
    if (wait_incomplete >= KEY_PROTECTION) {
112
     
113
      key_lock = false;
114
      wait_incomplete = 0;
115
      is_broken = true;
116
    }
117
118
119
    if (key_lock)
120
      wait_incomplete++;
121
122
123
    
124
    keypress *input;
125
    size_t input_len = 0;
126
127
    if (read_input(&input, &input_len, &key_lock, is_broken, DELAY)) {
128
   
129
      size_t k;
130
131
      for (k = 0; k < input_len; k++) {
132
        switch (input[k]) {
133
          case Q_KEY:
134
          case ESCAPE_KEY:
135
            exit = true;
136
            return false;
137
            break;
138
          case ENTER_KEY:
139
            end = -1; 
140
            break;
141
          default:
142
            break;
143
        } 
144
      }
145
146
      free_input(input);
147
    }
148
  }
149
150
  return exit ? false : true;
151
}