Subversion Repositories Code-Repo

Rev

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

Rev 242 Rev 276
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
#include "TIMER4.h"
4
 
5
 
5
static SNAKE_DATA *data_p;
6
static SNAKE_DATA *data_p;
6
static uint32_t rand_value __attribute__((persistent));
7
static uint32_t rand_value __attribute__((persistent));
7
 
8
 
8
void Snake_Init(SNAKE_DATA *data) {
9
void Snake_Init(SNAKE_DATA *data) {
Line 12... Line 13...
12
    data_p->body[0] = (SNAKE_POINT){0,0,7};
13
    data_p->body[0] = (SNAKE_POINT){0,0,7};
13
 
14
 
14
    data_p->pos_head = 0;
15
    data_p->pos_head = 0;
15
    data_p->pos_tail = 0;
16
    data_p->pos_tail = 0;
16
    data_p->length = 1;
17
    data_p->length = 1;
17
    data_p->level = 1;
18
    data_p->level = 0;
-
 
19
    data_p->delay = SNAKE_MAXIMUM_DELAY;
-
 
20
    data_p->direction = (SNAKE_POINT){1,0,7};
18
    data_p->delay = 800;
21
    data_p->last_direction = 0x08;
19
 
22
 
20
    srand(rand_value);
23
    srand(rand_value);
21
 
24
 
22
    // Generate a starting location for the candy
25
    // Generate a starting location for the candy
23
    data_p->candy_loc = Snake_Generate_Candy();
26
    data_p->candy_loc = Snake_Generate_Candy();
Line 34... Line 37...
34
    }
37
    }
35
}
38
}
36
 
39
 
37
void Snake_Main(void) {
40
void Snake_Main(void) {
38
    // Main function, loops and delays while updating the frame every x milliseconds
41
    // Main function, loops and delays while updating the frame every x milliseconds
-
 
42
 
-
 
43
    while(!Controller_Get_Connected()) {
-
 
44
        Delay_MS(100);
-
 
45
        Controller_Poll_Connected();
-
 
46
    }
-
 
47
 
-
 
48
    Controller_Set_Active(0);
-
 
49
    Delay_MS(20);
-
 
50
    Controller_Set_Left_Leds(0, 0x1);
-
 
51
    TIMER4_Start();
39
    Delay_MS(2000);
52
    Delay_MS(1000);
40
    while (1) {
53
    while (1) {
41
        // Regenerate the seed upon each update so that the candy starts somewhere new every time
54
        // Regenerate the seed upon each update so that the candy starts somewhere new every time
42
        rand_value = rand();
55
        rand_value = rand();
43
        
56
 
44
        Snake_Update_Frame();
57
        Snake_Update_Frame();
45
        Delay_MS(data_p->delay);
58
        Delay_MS(data_p->delay);
46
    }
59
    }
47
}
60
}
48
 
61
 
49
void Snake_Update_Direction(uint8_t p1, uint8_t p2) {
62
void Snake_Update_Direction(uint8_t controller, CTRL_BTN_STATUS value) {
50
    // Determine the next direction for the snake based off the last button press
63
    // Determine the next direction for the snake based off the last button press
51
    SNAKE_DIRECTION dir;
-
 
52
    dir.value = p1 | p2;
64
    if (controller == 0) {
53
    data_p->last_direction = dir.value;
65
        data_p->last_direction = value.w;
54
 
-
 
55
    SNAKE_POINT point = data_p->body[data_p->pos_head];
66
        SNAKE_POINT point = data_p->body[data_p->pos_head];
56
    
67
 
57
    if (dir.up) {
68
        if (value.BTN_L_N || value.BTN_L_E) {    // Up
58
        point.z = (point.z == CUBE_LAYER_COUNT - 1) ? 0 : point.z + 1;
69
            point.z = (point.z == CUBE_LAYER_COUNT - 1) ? 0 : point.z + 1;
59
    } else if (dir.down) {
70
        } else if (value.BTN_L_W || value.BTN_L_S) { // Down
60
        point.z = (point.z == 0) ? CUBE_LAYER_COUNT - 1 : point.z - 1;
71
            point.z = (point.z == 0) ? CUBE_LAYER_COUNT - 1 : point.z - 1;
61
    } else if (dir.forward) {
72
        } else if (value.BTN_R_N) { // Forward
62
        point.x = (point.x == CUBE_ROW_COUNT - 1) ? 0 : point.x + 1;
73
            point.x = (point.x == CUBE_ROW_COUNT - 1) ? 0 : point.x + 1;
63
    } else if (dir.right) {
74
        } else if (value.BTN_R_W) { // Right
64
        point.y = (point.y == CUBE_COLUMN_COUNT - 1) ? 0 : point.y + 1;
75
            point.y = (point.y == CUBE_COLUMN_COUNT - 1) ? 0 : point.y + 1;
65
    } else if (dir.backward) {
76
        } else if (value.BTN_R_S) { // Backward
66
        point.x = (point.x == 0) ? CUBE_ROW_COUNT - 1 : point.x - 1;
77
            point.x = (point.x == 0) ? CUBE_ROW_COUNT - 1 : point.x - 1;
67
    } else if (dir.left) {
78
        } else if (value.BTN_R_E) { // Left
68
        point.y = (point.y== 0) ? CUBE_COLUMN_COUNT - 1 : point.y - 1;
79
            point.y = (point.y== 0) ? CUBE_COLUMN_COUNT - 1 : point.y - 1;
69
    }
80
        }
70
 
81
 
71
    data_p->direction = point;
82
        data_p->direction = point;
-
 
83
    }
72
 
84
 
73
    // Update the overlay with the candy location
85
    // Update the overlay with the candy location
74
    Cube_Overlay_Clear();
86
    Cube_Overlay_Clear();
75
    Cube_Overlay_Set_Pixel(data_p->candy_loc.z, data_p->candy_loc.x, data_p->candy_loc.y, SNAKE_CANDY_COLOR);
87
    Cube_Overlay_Set_Pixel(data_p->candy_loc.z, data_p->candy_loc.x, data_p->candy_loc.y, SNAKE_CANDY_COLOR);
76
}
88
}
Line 122... Line 134...
122
            Cube_Set_Pixel(data_p->body[index].z, data_p->body[index].x, data_p->body[index].y, SNAKE_BODY_COLOR);
134
            Cube_Set_Pixel(data_p->body[index].z, data_p->body[index].x, data_p->body[index].y, SNAKE_BODY_COLOR);
123
        }
135
        }
124
    }
136
    }
125
 
137
 
126
    // Determine the next direction to take
138
    // Determine the next direction to take
127
    Snake_Update_Direction(data_p->last_direction, 0x0);
139
    Snake_Update_Direction(0, (CTRL_BTN_STATUS)data_p->last_direction);
128
 
140
 
129
    // If we ate a candy, delay for a bit to rest
141
    // If we ate a candy, delay for a bit to rest
130
    if (om_nom_nom) {
142
    if (om_nom_nom) {
131
        // Increase the level by one, show on controller if necessary
143
        // Increase the level by one
132
        data_p->level += 1;
144
        data_p->level += 1;
-
 
145
 
-
 
146
        TIMER4_Stop();
133
        if (data_p->level % SNAKE_LEVEL_STEP == 0) {
147
        Controller_Set_Middle_Leds(0, data_p->level);
134
            uint8_t tier = data_p->level / SNAKE_LEVEL_STEP;
148
        if (data_p->level >= 256)
135
            Controller_Set_Leds(tier, tier);
149
            Controller_Set_Left_Leds(0, 0x9);
136
        }
150
        TIMER4_Start();
-
 
151
 
137
        // Decrease the delay between frame updates by 5ms
152
        // Decrease the delay between frame updates by 5ms
-
 
153
        if (data_p->delay > SNAKE_MINIMUM_DELAY)
138
        data_p->delay -= 5;
154
            data_p->delay -= 5;
139
        // Clear the watchdog timer to prevent resets in a middle of a game
155
        // Clear the watchdog timer to prevent resets in a middle of a game
140
        ClearWDT();
156
        ClearWDT();
141
    }
157
    }
142
}
158
}
143
 
159