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@3:a8960004d261, 2019-03-29 (annotated)
- Committer:
- ellisbhastroud
- Date:
- Fri Mar 29 18:45:40 2019 +0000
- Revision:
- 3:a8960004d261
- Child:
- 4:035448357749
Menu Working, Game Mechanisms Developed. Can now use joystick and button to shoot golf ball with power dependant on joystick magnitude.
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 | 3:a8960004d261 | 23 | _x_vel = 0; |
| ellisbhastroud | 3:a8960004d261 | 24 | _y_vel = 0; |
| ellisbhastroud | 3:a8960004d261 | 25 | |
| ellisbhastroud | 3:a8960004d261 | 26 | } |
| ellisbhastroud | 3:a8960004d261 | 27 | |
| ellisbhastroud | 3:a8960004d261 | 28 | void Ball::draw_ball(N5110 &lcd) |
| ellisbhastroud | 3:a8960004d261 | 29 | { |
| ellisbhastroud | 3:a8960004d261 | 30 | lcd.drawRect(_x_pos,_y_pos,3,3,FILL_BLACK); //draws ball |
| ellisbhastroud | 3:a8960004d261 | 31 | } |
| ellisbhastroud | 3:a8960004d261 | 32 | |
| ellisbhastroud | 3:a8960004d261 | 33 | void Ball::draw_aim(N5110 &lcd) |
| ellisbhastroud | 3:a8960004d261 | 34 | { |
| ellisbhastroud | 3:a8960004d261 | 35 | lcd.drawLine((_x_pos+1.0f),(_y_pos+1.0f),(_x_pos+1.0f)-(_joy.x*16.0f),(_y_pos+1.0f)-(-_joy.y*16.0f),2); //draws ball |
| ellisbhastroud | 3:a8960004d261 | 36 | } |
| ellisbhastroud | 3:a8960004d261 | 37 | |
| ellisbhastroud | 3:a8960004d261 | 38 | void Ball::draw_screen(N5110 &lcd) |
| ellisbhastroud | 3:a8960004d261 | 39 | { |
| ellisbhastroud | 3:a8960004d261 | 40 | lcd.drawRect(16,8,51,31,FILL_TRANSPARENT); |
| ellisbhastroud | 3:a8960004d261 | 41 | } |
| ellisbhastroud | 3:a8960004d261 | 42 | |
| ellisbhastroud | 3:a8960004d261 | 43 | void Ball::move_ball() |
| ellisbhastroud | 3:a8960004d261 | 44 | { |
| ellisbhastroud | 3:a8960004d261 | 45 | _x_pos = _x_pos + _x_vel; |
| ellisbhastroud | 3:a8960004d261 | 46 | _y_pos = _y_pos + _y_vel; |
| ellisbhastroud | 3:a8960004d261 | 47 | _x_vel = _x_vel*0.9f; //ball slows down 10% each loop |
| ellisbhastroud | 3:a8960004d261 | 48 | _y_vel = _y_vel*0.9f; |
| ellisbhastroud | 3:a8960004d261 | 49 | check_hit_wall(); |
| ellisbhastroud | 3:a8960004d261 | 50 | |
| ellisbhastroud | 3:a8960004d261 | 51 | } |
| ellisbhastroud | 3:a8960004d261 | 52 | |
| ellisbhastroud | 3:a8960004d261 | 53 | void Ball::shoot_ball(Gamepad &pad) |
| ellisbhastroud | 3:a8960004d261 | 54 | { |
| ellisbhastroud | 3:a8960004d261 | 55 | if(pad.check_event(Gamepad::A_PRESSED) == true && _x_vel < 0.1f && _y_vel < 0.1f){ //if ball stationary and a pressed then shoot |
| ellisbhastroud | 3:a8960004d261 | 56 | |
| ellisbhastroud | 3:a8960004d261 | 57 | _x_vel = 8.0f * _joy.x; //scale x velocity by joystick direction and magnitude |
| ellisbhastroud | 3:a8960004d261 | 58 | _y_vel = 8.0f * -_joy.y; //scale y velocity by joystick direction and magnitude |
| ellisbhastroud | 3:a8960004d261 | 59 | } |
| ellisbhastroud | 3:a8960004d261 | 60 | |
| ellisbhastroud | 3:a8960004d261 | 61 | } |
| ellisbhastroud | 3:a8960004d261 | 62 | |
| ellisbhastroud | 3:a8960004d261 | 63 | void Ball::set_vel(float x_vel, float y_vel) |
| ellisbhastroud | 3:a8960004d261 | 64 | { |
| ellisbhastroud | 3:a8960004d261 | 65 | _x_vel = x_vel; |
| ellisbhastroud | 3:a8960004d261 | 66 | _y_vel = y_vel; |
| ellisbhastroud | 3:a8960004d261 | 67 | } |
| ellisbhastroud | 3:a8960004d261 | 68 | |
| ellisbhastroud | 3:a8960004d261 | 69 | void Ball::check_hit_wall() |
| ellisbhastroud | 3:a8960004d261 | 70 | { |
| ellisbhastroud | 3:a8960004d261 | 71 | |
| ellisbhastroud | 3:a8960004d261 | 72 | if(_x_pos <= 16) { |
| ellisbhastroud | 3:a8960004d261 | 73 | _x_pos = 17; |
| ellisbhastroud | 3:a8960004d261 | 74 | _x_vel = -_x_vel; |
| ellisbhastroud | 3:a8960004d261 | 75 | } |
| ellisbhastroud | 3:a8960004d261 | 76 | |
| ellisbhastroud | 3:a8960004d261 | 77 | else if(_x_pos >= (WIDTH - 18)) { |
| ellisbhastroud | 3:a8960004d261 | 78 | _x_pos = (WIDTH - 20); |
| ellisbhastroud | 3:a8960004d261 | 79 | _x_vel = -_x_vel; |
| ellisbhastroud | 3:a8960004d261 | 80 | } |
| ellisbhastroud | 3:a8960004d261 | 81 | else if(_y_pos <= 8) { |
| ellisbhastroud | 3:a8960004d261 | 82 | _y_pos = 9; |
| ellisbhastroud | 3:a8960004d261 | 83 | _y_vel = -_y_vel; |
| ellisbhastroud | 3:a8960004d261 | 84 | } |
| ellisbhastroud | 3:a8960004d261 | 85 | |
| ellisbhastroud | 3:a8960004d261 | 86 | else if(_y_pos >= (HEIGHT - 11)) { |
| ellisbhastroud | 3:a8960004d261 | 87 | _y_pos = (HEIGHT - 13); |
| ellisbhastroud | 3:a8960004d261 | 88 | _y_vel = -_y_vel; |
| ellisbhastroud | 3:a8960004d261 | 89 | } |
| ellisbhastroud | 3:a8960004d261 | 90 | } |
| ellisbhastroud | 3:a8960004d261 | 91 | |
| ellisbhastroud | 3:a8960004d261 | 92 | void Ball::read_joy(Gamepad &pad) |
| ellisbhastroud | 3:a8960004d261 | 93 | { |
| ellisbhastroud | 3:a8960004d261 | 94 | |
| ellisbhastroud | 3:a8960004d261 | 95 | _coords = pad.get_mapped_coord(); |
| ellisbhastroud | 3:a8960004d261 | 96 | _joy.mag = pad.get_mag(); |
| ellisbhastroud | 3:a8960004d261 | 97 | _joy.x = _coords.x; |
| ellisbhastroud | 3:a8960004d261 | 98 | _joy.y = _coords.y; |
| ellisbhastroud | 3:a8960004d261 | 99 | |
| ellisbhastroud | 3:a8960004d261 | 100 | } |
| ellisbhastroud | 3:a8960004d261 | 101 | //private methods |
| ellisbhastroud | 3:a8960004d261 | 102 |