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@16:1761dfe801af, 2019-04-23 (annotated)
- Committer:
- jamesheavey
- Date:
- Tue Apr 23 17:51:42 2019 +0000
- Revision:
- 16:1761dfe801af
- Parent:
- 15:b867a6620f96
- Child:
- 18:e5eb50c5e61f
able to choose between joy and tilt
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 | 0:7d4d2023ed9c | 20 | _height = height; |
| jamesheavey | 0:7d4d2023ed9c | 21 | _width = width; |
| jamesheavey | 0:7d4d2023ed9c | 22 | _speed = 1; // default speed |
| jamesheavey | 0:7d4d2023ed9c | 23 | _score = 0; // start score from zero |
| jamesheavey | 9:f6f0f39538c7 | 24 | _lives = 6; |
| jamesheavey | 16:1761dfe801af | 25 | _tilt = false; //use to choose between motion methods |
| jamesheavey | 0:7d4d2023ed9c | 26 | |
| jamesheavey | 0:7d4d2023ed9c | 27 | } |
| jamesheavey | 0:7d4d2023ed9c | 28 | |
| jamesheavey | 0:7d4d2023ed9c | 29 | void Paddle::draw(N5110 &lcd) |
| jamesheavey | 0:7d4d2023ed9c | 30 | { |
| jamesheavey | 0:7d4d2023ed9c | 31 | // draw paddle in screen buffer. |
| jamesheavey | 0:7d4d2023ed9c | 32 | lcd.drawRect(_x,_y,_width,_height,FILL_BLACK); |
| jamesheavey | 0:7d4d2023ed9c | 33 | } |
| jamesheavey | 0:7d4d2023ed9c | 34 | |
| jamesheavey | 0:7d4d2023ed9c | 35 | void Paddle::update(Direction d,float mag) |
| jamesheavey | 0:7d4d2023ed9c | 36 | { |
| jamesheavey | 16:1761dfe801af | 37 | if (_tilt == false) { |
| jamesheavey | 16:1761dfe801af | 38 | _speed = int(mag*10.0f); // scale is arbitrary, could be changed in future |
| jamesheavey | 16:1761dfe801af | 39 | |
| jamesheavey | 16:1761dfe801af | 40 | // update y value depending on direction of movement |
| jamesheavey | 16:1761dfe801af | 41 | // North is decrement as origin is at the top-left so decreasing moves up |
| jamesheavey | 16:1761dfe801af | 42 | if (d == W) { |
| jamesheavey | 16:1761dfe801af | 43 | _x-=_speed; |
| jamesheavey | 16:1761dfe801af | 44 | } else if (d == E) { |
| jamesheavey | 16:1761dfe801af | 45 | _x+=_speed; |
| jamesheavey | 16:1761dfe801af | 46 | } |
| jamesheavey | 15:b867a6620f96 | 47 | |
| jamesheavey | 16:1761dfe801af | 48 | // check the y origin to ensure that the paddle doesn't go off screen |
| jamesheavey | 16:1761dfe801af | 49 | if (_x < 1) { |
| jamesheavey | 16:1761dfe801af | 50 | _x = 1; |
| jamesheavey | 16:1761dfe801af | 51 | } |
| jamesheavey | 16:1761dfe801af | 52 | if (_x > WIDTH - _width - 1) { |
| jamesheavey | 16:1761dfe801af | 53 | _x = WIDTH - _width - 1; |
| jamesheavey | 16:1761dfe801af | 54 | } |
| jamesheavey | 16:1761dfe801af | 55 | } |
| jamesheavey | 16:1761dfe801af | 56 | else if (_tilt == true) { |
| jamesheavey | 15:b867a6620f96 | 57 | |
| jamesheavey | 16:1761dfe801af | 58 | Data values = acc.get_values(); |
| jamesheavey | 16:1761dfe801af | 59 | float roll_rad = atan2(values.ay,values.az); |
| jamesheavey | 16:1761dfe801af | 60 | _speed = int((roll_rad*(360/3.14159265))*0.3f); // scale is arbitrary, could be changed in future |
| jamesheavey | 16:1761dfe801af | 61 | |
| jamesheavey | 16:1761dfe801af | 62 | // update y value depending on direction of movement |
| jamesheavey | 16:1761dfe801af | 63 | // North is decrement as origin is at the top-left so decreasing moves up |
| jamesheavey | 16:1761dfe801af | 64 | _x -= _speed; |
| jamesheavey | 16:1761dfe801af | 65 | } |
| jamesheavey | 0:7d4d2023ed9c | 66 | |
| jamesheavey | 0:7d4d2023ed9c | 67 | // check the y origin to ensure that the paddle doesn't go off screen |
| jamesheavey | 0:7d4d2023ed9c | 68 | if (_x < 1) { |
| jamesheavey | 0:7d4d2023ed9c | 69 | _x = 1; |
| jamesheavey | 0:7d4d2023ed9c | 70 | } |
| jamesheavey | 0:7d4d2023ed9c | 71 | if (_x > WIDTH - _width - 1) { |
| jamesheavey | 0:7d4d2023ed9c | 72 | _x = WIDTH - _width - 1; |
| jamesheavey | 0:7d4d2023ed9c | 73 | } |
| jamesheavey | 0:7d4d2023ed9c | 74 | } |
| jamesheavey | 0:7d4d2023ed9c | 75 | |
| jamesheavey | 0:7d4d2023ed9c | 76 | void Paddle::add_score() |
| jamesheavey | 0:7d4d2023ed9c | 77 | { |
| jamesheavey | 0:7d4d2023ed9c | 78 | _score++; |
| jamesheavey | 0:7d4d2023ed9c | 79 | } |
| jamesheavey | 0:7d4d2023ed9c | 80 | int Paddle::get_score() |
| jamesheavey | 0:7d4d2023ed9c | 81 | { |
| jamesheavey | 0:7d4d2023ed9c | 82 | return _score; |
| jamesheavey | 0:7d4d2023ed9c | 83 | } |
| jamesheavey | 0:7d4d2023ed9c | 84 | |
| jamesheavey | 9:f6f0f39538c7 | 85 | int Paddle::get_lives() |
| jamesheavey | 9:f6f0f39538c7 | 86 | { |
| jamesheavey | 9:f6f0f39538c7 | 87 | return _lives; |
| jamesheavey | 9:f6f0f39538c7 | 88 | } |
| jamesheavey | 9:f6f0f39538c7 | 89 | |
| jamesheavey | 9:f6f0f39538c7 | 90 | void Paddle::lose_life() |
| jamesheavey | 9:f6f0f39538c7 | 91 | { |
| jamesheavey | 9:f6f0f39538c7 | 92 | _lives--; |
| jamesheavey | 9:f6f0f39538c7 | 93 | } |
| jamesheavey | 9:f6f0f39538c7 | 94 | |
| jamesheavey | 0:7d4d2023ed9c | 95 | Vector2D Paddle::get_pos() { |
| jamesheavey | 0:7d4d2023ed9c | 96 | Vector2D p = {_x,_y}; |
| jamesheavey | 0:7d4d2023ed9c | 97 | return p; |
| jamesheavey | 16:1761dfe801af | 98 | } |
| jamesheavey | 16:1761dfe801af | 99 | |
| jamesheavey | 16:1761dfe801af | 100 | void Paddle::set_tilt() |
| jamesheavey | 16:1761dfe801af | 101 | { |
| jamesheavey | 16:1761dfe801af | 102 | _tilt = true; |
| jamesheavey | 16:1761dfe801af | 103 | } |
| jamesheavey | 16:1761dfe801af | 104 | |
| jamesheavey | 16:1761dfe801af | 105 | void Paddle::set_joy() |
| jamesheavey | 16:1761dfe801af | 106 | { |
| jamesheavey | 16:1761dfe801af | 107 | _tilt = false; |
| jamesheavey | 16:1761dfe801af | 108 | } |