
Final Commit
Dependencies: mbed
Diff: SnakeEngine/SnakeEngine.cpp
- Revision:
- 24:4b180348826e
- Parent:
- 23:3081be418c89
- Child:
- 25:f03439ee32c6
--- a/SnakeEngine/SnakeEngine.cpp Tue May 01 10:55:49 2018 +0000 +++ b/SnakeEngine/SnakeEngine.cpp Tue May 01 12:20:42 2018 +0000 @@ -12,133 +12,6 @@ } ///////////////// global methods ///////////////// - -///////////////// public methods ///////////////// - - -void SnakeEngine::set_even_array(Gamepad &pad) -{ - - int i; - int length = g_tl; - - extern int g_odd_array_x[100]; - extern int g_odd_array_y[100]; - - Vector2D pos = _snake.get_snake_position(); - - g_odd_array_x[0] = pos.x; - g_odd_array_y[0] = pos.y; - - for(i = length; i >= 0; --i) { - - if(i > 0) { - - g_even_array_x[i] = g_odd_array_x[i-1]; - g_even_array_y[i] = g_odd_array_y[i-1]; - - } - - else if(i == 0) { - - g_even_array_x[0] = pos.x; - g_even_array_y[0] = pos.y; - - } - - - } - -} - -void SnakeEngine::set_odd_array(Gamepad &pad) -{ - - int i; - int length = g_tl; - - extern int g_even_array_x[100]; - extern int g_even_array_y[100]; - - Vector2D pos = _snake.get_snake_position(); - - g_even_array_x[0] = pos.x; - g_even_array_y[0] = pos.y; - - for(i = length; i >= 0; --i) { - - if(i > 0) { - - g_odd_array_x[i] = g_even_array_x[i-1]; - g_odd_array_y[i] = g_even_array_y[i-1]; - - } - - else if(i == 0) { - - g_odd_array_x[0] = pos.x; - g_odd_array_y[0] = pos.y; - - } - - } - -} - - -void SnakeEngine::set_tail_array(Gamepad &pad) -{ - - int c = g_engine_fc % 2; - - if(c == 1) { - - set_odd_array(pad); - - } - - else if (c == 0) { - - set_even_array(pad); - - } - -} - -void SnakeEngine::draw_tail(N5110 &lcd) -{ - int length = g_tl; - int c; - c = g_engine_fc % 2; - int i; - int x; - int y; - - //printf("Odd/Even %i \n", c); - - for(i=0; i<=length; ++i) { - - if(c == 1) { - - x = g_odd_array_x[i]; - y = g_odd_array_y[i]; - - lcd.setPixel(x,y,true); - - } - - else if (c == 0) { - - x = g_even_array_x[i]; - y = g_even_array_y[i]; - - lcd.setPixel(x,y,true); - - } - - } -} - int g_tail_length() { @@ -149,6 +22,8 @@ } +///////////////// public methods ///////////////// + void SnakeEngine::init(Direction in, Direction cur, int snake_pos_x, int snake_pos_y) { _snake_pos_x = snake_pos_x; @@ -162,16 +37,8 @@ } -void SnakeEngine::get_input(Gamepad &pad) -{ - - _in = pad.get_direction(); - -} - void SnakeEngine::update(Gamepad &pad) { - //int length; bool food_col; ++g_engine_fc; @@ -180,8 +47,6 @@ food_col = detect_food_collision(pad); _collision = food_col; set_tail_length(food_col); - //length = get_tail_length(); - //printf("Tail Length = %i \n", length); set_tail_array(pad); } @@ -190,18 +55,67 @@ void SnakeEngine::draw(N5110 &lcd) { + // puts fram around the game area + lcd.drawRect(0,0,WIDTH,HEIGHT,FILL_TRANSPARENT); lcd.setContrast(0.5); + _food.update(_collision); _food.draw(lcd); - //_snake.draw(lcd); - //draw_tail(lcd); - //_snake.update(_in, _cur); _snake.draw(lcd); draw_tail(lcd); } + +void SnakeEngine::draw_tail(N5110 &lcd) +{ + int length = g_tl; + int c; + c = g_engine_fc % 2; + int i; + int x; + int y; + + //printf("Odd/Even %i \n", c); + + // runs through each segment in the tail and draws to the lcd + + for(i=0; i<=length; ++i) { + + // when odd draw odd array + + if(c == 1) { + + x = g_odd_array_x[i]; + y = g_odd_array_y[i]; + + lcd.setPixel(x,y,true); + + } + + // when even draw the even array + + else if (c == 0) { + + x = g_even_array_x[i]; + y = g_even_array_y[i]; + + lcd.setPixel(x,y,true); + + } + + } +} + + +void SnakeEngine::get_input(Gamepad &pad) +{ + + _in = pad.get_direction(); + +} + bool SnakeEngine::detect_food_collision(Gamepad &pad) { Vector2D snake_pos = _snake.get_snake_position(); @@ -209,6 +123,8 @@ bool success_flag = false; + // when the position of the head and food are the same return true + if((snake_pos.x == food_pos.x) && (snake_pos.y == food_pos.y)) { success_flag = true; @@ -232,6 +148,8 @@ bool success_flag = false; + // when an snake position is at an edge return true + if((snake_pos.x == (0 || 84)) || (snake_pos.y == (0 || 48))) { success_flag = true; @@ -245,6 +163,7 @@ void SnakeEngine::set_tail_length(bool collision_detected) { + // when a collision is detected increment the tail length if(collision_detected) { @@ -268,6 +187,115 @@ return length; } + +void SnakeEngine::set_odd_array(Gamepad &pad) +{ + + int i; + int length = g_tl; + + extern int g_even_array_x[100]; + extern int g_even_array_y[100]; + + Vector2D pos = _snake.get_snake_position(); + + // sets the first value in the array to the new position + + g_even_array_x[0] = pos.x; + g_even_array_y[0] = pos.y; + + // starting from the last element in the arrays work back to zero + + for(i = length; i >= 0; --i) { + + // switch positions of the indexes adding the new head position + + if(i > 0) { + + g_odd_array_x[i] = g_even_array_x[i-1]; + g_odd_array_y[i] = g_even_array_y[i-1]; + + } + + // when snake has no length position 0 is the new position + + else if(i == 0) { + + g_odd_array_x[0] = pos.x; + g_odd_array_y[0] = pos.y; + + } + + } + +} + +void SnakeEngine::set_even_array(Gamepad &pad) +{ + + int i; + int length = g_tl; + + extern int g_odd_array_x[100]; + extern int g_odd_array_y[100]; + + Vector2D pos = _snake.get_snake_position(); + + // sets the first value in the array to the new position + + g_odd_array_x[0] = pos.x; + g_odd_array_y[0] = pos.y; + + // starting from the last element in the arrays work back to zero + + for(i = length; i >= 0; --i) { + + // switch positions of the indexes adding the new head position + + if(i > 0) { + + g_even_array_x[i] = g_odd_array_x[i-1]; + g_even_array_y[i] = g_odd_array_y[i-1]; + + } + + // when snake has no length position 0 is the new position + + else if(i == 0) { + + g_even_array_x[0] = pos.x; + g_even_array_y[0] = pos.y; + + } + + } + +} + + +void SnakeEngine::set_tail_array(Gamepad &pad) +{ + + int c = g_engine_fc % 2; + + // when the frame counter is odd set odd array + + if(c == 1) { + + set_odd_array(pad); + + } + + // when the frame counter is even set even array + + else if (c == 0) { + + set_even_array(pad); + + } + +} +