jag

snake_c

public

Small snake game in C

Star0
Watch0
a86bb596741adfd69cc4495c40666e7051cc3861
2
ssh://git@git.jaglekon.de:30/jag/snake_c.git
source/snake.hC114 lines2.5 KB
1
#ifndef SNAKE_H
2
#define SNAKE_H
3
4
5
#define _POSIX_C_SOURCE 199309L
6
7
#include <termios.h>
8
#include <unistd.h>
9
10
#include <stdbool.h>
11
#include <stdint.h>
12
#include <stdio.h>
13
#include <stdlib.h>
14
15
#include <sys/ioctl.h>
16
#include <sys/stat.h>
17
18
#include <errno.h>
19
20
#include <time.h>
21
22
#include <ctype.h>
23
#include <string.h>
24
25
#define RIGHT_CODE 'C'
26
#define LEFT_CODE 'D'
27
#define UP_CODE 'A'
28
#define DOWN_CODE 'B'
29
30
#define WIDTH 20
31
#define HEIGHT 20
32
33
#define MIN_SIZE 10
34
#define MAX_SIZE 100
35
36
#define STRING_LEN 20
37
38
#define SCORE_LEN 10
39
#define SCORE_NUMBER 10
40
41
#define SPEED 50 /* in ms */
42
43
#define MAX_LENGTH 1000
44
#define STARTING_LENGTH 2
45
46
#define SETTINGS_FILE "settings"
47
#define SCORE_FILE "score"
48
#define SETTINGS_FOLDER "snake_c"
49
50
typedef enum { NEW_GAME, SETTINGS, HIGHSCORE, EXIT, MAIN_MENU } main_menu;
51
52
typedef enum {HEIGHT_S, WIDTH_S, SPEED_S, BACK_S, SETTINGS_MENU} settings;
53
54
typedef enum {V_FAST, FAST, AVERAGE, SLOW, V_SLOW, SPEED_L} speed;
55
56
typedef enum {
57
  UP, LEFT, DOWN, RIGHT, DIRECTIONS,
58
  ENTER, P_KEY, Q_KEY, ESCAPE, DEL
59
} direction;
60
61
typedef enum {
62
  AIR, FRUIT, HEAD_1, HEAD_2, HORIZONTAL, VERTICAL, 
63
  TOP_RIGHT, TOP_LEFT, BOTTOM_RIGHT, BOTTOM_LEFT
64
} board_state;
65
66
typedef struct selected {
67
  int8_t x;
68
  int8_t y;
69
} selected;
70
71
typedef struct snake_part {
72
  selected coords;
73
  direction directions[2];
74
} snake_part;
75
76
typedef struct snake {
77
  snake_part * position;
78
  uint16_t length;
79
} snake;
80
81
typedef struct score {
82
  char name[SCORE_LEN];
83
  uint16_t points;
84
} score;
85
86
uint8_t update_snake (selected game_size, snake *my_snake, direction shift);
87
void update_field(snake my_snake, selected game_size, 
88
  board_state **board, bool init);
89
selected new_fruit(selected game_size, board_state **board);
90
91
bool start_animation();
92
93
bool draw_menu(main_menu menu, struct winsize window_size);
94
void draw_settings(settings selected_setting, speed game_speed, selected game_size, struct winsize window_size);
95
96
bool draw_board(selected game_size, board_state **board,
97
  struct winsize window_size, bool redraw, uint8_t pause, uint16_t score);
98
99
void draw_pause(struct winsize window_size, 
100
  uint8_t field_width,uint8_t field_height, uint8_t game_over);
101
void draw_highscore (int16_t new_score, 
102
  score leader_board[SCORE_NUMBER], struct winsize window_size);
103
104
bool print_buffer(uint16_t width, char *string);
105
106
char *give_state(board_state square);
107
108
direction read_direction(char *string, bool wasd_enabled);
109
110
uint8_t make_folder(char *path);
111
112
int32_t string_to_num(char* string, int32_t min, int32_t max, int32_t number);
113
114
#endif