jag

snake_c

public

Small snake game in C

Star0
Watch0
893f512567ee1b182a798d5ded71155349362ae6
2
ssh://git@git.jaglekon.de:30/jag/snake_c.git
source/snake.hC123 lines2.6 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
#include <limits.h>
26
27
#include <jag_parse.h>
28
#include <jag_write.h>
29
30
31
#define RIGHT_CODE 'C'
32
#define LEFT_CODE 'D'
33
#define UP_CODE 'A'
34
#define DOWN_CODE 'B'
35
36
#define WIDTH 20
37
#define HEIGHT 20
38
39
#define MIN_SIZE 10
40
#define MAX_SIZE 100
41
42
#define STRING_LEN 20
43
44
#define SCORE_LEN 10
45
#define SCORE_NUMBER 10
46
47
#define SPEED 50 /* in ms */
48
49
#define MAX_LENGTH 1000
50
#define STARTING_LENGTH 2
51
52
#define SETTINGS_FILE "settings"
53
#define SCORE_FILE "score"
54
#define SETTINGS_FOLDER "snake_c"
55
56
typedef enum { NEW_GAME, SETTINGS, HIGHSCORE, EXIT, MAIN_MENU } main_menu;
57
58
typedef enum {HEIGHT_S, WIDTH_S, SPEED_S, BACK_S, SETTINGS_MENU} settings;
59
60
typedef enum {V_FAST, FAST, AVERAGE, SLOW, V_SLOW, SPEED_L} speed;
61
62
typedef enum {
63
  UP, LEFT, DOWN, RIGHT, DIRECTIONS,
64
  ENTER, P_BUTTON, Q_BUTTON, ESCAPE, DEL
65
} direction;
66
67
typedef enum {
68
  AIR, FRUIT, HEAD_1, HEAD_2, HORIZONTAL, VERTICAL, 
69
  TOP_RIGHT, TOP_LEFT, BOTTOM_RIGHT, BOTTOM_LEFT
70
} board_state;
71
72
typedef struct selected {
73
  int8_t x;
74
  int8_t y;
75
} selected;
76
77
typedef struct snake_part {
78
  selected coords;
79
  direction directions[2];
80
} snake_part;
81
82
typedef struct snake {
83
  snake_part * position;
84
  uint16_t length;
85
} snake;
86
87
typedef struct score {
88
  char name[SCORE_LEN];
89
  uint16_t points;
90
} score;
91
92
uint8_t update_snake (selected game_size, snake *my_snake, direction shift);
93
void update_field(snake my_snake, selected game_size, 
94
  board_state **board, bool init);
95
selected new_fruit(selected game_size, board_state **board);
96
97
bool start_animation();
98
99
bool draw_menu(main_menu menu, struct winsize window_size);
100
void draw_settings(settings selected_setting, speed game_speed, selected game_size, struct winsize window_size);
101
102
bool draw_board(selected game_size, board_state **board,
103
  struct winsize window_size, bool redraw, uint8_t pause, uint16_t score);
104
105
void draw_pause(struct winsize window_size, 
106
  uint8_t field_width,uint8_t field_height, uint8_t game_over);
107
void draw_highscore (int16_t new_score, 
108
  score leader_board[SCORE_NUMBER], struct winsize window_size);
109
110
bool print_buffer(uint16_t width, char *string);
111
112
char *give_state(board_state square);
113
114
direction read_direction(char *string, bool wasd_enabled);
115
116
uint8_t make_folder(char *path);
117
118
int32_t string_to_num(char* string, int32_t min, int32_t max, int32_t number);
119
120
121
uint8_t parse_parameters(int argc, char *args[]);
122
123
#endif