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@9:f6f0f39538c7, 2019-04-21 (annotated)
- Committer:
- jamesheavey
- Date:
- Sun Apr 21 20:44:24 2019 +0000
- Revision:
- 9:f6f0f39538c7
- Parent:
- 1:e18615d46071
- Child:
- 15:b867a6620f96
added lives leds, need to make a lose screen when lives = 0 and need to sort collision detection on brick
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 | 0:7d4d2023ed9c | 3 | // nothing doing in the constructor and destructor |
| jamesheavey | 0:7d4d2023ed9c | 4 | Paddle::Paddle() |
| jamesheavey | 0:7d4d2023ed9c | 5 | { |
| jamesheavey | 0:7d4d2023ed9c | 6 | |
| jamesheavey | 0:7d4d2023ed9c | 7 | } |
| jamesheavey | 0:7d4d2023ed9c | 8 | |
| jamesheavey | 0:7d4d2023ed9c | 9 | Paddle::~Paddle() |
| jamesheavey | 0:7d4d2023ed9c | 10 | { |
| jamesheavey | 0:7d4d2023ed9c | 11 | |
| jamesheavey | 0:7d4d2023ed9c | 12 | } |
| jamesheavey | 0:7d4d2023ed9c | 13 | |
| jamesheavey | 1:e18615d46071 | 14 | void Paddle::init(int y,int height,int width) |
| jamesheavey | 0:7d4d2023ed9c | 15 | { |
| jamesheavey | 1:e18615d46071 | 16 | _x = WIDTH/2 - width/2; // x value on screen is fixed |
| jamesheavey | 1:e18615d46071 | 17 | _y = y; // y depends on height of screen and height of paddle |
| jamesheavey | 0:7d4d2023ed9c | 18 | _height = height; |
| jamesheavey | 0:7d4d2023ed9c | 19 | _width = width; |
| jamesheavey | 0:7d4d2023ed9c | 20 | _speed = 1; // default speed |
| jamesheavey | 0:7d4d2023ed9c | 21 | _score = 0; // start score from zero |
| jamesheavey | 9:f6f0f39538c7 | 22 | _lives = 6; |
| jamesheavey | 0:7d4d2023ed9c | 23 | |
| jamesheavey | 0:7d4d2023ed9c | 24 | } |
| jamesheavey | 0:7d4d2023ed9c | 25 | |
| jamesheavey | 0:7d4d2023ed9c | 26 | void Paddle::draw(N5110 &lcd) |
| jamesheavey | 0:7d4d2023ed9c | 27 | { |
| jamesheavey | 0:7d4d2023ed9c | 28 | // draw paddle in screen buffer. |
| jamesheavey | 0:7d4d2023ed9c | 29 | lcd.drawRect(_x,_y,_width,_height,FILL_BLACK); |
| jamesheavey | 0:7d4d2023ed9c | 30 | } |
| jamesheavey | 0:7d4d2023ed9c | 31 | |
| jamesheavey | 0:7d4d2023ed9c | 32 | void Paddle::update(Direction d,float mag) |
| jamesheavey | 0:7d4d2023ed9c | 33 | { |
| jamesheavey | 0:7d4d2023ed9c | 34 | _speed = int(mag*10.0f); // scale is arbitrary, could be changed in future |
| jamesheavey | 0:7d4d2023ed9c | 35 | |
| jamesheavey | 0:7d4d2023ed9c | 36 | // update y value depending on direction of movement |
| jamesheavey | 0:7d4d2023ed9c | 37 | // North is decrement as origin is at the top-left so decreasing moves up |
| jamesheavey | 0:7d4d2023ed9c | 38 | if (d == W) { |
| jamesheavey | 0:7d4d2023ed9c | 39 | _x-=_speed; |
| jamesheavey | 0:7d4d2023ed9c | 40 | } else if (d == E) { |
| jamesheavey | 0:7d4d2023ed9c | 41 | _x+=_speed; |
| jamesheavey | 0:7d4d2023ed9c | 42 | } |
| jamesheavey | 0:7d4d2023ed9c | 43 | |
| jamesheavey | 0:7d4d2023ed9c | 44 | // check the y origin to ensure that the paddle doesn't go off screen |
| jamesheavey | 0:7d4d2023ed9c | 45 | if (_x < 1) { |
| jamesheavey | 0:7d4d2023ed9c | 46 | _x = 1; |
| jamesheavey | 0:7d4d2023ed9c | 47 | } |
| jamesheavey | 0:7d4d2023ed9c | 48 | if (_x > WIDTH - _width - 1) { |
| jamesheavey | 0:7d4d2023ed9c | 49 | _x = WIDTH - _width - 1; |
| jamesheavey | 0:7d4d2023ed9c | 50 | } |
| jamesheavey | 0:7d4d2023ed9c | 51 | } |
| jamesheavey | 0:7d4d2023ed9c | 52 | |
| jamesheavey | 0:7d4d2023ed9c | 53 | void Paddle::add_score() |
| jamesheavey | 0:7d4d2023ed9c | 54 | { |
| jamesheavey | 0:7d4d2023ed9c | 55 | _score++; |
| jamesheavey | 0:7d4d2023ed9c | 56 | } |
| jamesheavey | 0:7d4d2023ed9c | 57 | int Paddle::get_score() |
| jamesheavey | 0:7d4d2023ed9c | 58 | { |
| jamesheavey | 0:7d4d2023ed9c | 59 | return _score; |
| jamesheavey | 0:7d4d2023ed9c | 60 | } |
| jamesheavey | 0:7d4d2023ed9c | 61 | |
| jamesheavey | 9:f6f0f39538c7 | 62 | int Paddle::get_lives() |
| jamesheavey | 9:f6f0f39538c7 | 63 | { |
| jamesheavey | 9:f6f0f39538c7 | 64 | return _lives; |
| jamesheavey | 9:f6f0f39538c7 | 65 | } |
| jamesheavey | 9:f6f0f39538c7 | 66 | |
| jamesheavey | 9:f6f0f39538c7 | 67 | void Paddle::lose_life() |
| jamesheavey | 9:f6f0f39538c7 | 68 | { |
| jamesheavey | 9:f6f0f39538c7 | 69 | _lives--; |
| jamesheavey | 9:f6f0f39538c7 | 70 | } |
| jamesheavey | 9:f6f0f39538c7 | 71 | |
| jamesheavey | 0:7d4d2023ed9c | 72 | Vector2D Paddle::get_pos() { |
| jamesheavey | 0:7d4d2023ed9c | 73 | Vector2D p = {_x,_y}; |
| jamesheavey | 0:7d4d2023ed9c | 74 | return p; |
| jamesheavey | 0:7d4d2023ed9c | 75 | } |