James Heavey
/
EL17JH
FINAL VERSION
Ball/Ball-test.h@0:7d4d2023ed9c, 2019-04-16 (annotated)
- Committer:
- jamesheavey
- Date:
- Tue Apr 16 18:58:18 2019 +0000
- Revision:
- 0:7d4d2023ed9c
Game changed to breakout form space invaders (new folder) template initialised, edited paddle, edited direction of movement, edited collision boundaries
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jamesheavey | 0:7d4d2023ed9c | 1 | #ifndef BALL_TEST_H |
jamesheavey | 0:7d4d2023ed9c | 2 | #define BALL_TEST_H |
jamesheavey | 0:7d4d2023ed9c | 3 | |
jamesheavey | 0:7d4d2023ed9c | 4 | /** |
jamesheavey | 0:7d4d2023ed9c | 5 | * \brief Check that Ball object goes to correct position when moved |
jamesheavey | 0:7d4d2023ed9c | 6 | * |
jamesheavey | 0:7d4d2023ed9c | 7 | * \returns true if all the tests passed |
jamesheavey | 0:7d4d2023ed9c | 8 | */ |
jamesheavey | 0:7d4d2023ed9c | 9 | bool Ball_test_movement() |
jamesheavey | 0:7d4d2023ed9c | 10 | { |
jamesheavey | 0:7d4d2023ed9c | 11 | // Initialise Ball object with a size of 2, and speed of 1 |
jamesheavey | 0:7d4d2023ed9c | 12 | Ball ball; |
jamesheavey | 0:7d4d2023ed9c | 13 | ball.init(2, 1); |
jamesheavey | 0:7d4d2023ed9c | 14 | |
jamesheavey | 0:7d4d2023ed9c | 15 | // Set the position to 5, 5 |
jamesheavey | 0:7d4d2023ed9c | 16 | Vector2D initial_pos = {5, 5}; |
jamesheavey | 0:7d4d2023ed9c | 17 | ball.set_pos(initial_pos); |
jamesheavey | 0:7d4d2023ed9c | 18 | |
jamesheavey | 0:7d4d2023ed9c | 19 | // Read the position |
jamesheavey | 0:7d4d2023ed9c | 20 | Vector2D read_pos_1 = ball.get_pos(); |
jamesheavey | 0:7d4d2023ed9c | 21 | printf("%f, %f\n", read_pos_1.x, read_pos_1.y); |
jamesheavey | 0:7d4d2023ed9c | 22 | |
jamesheavey | 0:7d4d2023ed9c | 23 | // Set the velocity to -2, 3 |
jamesheavey | 0:7d4d2023ed9c | 24 | Vector2D velocity = {-2, 3}; |
jamesheavey | 0:7d4d2023ed9c | 25 | ball.set_velocity(velocity); |
jamesheavey | 0:7d4d2023ed9c | 26 | |
jamesheavey | 0:7d4d2023ed9c | 27 | // Update the position |
jamesheavey | 0:7d4d2023ed9c | 28 | ball.update(); |
jamesheavey | 0:7d4d2023ed9c | 29 | |
jamesheavey | 0:7d4d2023ed9c | 30 | // Read the position |
jamesheavey | 0:7d4d2023ed9c | 31 | Vector2D read_pos_2 = ball.get_pos(); |
jamesheavey | 0:7d4d2023ed9c | 32 | printf("%f, %f\n", read_pos_2.x, read_pos_2.y); |
jamesheavey | 0:7d4d2023ed9c | 33 | |
jamesheavey | 0:7d4d2023ed9c | 34 | // Now check that both the positions are as expected |
jamesheavey | 0:7d4d2023ed9c | 35 | bool success_flag = true; |
jamesheavey | 0:7d4d2023ed9c | 36 | |
jamesheavey | 0:7d4d2023ed9c | 37 | // Fail the test if the initial position is wrong |
jamesheavey | 0:7d4d2023ed9c | 38 | if (read_pos_1.x != 5 || read_pos_1.y != 5) { |
jamesheavey | 0:7d4d2023ed9c | 39 | success_flag = false; |
jamesheavey | 0:7d4d2023ed9c | 40 | } |
jamesheavey | 0:7d4d2023ed9c | 41 | |
jamesheavey | 0:7d4d2023ed9c | 42 | // Fail the test if the final position is wrong |
jamesheavey | 0:7d4d2023ed9c | 43 | if (read_pos_2.x != 3 || read_pos_2.y != 8) { |
jamesheavey | 0:7d4d2023ed9c | 44 | success_flag = false; |
jamesheavey | 0:7d4d2023ed9c | 45 | } |
jamesheavey | 0:7d4d2023ed9c | 46 | |
jamesheavey | 0:7d4d2023ed9c | 47 | return success_flag; |
jamesheavey | 0:7d4d2023ed9c | 48 | } |
jamesheavey | 0:7d4d2023ed9c | 49 | #endif |