jag

snake_c

public

Small snake game in C

Star0
Watch0
9eb5767db864760d9c9d331d4dcaa3812c49b0b5
2
ssh://git@git.jaglekon.de:30/jag/snake_c.git
snake.cC138 lines2.6 KB
1
#include <unistd.h>
2
3
#include <stdio.h>
4
#include <stdint.h>
5
#include <stdbool.h>
6
7
#include <string.h>
8
9
#include <termios.h>
10
11
#define RIGHT_CODE 'C'
12
#define LEFT_CODE 'D'
13
#define UP_CODE 'A'
14
#define DOWN_CODE 'B'
15
16
#define WIDTH 4
17
#define HEIGHT 4
18
19
typedef enum {
20
  UP, DOWN, LEFT, RIGHT
21
} direction;
22
23
typedef struct selected {
24
  uint8_t x;
25
  uint8_t y;
26
} selected;
27
28
29
void draw_board(selected square_selected);
30
31
32
int main() {
33
34
  struct termios old, new;
35
36
  tcgetattr(STDIN_FILENO, &old);
37
38
  new = old;
39
40
  new.c_lflag &= ~(ECHO | ICANON);
41
  new.c_cc[VMIN] = 0;
42
  new.c_cc[VTIME] = 1;
43
44
  tcsetattr(STDIN_FILENO, TCSANOW, &new);
45
46
47
  selected square_selected = {0, 0};
48
49
  draw_board(square_selected);
50
51
  bool run = true;
52
53
  while (run) {
54
55
    char string[1000] = {};
56
    ssize_t n = read(STDIN_FILENO, string, sizeof(string) - 1);
57
    
58
59
    if (n > 0) {
60
      string[n] = '\0';
61
62
      if (strlen(string) >= 3) {
63
        if (string[0] == '\033') {
64
          if (string[1] == '[') {
65
          
66
            direction shift;
67
          
68
            switch (string[2]) {
69
              case RIGHT_CODE:
70
                shift = RIGHT;
71
                break;
72
              case LEFT_CODE:
73
                shift = LEFT;
74
                break;
75
              case UP_CODE:
76
                shift = UP;
77
                break;
78
              case DOWN_CODE:
79
                shift = DOWN;
80
                break;
81
            }
82
83
            switch (shift) {
84
              case UP:
85
                square_selected.y = ((square_selected.y + HEIGHT - 1) % HEIGHT);
86
                break;
87
              case DOWN:
88
                square_selected.y = ((square_selected.y + 1) % HEIGHT);
89
                break;
90
              case LEFT:
91
                square_selected.x = ((square_selected.x + WIDTH - 1) % WIDTH);
92
                break;
93
              case RIGHT:
94
                square_selected.x = ((square_selected.x + 1) % WIDTH);
95
                break;
96
            }
97
            
98
            uint8_t i;
99
            for (i = 0; i < HEIGHT * 2; i++)
100
              printf("\033[A");
101
            printf("\r");
102
103
            draw_board(square_selected);
104
          
105
          }
106
        }
107
      }
108
      if (strstr(string, "q"))
109
        run = false;
110
    }
111
  }
112
113
114
  tcsetattr(STDIN_FILENO, TCSANOW, &old);
115
116
  return 0;
117
}
118
119
120
void draw_board (selected square_selected) {
121
  
122
  uint8_t i, j; 
123
124
  for ( i = 0; i < HEIGHT; i++) {
125
    for (j = 0; j < WIDTH; j++) {
126
      char character = 'X';
127
      uint8_t color = 31;
128
129
      if (i == square_selected.y && j == square_selected.x) {
130
        character = 'O';
131
        color = 32;
132
      }
133
134
      printf("\033[%um%5c\033[0m", color,color,  character);
135
    }
136
    printf("\n\n");
137
  }
138
}