Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed FATFileSystem
Ball/Ball.cpp@5:0b31909caf7f, 2019-04-17 (annotated)
- Committer:
- ellisbhastroud
- Date:
- Wed Apr 17 10:27:09 2019 +0000
- Revision:
- 5:0b31909caf7f
- Parent:
- 4:035448357749
- Child:
- 8:d410856c6d04
Levels Added Not functioning
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| ellisbhastroud | 3:a8960004d261 | 1 | #include "Ball.h" |
| ellisbhastroud | 3:a8960004d261 | 2 | |
| ellisbhastroud | 3:a8960004d261 | 3 | //constructor |
| ellisbhastroud | 3:a8960004d261 | 4 | |
| ellisbhastroud | 3:a8960004d261 | 5 | Ball::Ball() |
| ellisbhastroud | 3:a8960004d261 | 6 | { |
| ellisbhastroud | 3:a8960004d261 | 7 | |
| ellisbhastroud | 3:a8960004d261 | 8 | } |
| ellisbhastroud | 3:a8960004d261 | 9 | |
| ellisbhastroud | 3:a8960004d261 | 10 | //deconstructor |
| ellisbhastroud | 3:a8960004d261 | 11 | |
| ellisbhastroud | 3:a8960004d261 | 12 | Ball::~Ball() |
| ellisbhastroud | 3:a8960004d261 | 13 | { |
| ellisbhastroud | 3:a8960004d261 | 14 | |
| ellisbhastroud | 3:a8960004d261 | 15 | } |
| ellisbhastroud | 3:a8960004d261 | 16 | |
| ellisbhastroud | 3:a8960004d261 | 17 | //public methods |
| ellisbhastroud | 3:a8960004d261 | 18 | |
| ellisbhastroud | 3:a8960004d261 | 19 | void Ball::init(float x, float y) //ball starts stationary in x and y positions given |
| ellisbhastroud | 3:a8960004d261 | 20 | { |
| ellisbhastroud | 3:a8960004d261 | 21 | _x_pos = x; |
| ellisbhastroud | 3:a8960004d261 | 22 | _y_pos = y; |
| ellisbhastroud | 4:035448357749 | 23 | _x_vel = 0.0f; |
| ellisbhastroud | 4:035448357749 | 24 | _y_vel = 0.0f; |
| ellisbhastroud | 4:035448357749 | 25 | _shot_count = 0; |
| ellisbhastroud | 3:a8960004d261 | 26 | |
| ellisbhastroud | 3:a8960004d261 | 27 | } |
| ellisbhastroud | 3:a8960004d261 | 28 | |
| ellisbhastroud | 5:0b31909caf7f | 29 | void Ball::drawBall(N5110 &lcd) |
| ellisbhastroud | 3:a8960004d261 | 30 | { |
| ellisbhastroud | 5:0b31909caf7f | 31 | lcd.drawRect(_x_pos,_y_pos,2,2,FILL_BLACK); //draws ball |
| ellisbhastroud | 3:a8960004d261 | 32 | } |
| ellisbhastroud | 3:a8960004d261 | 33 | |
| ellisbhastroud | 5:0b31909caf7f | 34 | void Ball::printShotCount(N5110 &lcd) |
| ellisbhastroud | 3:a8960004d261 | 35 | { |
| ellisbhastroud | 5:0b31909caf7f | 36 | char buffer[14]; |
| ellisbhastroud | 5:0b31909caf7f | 37 | sprintf(buffer,"Shot %i",_shot_count); |
| ellisbhastroud | 5:0b31909caf7f | 38 | lcd.printString(buffer,40,0); |
| ellisbhastroud | 4:035448357749 | 39 | } |
| ellisbhastroud | 4:035448357749 | 40 | |
| ellisbhastroud | 5:0b31909caf7f | 41 | void Ball::drawPower(N5110 &lcd, Gamepad &pad) |
| ellisbhastroud | 5:0b31909caf7f | 42 | { |
| ellisbhastroud | 5:0b31909caf7f | 43 | |
| ellisbhastroud | 5:0b31909caf7f | 44 | _mag = pad.get_mag(); |
| ellisbhastroud | 5:0b31909caf7f | 45 | lcd.drawRect(0,0,36,6,FILL_TRANSPARENT); |
| ellisbhastroud | 5:0b31909caf7f | 46 | lcd.drawRect(0,0,36*_mag,6,FILL_BLACK); |
| ellisbhastroud | 3:a8960004d261 | 47 | } |
| ellisbhastroud | 3:a8960004d261 | 48 | |
| ellisbhastroud | 3:a8960004d261 | 49 | |
| ellisbhastroud | 5:0b31909caf7f | 50 | void Ball::move_ball(int frame_rate) |
| ellisbhastroud | 3:a8960004d261 | 51 | { |
| ellisbhastroud | 4:035448357749 | 52 | _x_pos = _x_pos + _x_vel*10.0f/frame_rate; //frame_rate used to scale ball movement so that it is the same for any fps |
| ellisbhastroud | 4:035448357749 | 53 | _y_pos = _y_pos + _y_vel*10.0f/frame_rate; |
| ellisbhastroud | 4:035448357749 | 54 | _x_vel = _x_vel*(1.0f-(0.8f/frame_rate)); //ball slows down 10% each loop (scaled by time between frames) |
| ellisbhastroud | 4:035448357749 | 55 | _y_vel = _y_vel*(1.0f-(0.8f/frame_rate)); |
| ellisbhastroud | 3:a8960004d261 | 56 | |
| ellisbhastroud | 3:a8960004d261 | 57 | } |
| ellisbhastroud | 3:a8960004d261 | 58 | |
| ellisbhastroud | 4:035448357749 | 59 | Vector2D Ball::get_ball_pos() |
| ellisbhastroud | 4:035448357749 | 60 | { |
| ellisbhastroud | 4:035448357749 | 61 | Vector2D pos = {_x_pos, _y_pos}; |
| ellisbhastroud | 4:035448357749 | 62 | return pos; |
| ellisbhastroud | 4:035448357749 | 63 | } |
| ellisbhastroud | 4:035448357749 | 64 | |
| ellisbhastroud | 3:a8960004d261 | 65 | void Ball::shoot_ball(Gamepad &pad) |
| ellisbhastroud | 5:0b31909caf7f | 66 | { |
| ellisbhastroud | 5:0b31909caf7f | 67 | _joystick = pad.get_mapped_coord(); |
| ellisbhastroud | 3:a8960004d261 | 68 | |
| ellisbhastroud | 4:035448357749 | 69 | if(pad.check_event(Gamepad::A_PRESSED) == true && abs(_x_vel) < 0.05f && abs(_y_vel) < 0.05f){ //if ball stationary and a pressed then shoot |
| ellisbhastroud | 5:0b31909caf7f | 70 | _x_vel = 8.0f * _joystick.x; //scale x velocity by joystick direction and magnitude |
| ellisbhastroud | 5:0b31909caf7f | 71 | _y_vel = 8.0f * -_joystick.y; //scale y velocity by joystick direction and magnitude |
| ellisbhastroud | 5:0b31909caf7f | 72 | _shot_count ++; //increment shot count |
| ellisbhastroud | 3:a8960004d261 | 73 | } |
| ellisbhastroud | 3:a8960004d261 | 74 | |
| ellisbhastroud | 3:a8960004d261 | 75 | } |
| ellisbhastroud | 3:a8960004d261 | 76 | |
| ellisbhastroud | 4:035448357749 | 77 | int Ball::get_shot_count() |
| ellisbhastroud | 4:035448357749 | 78 | { |
| ellisbhastroud | 4:035448357749 | 79 | int shot_count = _shot_count; |
| ellisbhastroud | 4:035448357749 | 80 | return shot_count; |
| ellisbhastroud | 4:035448357749 | 81 | } |
| ellisbhastroud | 4:035448357749 | 82 | |
| ellisbhastroud | 3:a8960004d261 | 83 | void Ball::set_vel(float x_vel, float y_vel) |
| ellisbhastroud | 3:a8960004d261 | 84 | { |
| ellisbhastroud | 3:a8960004d261 | 85 | _x_vel = x_vel; |
| ellisbhastroud | 3:a8960004d261 | 86 | _y_vel = y_vel; |
| ellisbhastroud | 3:a8960004d261 | 87 | } |
| ellisbhastroud | 3:a8960004d261 | 88 | |
| ellisbhastroud | 3:a8960004d261 | 89 | void Ball::read_joy(Gamepad &pad) |
| ellisbhastroud | 3:a8960004d261 | 90 | { |
| ellisbhastroud | 3:a8960004d261 | 91 | |
| ellisbhastroud | 4:035448357749 | 92 | _joystick = pad.get_mapped_coord(); //x and y coordinates from joystick assigned |
| ellisbhastroud | 4:035448357749 | 93 | |
| ellisbhastroud | 3:a8960004d261 | 94 | |
| ellisbhastroud | 3:a8960004d261 | 95 | } |
| ellisbhastroud | 5:0b31909caf7f | 96 | |
| ellisbhastroud | 5:0b31909caf7f | 97 | void Ball::check_wall_bounce() //check before and after move_ball called |
| ellisbhastroud | 5:0b31909caf7f | 98 | { |
| ellisbhastroud | 5:0b31909caf7f | 99 | if(_x_pos - 1 < 9 && _y_pos >= 26 && _y_pos <= 40 && _x_vel < 0){ // left wall (x=9 ,26<=y<=40) |
| ellisbhastroud | 5:0b31909caf7f | 100 | _x_pos = 10; |
| ellisbhastroud | 5:0b31909caf7f | 101 | _x_vel = -0.95f*_x_vel; //5% velocity lost on impact and bounce |
| ellisbhastroud | 5:0b31909caf7f | 102 | } |
| ellisbhastroud | 5:0b31909caf7f | 103 | if(_x_pos + 1 > 74 && _y_pos <= 40 && _y_pos >= 9 && _x_vel > 0){ //right wall x + 1 |
| ellisbhastroud | 5:0b31909caf7f | 104 | _x_pos = 72; |
| ellisbhastroud | 5:0b31909caf7f | 105 | _x_vel = -0.95f*_x_vel; |
| ellisbhastroud | 5:0b31909caf7f | 106 | } |
| ellisbhastroud | 5:0b31909caf7f | 107 | if(_y_pos - 1 < 9 && _x_pos <= 74 && _x_pos >= 50 && _y_vel < 0){ //top wall y -1 |
| ellisbhastroud | 5:0b31909caf7f | 108 | _y_pos = 10; |
| ellisbhastroud | 5:0b31909caf7f | 109 | _y_vel = -0.95f*_y_vel; |
| ellisbhastroud | 5:0b31909caf7f | 110 | } |
| ellisbhastroud | 5:0b31909caf7f | 111 | if(_y_pos + 1 > 40 && _x_pos >= 9 && _x_pos <= 74 && _y_vel > 0){ //bottom wall y + 2 |
| ellisbhastroud | 5:0b31909caf7f | 112 | _y_pos = 39; |
| ellisbhastroud | 5:0b31909caf7f | 113 | _y_vel = -0.95f*_y_vel; |
| ellisbhastroud | 5:0b31909caf7f | 114 | } |
| ellisbhastroud | 5:0b31909caf7f | 115 | if(_x_pos - 1 < 50 && _y_pos >= 9 && _y_pos <= 26 && _x_vel < 0){ // left wall x -1 |
| ellisbhastroud | 5:0b31909caf7f | 116 | _x_pos = 51; |
| ellisbhastroud | 5:0b31909caf7f | 117 | _x_vel = -0.95f*_x_vel; //5% velocity lost on impact and bounce |
| ellisbhastroud | 5:0b31909caf7f | 118 | } |
| ellisbhastroud | 5:0b31909caf7f | 119 | if(_y_pos - 1 < 26 && _x_pos <= 50 && _x_pos >= 9 && _y_vel < 0 ){ //top wall y -1 |
| ellisbhastroud | 5:0b31909caf7f | 120 | _y_pos = 27; |
| ellisbhastroud | 5:0b31909caf7f | 121 | _y_vel = -0.95f*_y_vel; |
| ellisbhastroud | 5:0b31909caf7f | 122 | } |
| ellisbhastroud | 5:0b31909caf7f | 123 | } |
| ellisbhastroud | 3:a8960004d261 | 124 | //private methods |
| ellisbhastroud | 3:a8960004d261 | 125 |