Subversion Repositories Code-Repo

Rev

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

Rev 240 Rev 241
Line 5... Line 5...
5
static SNAKE_DATA *data_p;
5
static SNAKE_DATA *data_p;
6
 
6
 
7
void Snake_Init(SNAKE_DATA *data) {
7
void Snake_Init(SNAKE_DATA *data) {
8
    data_p = data;
8
    data_p = data;
9
 
9
 
10
    // Disable watchdog timer
-
 
11
    WDTCON = 0x00000000;
-
 
12
 
-
 
13
    // Set starting point
10
    // Set starting point
14
    data_p->body[0].x = 0;
-
 
15
    data_p->body[0].y = 0;
-
 
16
    data_p->body[0].z = 0;
11
    data_p->body[0] = (SNAKE_POINT){0,0,7};
17
 
12
 
18
    data_p->pos_head = 0;
13
    data_p->pos_head = 0;
19
    data_p->pos_tail = 0;
14
    data_p->pos_tail = 0;
20
    data_p->length = 1;
15
    data_p->length = 1;
21
    data_p->level = 1;
16
    data_p->level = 1;
Line 35... Line 30...
35
        }
30
        }
36
    }
31
    }
37
}
32
}
38
 
33
 
39
void Snake_Main(void) {
34
void Snake_Main(void) {
-
 
35
    // Main function, loops and delays while updating the frame every x milliseconds
40
    Delay_MS(2000);
36
    Delay_MS(2000);
41
    while (1) {
37
    while (1) {
42
        Snake_Update_Frame();
38
        Snake_Update_Frame();
43
        Delay_MS(data_p->delay);
39
        Delay_MS(data_p->delay);
44
    }
40
    }
Line 91... Line 87...
91
    uint32_t pos = data_p->pos_tail;
87
    uint32_t pos = data_p->pos_tail;
92
    while (pos != data_p->pos_head) {
88
    while (pos != data_p->pos_head) {
93
        if (data_p->direction.x == data_p->body[pos].x &&
89
        if (data_p->direction.x == data_p->body[pos].x &&
94
                data_p->direction.y == data_p->body[pos].y &&
90
                data_p->direction.y == data_p->body[pos].y &&
95
                data_p->direction.z == data_p->body[pos].z) {
91
                data_p->direction.z == data_p->body[pos].z) {
-
 
92
            // Indicate the overlapping pixel, delay, then return to idle state
96
            Cube_Overlay_Set_Pixel(data_p->direction.z, data_p->direction.x, data_p->direction.y, SNAKE_HEAD_COLOR);
93
            Cube_Overlay_Set_Pixel(data_p->direction.z, data_p->direction.x, data_p->direction.y, SNAKE_HEAD_COLOR);
97
            Delay_MS(3000);
94
            Delay_MS(3000);
98
            Reset_Board(BOARD_MODE_IDLE);
95
            Reset_Board(BOARD_MODE_IDLE);
99
        }
96
        }
100
        pos = (pos == CUBE_PIXELS - 1) ? 0 : pos + 1;
97
        pos = (pos == CUBE_PIXELS - 1) ? 0 : pos + 1;
Line 121... Line 118...
121
    // Determine the next direction to take
118
    // Determine the next direction to take
122
    Snake_Update_Direction(data_p->last_direction, 0x0);
119
    Snake_Update_Direction(data_p->last_direction, 0x0);
123
 
120
 
124
    // If we ate a candy, delay for a bit to rest
121
    // If we ate a candy, delay for a bit to rest
125
    if (om_nom_nom) {
122
    if (om_nom_nom) {
-
 
123
        // Increase the level by one, show on controller if necessary
126
        data_p->level += 1; // Increase the level by one
124
        data_p->level += 1;
127
        if (data_p->level % SNAKE_LEVEL_STEP == 0) {
125
        if (data_p->level % SNAKE_LEVEL_STEP == 0) {
128
            uint8_t tier = data_p->level / SNAKE_LEVEL_STEP;
126
            uint8_t tier = data_p->level / SNAKE_LEVEL_STEP;
129
            Controller_Set_Leds(tier, tier);
127
            Controller_Set_Leds(tier, tier);
130
        }
128
        }
131
        data_p->delay -= 5; // Decrease the delay between frame updates by 5ms
129
        // Decrease the delay between frame updates by 5ms
-
 
130
        data_p->delay -= 5;
-
 
131
        // Clear the watchdog timer to prevent resets in a middle of a game
-
 
132
        ClearWDT();
132
    }
133
    }
133
}
134
}
134
 
135
 
135
SNAKE_POINT Snake_Generate_Candy(void) {
136
SNAKE_POINT Snake_Generate_Candy(void) {
136
    // Generates a random position within the cube that doesnt overlap anything
137
    // Generates a random position within the cube that doesnt overlap anything