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.
Brick/Brick.cpp@0:92b180c8d407, 2021-01-05 (annotated)
- Committer:
- jamesheavey
- Date:
- Tue Jan 05 01:14:11 2021 +0000
- Revision:
- 0:92b180c8d407
test
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jamesheavey | 0:92b180c8d407 | 1 | #include "Brick.h" |
jamesheavey | 0:92b180c8d407 | 2 | |
jamesheavey | 0:92b180c8d407 | 3 | Brick::Brick() |
jamesheavey | 0:92b180c8d407 | 4 | { |
jamesheavey | 0:92b180c8d407 | 5 | |
jamesheavey | 0:92b180c8d407 | 6 | } |
jamesheavey | 0:92b180c8d407 | 7 | |
jamesheavey | 0:92b180c8d407 | 8 | Brick::~Brick() |
jamesheavey | 0:92b180c8d407 | 9 | { |
jamesheavey | 0:92b180c8d407 | 10 | |
jamesheavey | 0:92b180c8d407 | 11 | } |
jamesheavey | 0:92b180c8d407 | 12 | |
jamesheavey | 0:92b180c8d407 | 13 | void Brick::init(int x,int y, int lives) // initialise the Brick object at the specified x, y coordinate and with the specified number of lives |
jamesheavey | 0:92b180c8d407 | 14 | { |
jamesheavey | 0:92b180c8d407 | 15 | _x = x; // x value on screen is fixed and must be defined at initialisation |
jamesheavey | 0:92b180c8d407 | 16 | _y = y; // y value on screen is fixed and must be defined at initialisation |
jamesheavey | 0:92b180c8d407 | 17 | _height = BRICK_HEIGHT; // set brick height and width defined in Brick.h |
jamesheavey | 0:92b180c8d407 | 18 | _width = BRICK_WIDTH; |
jamesheavey | 0:92b180c8d407 | 19 | _lives = lives; // lives set at initialisation, will be decremented upon collisions |
jamesheavey | 0:92b180c8d407 | 20 | _base_lives = lives; // _base_lives variable used to reset the bricks to the number of lives specified at init |
jamesheavey | 0:92b180c8d407 | 21 | |
jamesheavey | 0:92b180c8d407 | 22 | } |
jamesheavey | 0:92b180c8d407 | 23 | |
jamesheavey | 0:92b180c8d407 | 24 | |
jamesheavey | 0:92b180c8d407 | 25 | void Brick::draw(N5110 &lcd) // draw the brick on the LCD |
jamesheavey | 0:92b180c8d407 | 26 | { |
jamesheavey | 0:92b180c8d407 | 27 | if (_x >= 0) { // only draw if on screen (more efficient?) |
jamesheavey | 0:92b180c8d407 | 28 | if (_lives <= 2) { |
jamesheavey | 0:92b180c8d407 | 29 | lcd.drawRect(_x,_y,_width,_height,FILL_TRANSPARENT); // if lives < 3 then draw a hollow brick |
jamesheavey | 0:92b180c8d407 | 30 | } else if (_lives <= 4) { |
jamesheavey | 0:92b180c8d407 | 31 | lcd.drawRect(_x,_y,_width,_height,FILL_BLACK); |
jamesheavey | 0:92b180c8d407 | 32 | lcd.drawRect(_x + 3, _y + 1,_width-6,_height-2,FILL_WHITE); // if lives < 5 then fill up the brick a little bit (less hollow) |
jamesheavey | 0:92b180c8d407 | 33 | } else { |
jamesheavey | 0:92b180c8d407 | 34 | lcd.drawRect(_x,_y,_width,_height,FILL_BLACK); // if lives >= 5 then draw a fully black brick |
jamesheavey | 0:92b180c8d407 | 35 | } |
jamesheavey | 0:92b180c8d407 | 36 | } |
jamesheavey | 0:92b180c8d407 | 37 | } |
jamesheavey | 0:92b180c8d407 | 38 | |
jamesheavey | 0:92b180c8d407 | 39 | |
jamesheavey | 0:92b180c8d407 | 40 | bool Brick::hit() // decrement the number of lives by one (triggered by collisions), returns a bool |
jamesheavey | 0:92b180c8d407 | 41 | { |
jamesheavey | 0:92b180c8d407 | 42 | _lives = _lives - 1; // decrement lives |
jamesheavey | 0:92b180c8d407 | 43 | if (_lives <= 0) { |
jamesheavey | 0:92b180c8d407 | 44 | return true; // if lives <=0 brick is destroyed and will need to be moved off screen |
jamesheavey | 0:92b180c8d407 | 45 | } else { |
jamesheavey | 0:92b180c8d407 | 46 | return false; // else the brick is still alive and can remain where it is |
jamesheavey | 0:92b180c8d407 | 47 | } |
jamesheavey | 0:92b180c8d407 | 48 | } |
jamesheavey | 0:92b180c8d407 | 49 | |
jamesheavey | 0:92b180c8d407 | 50 | |
jamesheavey | 0:92b180c8d407 | 51 | void Brick::reset_lives(int inc) // reset the lives after victory, takes inc |
jamesheavey | 0:92b180c8d407 | 52 | { |
jamesheavey | 0:92b180c8d407 | 53 | _lives = _base_lives + inc; // lives goes back to what it was at init plus 1 for every victory screen (inc = _multiplier in the breakout engine) |
jamesheavey | 0:92b180c8d407 | 54 | } |
jamesheavey | 0:92b180c8d407 | 55 | |
jamesheavey | 0:92b180c8d407 | 56 | int Brick::get_x() // return the brick's x coordinate (broken up into individual coordinates so it is easier to work with for list iterators) |
jamesheavey | 0:92b180c8d407 | 57 | { |
jamesheavey | 0:92b180c8d407 | 58 | return _x; |
jamesheavey | 0:92b180c8d407 | 59 | } |
jamesheavey | 0:92b180c8d407 | 60 | |
jamesheavey | 0:92b180c8d407 | 61 | |
jamesheavey | 0:92b180c8d407 | 62 | int Brick::get_y() // return the brick's y coordinate |
jamesheavey | 0:92b180c8d407 | 63 | { |
jamesheavey | 0:92b180c8d407 | 64 | return _y; |
jamesheavey | 0:92b180c8d407 | 65 | } |
jamesheavey | 0:92b180c8d407 | 66 | |
jamesheavey | 0:92b180c8d407 | 67 | |
jamesheavey | 0:92b180c8d407 | 68 | void Brick::set_posx(int x) // set the brick's x coordinate (used to move it off and on screen). y is unneccessary |
jamesheavey | 0:92b180c8d407 | 69 | { |
jamesheavey | 0:92b180c8d407 | 70 | _x = x; |
jamesheavey | 0:92b180c8d407 | 71 | } |