
Lewis Cheadle 201245660
Dependencies: mbed
Ball/Ball.cpp@13:fd290d2fd917, 2020-05-26 (annotated)
- Committer:
- ll17lrc
- Date:
- Tue May 26 22:41:23 2020 +0000
- Revision:
- 13:fd290d2fd917
- Parent:
- 8:10eb578dd754
Final Submission. I have read and agreed with Statement of Academic Integrity.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ll17lrc | 2:823dea76ff2e | 1 | #include "Ball.h" |
ll17lrc | 2:823dea76ff2e | 2 | |
ll17lrc | 2:823dea76ff2e | 3 | Ball::Ball() |
ll17lrc | 2:823dea76ff2e | 4 | { |
ll17lrc | 6:312b7ae0c0f2 | 5 | |
ll17lrc | 2:823dea76ff2e | 6 | } |
ll17lrc | 2:823dea76ff2e | 7 | |
ll17lrc | 2:823dea76ff2e | 8 | Ball::~Ball() |
ll17lrc | 2:823dea76ff2e | 9 | { |
ll17lrc | 2:823dea76ff2e | 10 | |
ll17lrc | 2:823dea76ff2e | 11 | } |
ll17lrc | 2:823dea76ff2e | 12 | |
ll17lrc | 13:fd290d2fd917 | 13 | //Initialises the ball sprite and the initial co-ords of the ball |
ll17lrc | 7:35465b3bf586 | 14 | void Ball::init(int x,int y) |
ll17lrc | 4:a9d5fca3b7ba | 15 | { |
ll17lrc | 4:a9d5fca3b7ba | 16 | int ball[2][2] = { |
ll17lrc | 2:823dea76ff2e | 17 | {1,1}, |
ll17lrc | 2:823dea76ff2e | 18 | {1,1}, |
ll17lrc | 2:823dea76ff2e | 19 | }; |
ll17lrc | 7:35465b3bf586 | 20 | ball_x_pos = x; |
ll17lrc | 7:35465b3bf586 | 21 | ball_y_pos = y; |
ll17lrc | 7:35465b3bf586 | 22 | |
ll17lrc | 2:823dea76ff2e | 23 | } |
ll17lrc | 2:823dea76ff2e | 24 | |
ll17lrc | 13:fd290d2fd917 | 25 | //Takes x and y co-ordinates and draws the ball at that position. |
ll17lrc | 2:823dea76ff2e | 26 | void Ball::draw(N5110 &lcd) |
ll17lrc | 2:823dea76ff2e | 27 | { |
ll17lrc | 7:35465b3bf586 | 28 | lcd.drawSprite(ball_x_pos,ball_y_pos,2,2,(int *)ball); |
ll17lrc | 2:823dea76ff2e | 29 | } |
ll17lrc | 2:823dea76ff2e | 30 | |
ll17lrc | 13:fd290d2fd917 | 31 | //Takes the inputs of the gamepad and updates the x and y co-ords based on the |
ll17lrc | 13:fd290d2fd917 | 32 | //direction of the joystick and whether button A has been pressed. |
ll17lrc | 13:fd290d2fd917 | 33 | //Pressing A double the speed of the ball. |
ll17lrc | 8:10eb578dd754 | 34 | void Ball::update(Direction d,Gamepad &pad) |
ll17lrc | 2:823dea76ff2e | 35 | { |
ll17lrc | 7:35465b3bf586 | 36 | |
ll17lrc | 7:35465b3bf586 | 37 | if(d != CENTRE){ |
ll17lrc | 7:35465b3bf586 | 38 | |
ll17lrc | 8:10eb578dd754 | 39 | if (d == S && ball_y_pos < 47){ |
ll17lrc | 8:10eb578dd754 | 40 | if(pad.A_held() == true){ball_y_pos++;} |
ll17lrc | 7:35465b3bf586 | 41 | ball_y_pos++; |
ll17lrc | 7:35465b3bf586 | 42 | } |
ll17lrc | 2:823dea76ff2e | 43 | |
ll17lrc | 7:35465b3bf586 | 44 | if (d == N && ball_y_pos > 1){ |
ll17lrc | 8:10eb578dd754 | 45 | if(pad.A_held() == true){ball_y_pos--;} |
ll17lrc | 7:35465b3bf586 | 46 | ball_y_pos--; |
ll17lrc | 7:35465b3bf586 | 47 | } |
ll17lrc | 5:5c132202b642 | 48 | |
ll17lrc | 7:35465b3bf586 | 49 | if (d == E && ball_x_pos < 83){ |
ll17lrc | 8:10eb578dd754 | 50 | if(pad.A_held() == true){ball_x_pos++;} |
ll17lrc | 7:35465b3bf586 | 51 | ball_x_pos++; |
ll17lrc | 7:35465b3bf586 | 52 | } |
ll17lrc | 5:5c132202b642 | 53 | |
ll17lrc | 5:5c132202b642 | 54 | if (d == W && ball_x_pos > 0){ |
ll17lrc | 8:10eb578dd754 | 55 | if(pad.A_held() == true){ball_x_pos--;} |
ll17lrc | 7:35465b3bf586 | 56 | ball_x_pos--; |
ll17lrc | 7:35465b3bf586 | 57 | } |
ll17lrc | 7:35465b3bf586 | 58 | |
ll17lrc | 8:10eb578dd754 | 59 | if (d == SE && ball_y_pos < 47 && ball_x_pos < 83){ |
ll17lrc | 8:10eb578dd754 | 60 | if(pad.A_held() == true){ball_y_pos++; |
ll17lrc | 8:10eb578dd754 | 61 | ball_x_pos++;} |
ll17lrc | 7:35465b3bf586 | 62 | ball_x_pos++; |
ll17lrc | 7:35465b3bf586 | 63 | ball_y_pos++; |
ll17lrc | 7:35465b3bf586 | 64 | } |
ll17lrc | 7:35465b3bf586 | 65 | |
ll17lrc | 7:35465b3bf586 | 66 | if (d == NE && ball_y_pos > 1 && ball_x_pos < 83){ |
ll17lrc | 8:10eb578dd754 | 67 | if(pad.A_held() == true){ball_y_pos--; |
ll17lrc | 8:10eb578dd754 | 68 | ball_x_pos++;} |
ll17lrc | 7:35465b3bf586 | 69 | ball_x_pos++; |
ll17lrc | 7:35465b3bf586 | 70 | ball_y_pos--; |
ll17lrc | 2:823dea76ff2e | 71 | }; |
ll17lrc | 2:823dea76ff2e | 72 | |
ll17lrc | 7:35465b3bf586 | 73 | if (d == NW && ball_x_pos > 0 && ball_y_pos > 1){ |
ll17lrc | 8:10eb578dd754 | 74 | if(pad.A_held() == true){ball_y_pos--; |
ll17lrc | 8:10eb578dd754 | 75 | ball_x_pos--;} |
ll17lrc | 7:35465b3bf586 | 76 | ball_x_pos--; |
ll17lrc | 7:35465b3bf586 | 77 | ball_y_pos--; |
ll17lrc | 7:35465b3bf586 | 78 | } |
ll17lrc | 2:823dea76ff2e | 79 | |
ll17lrc | 8:10eb578dd754 | 80 | if (d == SW && ball_x_pos > 0 && ball_y_pos < 47){ |
ll17lrc | 8:10eb578dd754 | 81 | if(pad.A_held() == true){ball_y_pos++; |
ll17lrc | 8:10eb578dd754 | 82 | ball_x_pos--;} |
ll17lrc | 7:35465b3bf586 | 83 | ball_x_pos--; |
ll17lrc | 7:35465b3bf586 | 84 | ball_y_pos++; |
ll17lrc | 7:35465b3bf586 | 85 | } |
ll17lrc | 2:823dea76ff2e | 86 | |
ll17lrc | 7:35465b3bf586 | 87 | } |
ll17lrc | 7:35465b3bf586 | 88 | |
ll17lrc | 7:35465b3bf586 | 89 | |
ll17lrc | 5:5c132202b642 | 90 | |
ll17lrc | 2:823dea76ff2e | 91 | } |
ll17lrc | 4:a9d5fca3b7ba | 92 | |
ll17lrc | 13:fd290d2fd917 | 93 | //Functions to return the x and y co-ords of the ball |
ll17lrc | 4:a9d5fca3b7ba | 94 | int Ball::get_ball_x_pos(){ |
ll17lrc | 4:a9d5fca3b7ba | 95 | return ball_x_pos; |
ll17lrc | 4:a9d5fca3b7ba | 96 | } |
ll17lrc | 4:a9d5fca3b7ba | 97 | |
ll17lrc | 4:a9d5fca3b7ba | 98 | int Ball::get_ball_y_pos(){ |
ll17lrc | 4:a9d5fca3b7ba | 99 | return ball_y_pos; |
ll17lrc | 4:a9d5fca3b7ba | 100 | } |
ll17lrc | 4:a9d5fca3b7ba | 101 | |
ll17lrc | 13:fd290d2fd917 | 102 | //When a level has been completed, resets the balls co-ords |
ll17lrc | 4:a9d5fca3b7ba | 103 | void Ball::level_finish(){ |
ll17lrc | 4:a9d5fca3b7ba | 104 | ball_x_pos = 0; |
ll17lrc | 4:a9d5fca3b7ba | 105 | ball_y_pos = 21; |
ll17lrc | 4:a9d5fca3b7ba | 106 | } |