-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslithering.h
135 lines (104 loc) · 3.37 KB
/
slithering.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/**
* Program: UCFK4 Snake
* Module: Header file for slithering.c
* Authors: Francis Phan, Richard Li
**/
#ifndef SLITHERING_H
#define SLITHERING_H
/** HEADER FILES **/
#include <stdint.h>
#include <stdlib.h>
#include "system.h"
#include "tinygl.h"
#include "pacer.h"
#include "navswitch.h"
#include "led.h"
#include "display.h"
#include "../fonts/font3x5_1.h"
#include "font.h"
#include "pio.h"
/** CONSTANTS USED IN SLITHERING.C ------------------------------------------------------------------ **/
/* Basic constants */
#define START_SNAKE_MODE 0
#define APPLE_MODE 1
#define PACER_RATE 500
#define LOOP_RATE 300
#define MESSAGE_RATE 20
#define BUTTON_RATE 300
#define LED_ON 1
#define LED_OFF 0
/* strings to display on the LED matrix */
#define SNAKE_TEXT 0
#define GAME_OVER_TEXT 1
#define WELL_PLAYED_TEXT 2
/* Navswitch scanner as strings are displayed */
#define PUSH_NAVSWITCH_TO_EXIT 1
#define NO_EXIT 0
/* Navswtich directions */
#define NAVSWITCH_PUSHED_DOWN 0
#define NAVSWITCH_PUSHED_NORTH 1
#define NAVSWITCH_PUSHED_SOUTH 2
#define NAVSWITCH_PUSHED_WEST 3
#define NAVSWITCH_PUSHED_EAST 4
#define GAME_FIRST_START 1
#define GAME_ALREADY_STARTED 0
#define SNAKE_MAX_LENGTH = 11;
/* Restart game code */
#define RESTART 5
/* Connect piezo tweeter to pins 6 and 8 of UCFK4 P1 connector
for push-pull operation. */
#define PIEZO_PIO PIO_DEFINE (PORT_D, 6)
#define PIEZO1_PIO PIO_DEFINE (PORT_D, 4)
#define PIEZO2_PIO PIO_DEFINE (PORT_D, 6)
/** STRUCT -------------------------------------------------------------------------------------- **/
/* Direction struct */
enum direction {NORTH, EAST, SOUTH, WEST};
typedef enum direction direction_t;
/* Snake struct */
struct snake
{
tinygl_point_t head; /* Current head of snake. */
direction_t head_direction; /* Current direction. */
tinygl_point_t body[12]; // Current body of snake
direction_t body_direction[12];
uint16_t body_length; // Current length of the snake
};
typedef struct snake snake_t;
/* Apple struct */
struct apple
{
tinygl_point_t location; // X and Y location of the apple
};
typedef struct apple apple_t;
/** SIGNATURES -------------------------------------------------------------------------------------- **/
/** Automatically slither the snake
* @param the snake itself
* @return the snake itself **/
snake_t snake_slither_forward(snake_t snake);
/** This function will check if the snake has crossed the matrix's boundary.
* @param the snake itself.
* @return the RESTART code if game is over **/
int check_boundary_cross(snake_t snake);
/** Check if the snake has collided with itself
* @param the snake itself.
* @return the RESTART code if game is over **/
int check_collision(snake_t snake);
/** Grow the snake
* @param the snake itself.
* @return the snake that has been grown **/
snake_t snake_grow(snake_t my_snake);
/** Grow the snake
* @param the snake itself.
* @return the snake that has been grown **/
snake_t snake_init(void);
/** A function to randomise a number between two bounds inclusive
* @param upper bound, lower bound
* @return a random integer in that bound inclusive **/
int randomiser(int upper, int lower);
/** Make a new apple at a location that isn't where the snake is
* @param the snake including its location
* @return an apple **/
apple_t make_apple(snake_t snake);
/** Main function to coordinate the game **/
int control(int level);
#endif