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@4:035448357749, 2019-04-08 (annotated)
- Committer:
- ellisbhastroud
- Date:
- Mon Apr 08 15:10:28 2019 +0000
- Revision:
- 4:035448357749
- Parent:
- 3:a8960004d261
- Child:
- 5:0b31909caf7f
Settings section of menu fully 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 | 3:a8960004d261 | 29 | void Ball::draw_ball(N5110 &lcd) |
| ellisbhastroud | 3:a8960004d261 | 30 | { |
| ellisbhastroud | 3:a8960004d261 | 31 | lcd.drawRect(_x_pos,_y_pos,3,3,FILL_BLACK); //draws ball |
| ellisbhastroud | 3:a8960004d261 | 32 | } |
| ellisbhastroud | 3:a8960004d261 | 33 | |
| ellisbhastroud | 4:035448357749 | 34 | void Ball::draw_aim(N5110 &lcd, Gamepad &pad) |
| ellisbhastroud | 3:a8960004d261 | 35 | { |
| ellisbhastroud | 4:035448357749 | 36 | read_joy(pad); |
| ellisbhastroud | 4:035448357749 | 37 | lcd.drawLine((_x_pos+1.0f),(_y_pos+1.0f),(_x_pos+1.0f)+(_joystick.x*16.0f),(_y_pos+1.0f)-(_joystick.y*16.0f),1); //draws ball |
| ellisbhastroud | 4:035448357749 | 38 | } |
| ellisbhastroud | 4:035448357749 | 39 | |
| ellisbhastroud | 4:035448357749 | 40 | void Ball::draw_course(N5110 &lcd) |
| ellisbhastroud | 4:035448357749 | 41 | { |
| ellisbhastroud | 4:035448357749 | 42 | lcd.drawRect(0,16,83,32,FILL_TRANSPARENT); |
| ellisbhastroud | 3:a8960004d261 | 43 | } |
| ellisbhastroud | 3:a8960004d261 | 44 | |
| ellisbhastroud | 4:035448357749 | 45 | void Ball::draw_screen(N5110 &lcd, Gamepad &pad) { |
| ellisbhastroud | 4:035448357749 | 46 | |
| ellisbhastroud | 4:035448357749 | 47 | draw_course(lcd); |
| ellisbhastroud | 4:035448357749 | 48 | draw_ball(lcd); |
| ellisbhastroud | 4:035448357749 | 49 | draw_aim(lcd, pad); |
| ellisbhastroud | 4:035448357749 | 50 | print_shot_count(lcd); |
| ellisbhastroud | 3:a8960004d261 | 51 | } |
| ellisbhastroud | 3:a8960004d261 | 52 | |
| ellisbhastroud | 4:035448357749 | 53 | void Ball::move_ball(int frame_rate, N5110 &lcd) |
| ellisbhastroud | 3:a8960004d261 | 54 | { |
| ellisbhastroud | 4:035448357749 | 55 | check_bounce(lcd); |
| ellisbhastroud | 4:035448357749 | 56 | _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 | 57 | _y_pos = _y_pos + _y_vel*10.0f/frame_rate; |
| ellisbhastroud | 4:035448357749 | 58 | _x_vel = _x_vel*(1.0f-(0.8f/frame_rate)); //ball slows down 10% each loop (scaled by time between frames) |
| ellisbhastroud | 4:035448357749 | 59 | _y_vel = _y_vel*(1.0f-(0.8f/frame_rate)); |
| ellisbhastroud | 4:035448357749 | 60 | check_bounce(lcd); |
| ellisbhastroud | 3:a8960004d261 | 61 | |
| ellisbhastroud | 3:a8960004d261 | 62 | } |
| ellisbhastroud | 3:a8960004d261 | 63 | |
| ellisbhastroud | 4:035448357749 | 64 | Vector2D Ball::get_ball_pos() |
| ellisbhastroud | 4:035448357749 | 65 | { |
| ellisbhastroud | 4:035448357749 | 66 | Vector2D pos = {_x_pos, _y_pos}; |
| ellisbhastroud | 4:035448357749 | 67 | return pos; |
| ellisbhastroud | 4:035448357749 | 68 | } |
| ellisbhastroud | 4:035448357749 | 69 | |
| ellisbhastroud | 3:a8960004d261 | 70 | void Ball::shoot_ball(Gamepad &pad) |
| ellisbhastroud | 3:a8960004d261 | 71 | { |
| ellisbhastroud | 3:a8960004d261 | 72 | |
| ellisbhastroud | 4:035448357749 | 73 | 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 | 4:035448357749 | 74 | _x_vel = 8.0f * _joystick.x; //scale x velocity by joystick direction and magnitude |
| ellisbhastroud | 4:035448357749 | 75 | _y_vel = 8.0f * -_joystick.y; //scale y velocity by joystick direction and magnitude |
| ellisbhastroud | 4:035448357749 | 76 | _shot_count ++; //increment shot count |
| ellisbhastroud | 4:035448357749 | 77 | |
| ellisbhastroud | 3:a8960004d261 | 78 | } |
| ellisbhastroud | 3:a8960004d261 | 79 | |
| ellisbhastroud | 3:a8960004d261 | 80 | } |
| ellisbhastroud | 3:a8960004d261 | 81 | |
| ellisbhastroud | 4:035448357749 | 82 | int Ball::get_shot_count() |
| ellisbhastroud | 4:035448357749 | 83 | { |
| ellisbhastroud | 4:035448357749 | 84 | int shot_count = _shot_count; |
| ellisbhastroud | 4:035448357749 | 85 | return shot_count; |
| ellisbhastroud | 4:035448357749 | 86 | } |
| ellisbhastroud | 4:035448357749 | 87 | |
| ellisbhastroud | 4:035448357749 | 88 | void Ball::print_shot_count(N5110 &lcd) |
| ellisbhastroud | 4:035448357749 | 89 | { |
| ellisbhastroud | 4:035448357749 | 90 | char buffer[14]; |
| ellisbhastroud | 4:035448357749 | 91 | sprintf(buffer,"Shot Count = %i",_shot_count); |
| ellisbhastroud | 4:035448357749 | 92 | lcd.printString(buffer,0,0); |
| ellisbhastroud | 4:035448357749 | 93 | } |
| ellisbhastroud | 4:035448357749 | 94 | |
| ellisbhastroud | 3:a8960004d261 | 95 | void Ball::set_vel(float x_vel, float y_vel) |
| ellisbhastroud | 3:a8960004d261 | 96 | { |
| ellisbhastroud | 3:a8960004d261 | 97 | _x_vel = x_vel; |
| ellisbhastroud | 3:a8960004d261 | 98 | _y_vel = y_vel; |
| ellisbhastroud | 3:a8960004d261 | 99 | } |
| ellisbhastroud | 3:a8960004d261 | 100 | |
| ellisbhastroud | 4:035448357749 | 101 | void Ball::check_bounce(N5110 &lcd) |
| ellisbhastroud | 3:a8960004d261 | 102 | { |
| ellisbhastroud | 3:a8960004d261 | 103 | |
| ellisbhastroud | 4:035448357749 | 104 | if(_x_pos - 1 < 0 ) { // |
| ellisbhastroud | 4:035448357749 | 105 | _x_pos = 1; |
| ellisbhastroud | 3:a8960004d261 | 106 | _x_vel = -_x_vel; |
| ellisbhastroud | 3:a8960004d261 | 107 | } |
| ellisbhastroud | 4:035448357749 | 108 | else if(_y_pos + 3 > 48) { |
| ellisbhastroud | 4:035448357749 | 109 | _y_pos = 45; |
| ellisbhastroud | 3:a8960004d261 | 110 | _y_vel = -_y_vel; |
| ellisbhastroud | 3:a8960004d261 | 111 | } |
| ellisbhastroud | 4:035448357749 | 112 | else if(_x_pos + 3 > 83 ) { |
| ellisbhastroud | 4:035448357749 | 113 | _x_pos = 80; |
| ellisbhastroud | 4:035448357749 | 114 | _x_vel = -_x_vel; |
| ellisbhastroud | 4:035448357749 | 115 | } |
| ellisbhastroud | 4:035448357749 | 116 | else if(_y_pos - 1 < 16) { |
| ellisbhastroud | 4:035448357749 | 117 | _y_pos = 17; |
| ellisbhastroud | 4:035448357749 | 118 | _y_vel = -_y_vel; |
| ellisbhastroud | 4:035448357749 | 119 | } |
| ellisbhastroud | 4:035448357749 | 120 | |
| ellisbhastroud | 4:035448357749 | 121 | |
| ellisbhastroud | 3:a8960004d261 | 122 | } |
| ellisbhastroud | 3:a8960004d261 | 123 | |
| ellisbhastroud | 3:a8960004d261 | 124 | void Ball::read_joy(Gamepad &pad) |
| ellisbhastroud | 3:a8960004d261 | 125 | { |
| ellisbhastroud | 3:a8960004d261 | 126 | |
| ellisbhastroud | 4:035448357749 | 127 | _joystick = pad.get_mapped_coord(); //x and y coordinates from joystick assigned |
| ellisbhastroud | 4:035448357749 | 128 | |
| ellisbhastroud | 3:a8960004d261 | 129 | |
| ellisbhastroud | 3:a8960004d261 | 130 | } |
| ellisbhastroud | 3:a8960004d261 | 131 | //private methods |
| ellisbhastroud | 3:a8960004d261 | 132 |