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.
Paddle/Paddle.cpp@89:806679ed11f2, 2019-05-06 (annotated)
- Committer:
- jamesheavey
- Date:
- Mon May 06 19:24:32 2019 +0000
- Revision:
- 89:806679ed11f2
- Parent:
- 75:d96b177585aa
- Child:
- 90:aea4a1a0f583
- Child:
- 91:c01a736fb0d9
paddle, does it need y and height width in its init?
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| jamesheavey | 0:7d4d2023ed9c | 1 | #include "Paddle.h" |
| jamesheavey | 0:7d4d2023ed9c | 2 | |
| jamesheavey | 15:b867a6620f96 | 3 | FXOS8700CQ acc(I2C_SDA,I2C_SCL); |
| jamesheavey | 15:b867a6620f96 | 4 | |
| jamesheavey | 0:7d4d2023ed9c | 5 | // nothing doing in the constructor and destructor |
| jamesheavey | 0:7d4d2023ed9c | 6 | Paddle::Paddle() |
| jamesheavey | 0:7d4d2023ed9c | 7 | { |
| jamesheavey | 0:7d4d2023ed9c | 8 | |
| jamesheavey | 0:7d4d2023ed9c | 9 | } |
| jamesheavey | 0:7d4d2023ed9c | 10 | |
| jamesheavey | 0:7d4d2023ed9c | 11 | Paddle::~Paddle() |
| jamesheavey | 0:7d4d2023ed9c | 12 | { |
| jamesheavey | 0:7d4d2023ed9c | 13 | |
| jamesheavey | 0:7d4d2023ed9c | 14 | } |
| jamesheavey | 0:7d4d2023ed9c | 15 | |
| jamesheavey | 1:e18615d46071 | 16 | void Paddle::init(int y,int height,int width) |
| jamesheavey | 0:7d4d2023ed9c | 17 | { |
| jamesheavey | 1:e18615d46071 | 18 | _x = WIDTH/2 - width/2; // x value on screen is fixed |
| jamesheavey | 1:e18615d46071 | 19 | _y = y; // y depends on height of screen and height of paddle |
| jamesheavey | 89:806679ed11f2 | 20 | _height = height; // paddle height, defined at init |
| jamesheavey | 89:806679ed11f2 | 21 | _width = width; // paddle width, defined at init |
| jamesheavey | 89:806679ed11f2 | 22 | _sens = 0.5; // default sensitivity of tilt/joystick |
| jamesheavey | 89:806679ed11f2 | 23 | _speed = 4; // default speed = _speed * _sens |
| jamesheavey | 89:806679ed11f2 | 24 | _score = 0; // start score from 0 |
| jamesheavey | 89:806679ed11f2 | 25 | _lives = 6; // number of lives = number of LEDs |
| jamesheavey | 89:806679ed11f2 | 26 | _tilt = false; //used to choose between motion methods |
| jamesheavey | 0:7d4d2023ed9c | 27 | |
| jamesheavey | 0:7d4d2023ed9c | 28 | } |
| jamesheavey | 0:7d4d2023ed9c | 29 | |
| jamesheavey | 0:7d4d2023ed9c | 30 | void Paddle::draw(N5110 &lcd) |
| jamesheavey | 0:7d4d2023ed9c | 31 | { |
| jamesheavey | 0:7d4d2023ed9c | 32 | // draw paddle in screen buffer. |
| jamesheavey | 0:7d4d2023ed9c | 33 | lcd.drawRect(_x,_y,_width,_height,FILL_BLACK); |
| jamesheavey | 0:7d4d2023ed9c | 34 | } |
| jamesheavey | 0:7d4d2023ed9c | 35 | |
| jamesheavey | 0:7d4d2023ed9c | 36 | void Paddle::update(Direction d,float mag) |
| jamesheavey | 0:7d4d2023ed9c | 37 | { |
| jamesheavey | 19:06f4d39b9290 | 38 | acc.init(); |
| jamesheavey | 19:06f4d39b9290 | 39 | |
| jamesheavey | 16:1761dfe801af | 40 | if (_tilt == false) { |
| jamesheavey | 16:1761dfe801af | 41 | _speed = int(mag*10.0f); // scale is arbitrary, could be changed in future |
| jamesheavey | 16:1761dfe801af | 42 | |
| jamesheavey | 16:1761dfe801af | 43 | // update y value depending on direction of movement |
| jamesheavey | 16:1761dfe801af | 44 | // North is decrement as origin is at the top-left so decreasing moves up |
| jamesheavey | 16:1761dfe801af | 45 | if (d == W) { |
| jamesheavey | 72:7254d2a8a1cd | 46 | _x-=_speed * _sens; |
| jamesheavey | 16:1761dfe801af | 47 | } else if (d == E) { |
| jamesheavey | 72:7254d2a8a1cd | 48 | _x+=_speed * _sens; |
| jamesheavey | 16:1761dfe801af | 49 | } |
| jamesheavey | 15:b867a6620f96 | 50 | |
| jamesheavey | 16:1761dfe801af | 51 | // check the y origin to ensure that the paddle doesn't go off screen |
| jamesheavey | 16:1761dfe801af | 52 | if (_x < 1) { |
| jamesheavey | 16:1761dfe801af | 53 | _x = 1; |
| jamesheavey | 16:1761dfe801af | 54 | } |
| jamesheavey | 16:1761dfe801af | 55 | if (_x > WIDTH - _width - 1) { |
| jamesheavey | 16:1761dfe801af | 56 | _x = WIDTH - _width - 1; |
| jamesheavey | 16:1761dfe801af | 57 | } |
| jamesheavey | 16:1761dfe801af | 58 | } |
| jamesheavey | 16:1761dfe801af | 59 | else if (_tilt == true) { |
| jamesheavey | 15:b867a6620f96 | 60 | |
| jamesheavey | 16:1761dfe801af | 61 | Data values = acc.get_values(); |
| jamesheavey | 74:dfb1d693d8e3 | 62 | float roll_rad = atan2(values.ay,values.az); |
| jamesheavey | 19:06f4d39b9290 | 63 | _speed = int((roll_rad*(360/3.14159265))*0.3f); // scale is arbitrary, could be changed in future |
| jamesheavey | 16:1761dfe801af | 64 | |
| jamesheavey | 16:1761dfe801af | 65 | // update y value depending on direction of movement |
| jamesheavey | 16:1761dfe801af | 66 | // North is decrement as origin is at the top-left so decreasing moves up |
| jamesheavey | 72:7254d2a8a1cd | 67 | _x -= _speed * _sens; |
| jamesheavey | 16:1761dfe801af | 68 | } |
| jamesheavey | 0:7d4d2023ed9c | 69 | |
| jamesheavey | 0:7d4d2023ed9c | 70 | // check the y origin to ensure that the paddle doesn't go off screen |
| jamesheavey | 0:7d4d2023ed9c | 71 | if (_x < 1) { |
| jamesheavey | 0:7d4d2023ed9c | 72 | _x = 1; |
| jamesheavey | 0:7d4d2023ed9c | 73 | } |
| jamesheavey | 0:7d4d2023ed9c | 74 | if (_x > WIDTH - _width - 1) { |
| jamesheavey | 0:7d4d2023ed9c | 75 | _x = WIDTH - _width - 1; |
| jamesheavey | 0:7d4d2023ed9c | 76 | } |
| jamesheavey | 0:7d4d2023ed9c | 77 | } |
| jamesheavey | 0:7d4d2023ed9c | 78 | |
| jamesheavey | 0:7d4d2023ed9c | 79 | void Paddle::add_score() |
| jamesheavey | 0:7d4d2023ed9c | 80 | { |
| jamesheavey | 0:7d4d2023ed9c | 81 | _score++; |
| jamesheavey | 0:7d4d2023ed9c | 82 | } |
| jamesheavey | 0:7d4d2023ed9c | 83 | int Paddle::get_score() |
| jamesheavey | 0:7d4d2023ed9c | 84 | { |
| jamesheavey | 0:7d4d2023ed9c | 85 | return _score; |
| jamesheavey | 0:7d4d2023ed9c | 86 | } |
| jamesheavey | 0:7d4d2023ed9c | 87 | |
| jamesheavey | 9:f6f0f39538c7 | 88 | int Paddle::get_lives() |
| jamesheavey | 9:f6f0f39538c7 | 89 | { |
| jamesheavey | 9:f6f0f39538c7 | 90 | return _lives; |
| jamesheavey | 9:f6f0f39538c7 | 91 | } |
| jamesheavey | 9:f6f0f39538c7 | 92 | |
| jamesheavey | 9:f6f0f39538c7 | 93 | void Paddle::lose_life() |
| jamesheavey | 9:f6f0f39538c7 | 94 | { |
| jamesheavey | 9:f6f0f39538c7 | 95 | _lives--; |
| jamesheavey | 9:f6f0f39538c7 | 96 | } |
| jamesheavey | 9:f6f0f39538c7 | 97 | |
| jamesheavey | 0:7d4d2023ed9c | 98 | Vector2D Paddle::get_pos() { |
| jamesheavey | 0:7d4d2023ed9c | 99 | Vector2D p = {_x,_y}; |
| jamesheavey | 0:7d4d2023ed9c | 100 | return p; |
| jamesheavey | 16:1761dfe801af | 101 | } |
| jamesheavey | 16:1761dfe801af | 102 | |
| jamesheavey | 16:1761dfe801af | 103 | void Paddle::set_tilt() |
| jamesheavey | 16:1761dfe801af | 104 | { |
| jamesheavey | 16:1761dfe801af | 105 | _tilt = true; |
| jamesheavey | 16:1761dfe801af | 106 | } |
| jamesheavey | 16:1761dfe801af | 107 | |
| jamesheavey | 16:1761dfe801af | 108 | void Paddle::set_joy() |
| jamesheavey | 16:1761dfe801af | 109 | { |
| jamesheavey | 16:1761dfe801af | 110 | _tilt = false; |
| jamesheavey | 16:1761dfe801af | 111 | } |
| jamesheavey | 69:db1354676fde | 112 | |
| jamesheavey | 69:db1354676fde | 113 | void Paddle::recentre() |
| jamesheavey | 69:db1354676fde | 114 | { |
| jamesheavey | 69:db1354676fde | 115 | _x = WIDTH/2 - 15/2; // x value on screen is fixed |
| jamesheavey | 69:db1354676fde | 116 | } |
| jamesheavey | 72:7254d2a8a1cd | 117 | |
| jamesheavey | 72:7254d2a8a1cd | 118 | void Paddle::set_sens(float sens) |
| jamesheavey | 72:7254d2a8a1cd | 119 | { |
| jamesheavey | 72:7254d2a8a1cd | 120 | _sens = sens; |
| jamesheavey | 72:7254d2a8a1cd | 121 | } |