Ahmed Adamjee
/
SnakeVSBlock
Snake vs Block Game to be run upon K64F.
GameObjects/Snake/Snake-test.h@68:b9cfd27987ac, 2019-05-04 (annotated)
- Committer:
- AhmedPlaymaker
- Date:
- Sat May 04 16:56:04 2019 +0000
- Revision:
- 68:b9cfd27987ac
- Child:
- 69:55e309da7efd
Added test headers and functions that makes sure that my snake moves and updates as expected.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
AhmedPlaymaker | 68:b9cfd27987ac | 1 | #ifndef SNAKE_TEST_H |
AhmedPlaymaker | 68:b9cfd27987ac | 2 | #define SNAKE_TEST_H |
AhmedPlaymaker | 68:b9cfd27987ac | 3 | |
AhmedPlaymaker | 68:b9cfd27987ac | 4 | /** |
AhmedPlaymaker | 68:b9cfd27987ac | 5 | * \brief Check that Snake object goes to correct position when moved |
AhmedPlaymaker | 68:b9cfd27987ac | 6 | * |
AhmedPlaymaker | 68:b9cfd27987ac | 7 | * \returns true if all the tests passed |
AhmedPlaymaker | 68:b9cfd27987ac | 8 | */ |
AhmedPlaymaker | 68:b9cfd27987ac | 9 | bool Snake_test_movement() |
AhmedPlaymaker | 68:b9cfd27987ac | 10 | { |
AhmedPlaymaker | 68:b9cfd27987ac | 11 | // Create Snake object with a length of 15, and x axis speed of 2. |
AhmedPlaymaker | 68:b9cfd27987ac | 12 | Snake snake; |
AhmedPlaymaker | 68:b9cfd27987ac | 13 | snake.init(15, 2); |
AhmedPlaymaker | 68:b9cfd27987ac | 14 | |
AhmedPlaymaker | 68:b9cfd27987ac | 15 | // Set the position to WIDTH/2, HEIGHT - 3 |
AhmedPlaymaker | 68:b9cfd27987ac | 16 | |
AhmedPlaymaker | 68:b9cfd27987ac | 17 | Vector2D initial_pos = {WIDTH/2, HEIGHT - 3}; //Spawns player sprite near the middle of the screen. |
AhmedPlaymaker | 68:b9cfd27987ac | 18 | snake.set_pos(initial_pos); |
AhmedPlaymaker | 68:b9cfd27987ac | 19 | |
AhmedPlaymaker | 68:b9cfd27987ac | 20 | // Read the position |
AhmedPlaymaker | 68:b9cfd27987ac | 21 | Vector2D read_pos_1 = snake.get_pos(); |
AhmedPlaymaker | 68:b9cfd27987ac | 22 | printf("%f, %f\n", read_pos_1.x, read_pos_1.y); |
AhmedPlaymaker | 68:b9cfd27987ac | 23 | |
AhmedPlaymaker | 68:b9cfd27987ac | 24 | // Set the direction to W and set snake motion free by setting b as 1; |
AhmedPlaymaker | 68:b9cfd27987ac | 25 | Direction d = W; |
AhmedPlaymaker | 68:b9cfd27987ac | 26 | |
AhmedPlaymaker | 68:b9cfd27987ac | 27 | int b[15]; //each element in this array represents the beed number in the snake. |
AhmedPlaymaker | 68:b9cfd27987ac | 28 | for(int i=0; i<=13; i++) { |
AhmedPlaymaker | 68:b9cfd27987ac | 29 | b[i] = 1; |
AhmedPlaymaker | 68:b9cfd27987ac | 30 | } |
AhmedPlaymaker | 68:b9cfd27987ac | 31 | b[14] = 1; |
AhmedPlaymaker | 68:b9cfd27987ac | 32 | |
AhmedPlaymaker | 68:b9cfd27987ac | 33 | // Update the snake position |
AhmedPlaymaker | 68:b9cfd27987ac | 34 | snake.update(d, b); |
AhmedPlaymaker | 68:b9cfd27987ac | 35 | |
AhmedPlaymaker | 68:b9cfd27987ac | 36 | // Read the position |
AhmedPlaymaker | 68:b9cfd27987ac | 37 | Vector2D read_pos_2 = snake.get_pos(); |
AhmedPlaymaker | 68:b9cfd27987ac | 38 | printf("%f, %f\n", read_pos_2.x, read_pos_2.y); |
AhmedPlaymaker | 68:b9cfd27987ac | 39 | |
AhmedPlaymaker | 68:b9cfd27987ac | 40 | // Now check that both the positions are as expected |
AhmedPlaymaker | 68:b9cfd27987ac | 41 | bool success_flag = true; |
AhmedPlaymaker | 68:b9cfd27987ac | 42 | |
AhmedPlaymaker | 68:b9cfd27987ac | 43 | // Fail the test if the initial position is wrong |
AhmedPlaymaker | 68:b9cfd27987ac | 44 | if (read_pos_1.x != WIDTH/2 || read_pos_1.y != 18) { //18 because I'm testing the position of the last snake beed. |
AhmedPlaymaker | 68:b9cfd27987ac | 45 | success_flag = false; |
AhmedPlaymaker | 68:b9cfd27987ac | 46 | } |
AhmedPlaymaker | 68:b9cfd27987ac | 47 | |
AhmedPlaymaker | 68:b9cfd27987ac | 48 | // Fail the test if the final position is wrong |
AhmedPlaymaker | 68:b9cfd27987ac | 49 | if (read_pos_2.x != ((WIDTH/2)-2) || read_pos_2.y != 18) { //18 because I'm testing the position of the last snake beed and ((WIDTH/2)-2) because speed is set to 2. |
AhmedPlaymaker | 68:b9cfd27987ac | 50 | success_flag = false; |
AhmedPlaymaker | 68:b9cfd27987ac | 51 | } |
AhmedPlaymaker | 68:b9cfd27987ac | 52 | |
AhmedPlaymaker | 68:b9cfd27987ac | 53 | return success_flag; |
AhmedPlaymaker | 68:b9cfd27987ac | 54 | } |
AhmedPlaymaker | 68:b9cfd27987ac | 55 | #endif |