Subversion Repositories Code-Repo

Rev

Rev 241 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 241 Rev 242
Line 1... Line 1...
1
#include "defines.h"
1
#include "defines.h"
2
#include "CONTROLLERS.h"
2
#include "CONTROLLERS.h"
3
#include "SNAKE.h"
3
#include "SNAKE.h"
4
 
4
 
5
static SNAKE_DATA *data_p;
5
static SNAKE_DATA *data_p;
-
 
6
static uint32_t rand_value __attribute__((persistent));
6
 
7
 
7
void Snake_Init(SNAKE_DATA *data) {
8
void Snake_Init(SNAKE_DATA *data) {
8
    data_p = data;
9
    data_p = data;
9
 
10
 
10
    // Set starting point
11
    // Set starting point
Line 14... Line 15...
14
    data_p->pos_tail = 0;
15
    data_p->pos_tail = 0;
15
    data_p->length = 1;
16
    data_p->length = 1;
16
    data_p->level = 1;
17
    data_p->level = 1;
17
    data_p->delay = 800;
18
    data_p->delay = 800;
18
 
19
 
-
 
20
    srand(rand_value);
-
 
21
 
19
    // Generate a starting location for the candy
22
    // Generate a starting location for the candy
20
    data_p->candy_loc = Snake_Generate_Candy();
23
    data_p->candy_loc = Snake_Generate_Candy();
21
 
24
 
22
    // Draw the snake (head)
25
    // Draw the snake (head)
23
    Cube_Clear();
26
    Cube_Clear();
Line 33... Line 36...
33
 
36
 
34
void Snake_Main(void) {
37
void Snake_Main(void) {
35
    // Main function, loops and delays while updating the frame every x milliseconds
38
    // Main function, loops and delays while updating the frame every x milliseconds
36
    Delay_MS(2000);
39
    Delay_MS(2000);
37
    while (1) {
40
    while (1) {
-
 
41
        // Regenerate the seed upon each update so that the candy starts somewhere new every time
-
 
42
        rand_value = rand();
-
 
43
        
38
        Snake_Update_Frame();
44
        Snake_Update_Frame();
39
        Delay_MS(data_p->delay);
45
        Delay_MS(data_p->delay);
40
    }
46
    }
41
}
47
}
42
 
48
 
Line 88... Line 94...
88
    while (pos != data_p->pos_head) {
94
    while (pos != data_p->pos_head) {
89
        if (data_p->direction.x == data_p->body[pos].x &&
95
        if (data_p->direction.x == data_p->body[pos].x &&
90
                data_p->direction.y == data_p->body[pos].y &&
96
                data_p->direction.y == data_p->body[pos].y &&
91
                data_p->direction.z == data_p->body[pos].z) {
97
                data_p->direction.z == data_p->body[pos].z) {
92
            // Indicate the overlapping pixel, delay, then return to idle state
98
            // Indicate the overlapping pixel, delay, then return to idle state
93
            Cube_Overlay_Set_Pixel(data_p->direction.z, data_p->direction.x, data_p->direction.y, SNAKE_HEAD_COLOR);
99
            Cube_Set_Pixel(data_p->direction.z, data_p->direction.x, data_p->direction.y, SNAKE_COLLISION_COLOR);
94
            Delay_MS(3000);
100
            Delay_MS(3000);
-
 
101
            Cube_Overlay_Clear();
-
 
102
            Animation_Cube_In_Out(200, ORANGE);
95
            Reset_Board(BOARD_MODE_IDLE);
103
            Reset_Board(BOARD_MODE_IDLE);
96
        }
104
        }
97
        pos = (pos == CUBE_PIXELS - 1) ? 0 : pos + 1;
105
        pos = (pos == CUBE_PIXELS - 1) ? 0 : pos + 1;
98
    }
106
    }
99
 
107