Snake vs Block Game to be run upon K64F.

Dependencies:   mbed

Committer:
AhmedPlaymaker
Date:
Sat May 04 20:48:10 2019 +0000
Revision:
70:7caab8069b9b
Parent:
69:55e309da7efd
Child:
74:7b6568bc16d5
Thanks to my newly created test files, I removed some of the most uselessly redundant functions in my snake class relating to get pos. The test classes helped me debug the problems with the bulky code by letting me know what coordinates were saved.

Who changed what in which revision?

UserRevisionLine numberNew 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 69:55e309da7efd 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 70:7caab8069b9b 21 Vector2D read_pos_1 = snake.get_pos(0);
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 70:7caab8069b9b 27 int b[10]; //each element in this array represents the beed number in the snake.
AhmedPlaymaker 70:7caab8069b9b 28 for(int i=0; i<=9; i++) {
AhmedPlaymaker 68:b9cfd27987ac 29 b[i] = 1;
AhmedPlaymaker 68:b9cfd27987ac 30 }
AhmedPlaymaker 68:b9cfd27987ac 31
AhmedPlaymaker 68:b9cfd27987ac 32 // Update the snake position
AhmedPlaymaker 68:b9cfd27987ac 33 snake.update(d, b);
AhmedPlaymaker 68:b9cfd27987ac 34
AhmedPlaymaker 68:b9cfd27987ac 35 // Read the position
AhmedPlaymaker 70:7caab8069b9b 36 Vector2D read_pos_2 = snake.get_pos(0);
AhmedPlaymaker 68:b9cfd27987ac 37 printf("%f, %f\n", read_pos_2.x, read_pos_2.y);
AhmedPlaymaker 68:b9cfd27987ac 38
AhmedPlaymaker 68:b9cfd27987ac 39 // Now check that both the positions are as expected
AhmedPlaymaker 68:b9cfd27987ac 40 bool success_flag = true;
AhmedPlaymaker 68:b9cfd27987ac 41
AhmedPlaymaker 68:b9cfd27987ac 42 // Fail the test if the initial position is wrong
AhmedPlaymaker 68:b9cfd27987ac 43 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 44 success_flag = false;
AhmedPlaymaker 68:b9cfd27987ac 45 }
AhmedPlaymaker 68:b9cfd27987ac 46
AhmedPlaymaker 68:b9cfd27987ac 47 // Fail the test if the final position is wrong
AhmedPlaymaker 68:b9cfd27987ac 48 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 49 success_flag = false;
AhmedPlaymaker 68:b9cfd27987ac 50 }
AhmedPlaymaker 68:b9cfd27987ac 51
AhmedPlaymaker 68:b9cfd27987ac 52 return success_flag;
AhmedPlaymaker 68:b9cfd27987ac 53 }
AhmedPlaymaker 68:b9cfd27987ac 54 #endif