James Heavey
/
EL17JH
FINAL VERSION
Brick/Brick.cpp@140:d8634e76ecce, 2019-05-09 (annotated)
- Committer:
- jamesheavey
- Date:
- Thu May 09 14:36:51 2019 +0000
- Revision:
- 140:d8634e76ecce
- Parent:
- 136:04a2724f90cf
Final Submission. I have read and agreed with Statement of Academic Integrity.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jamesheavey | 1:e18615d46071 | 1 | #include "Brick.h" |
jamesheavey | 1:e18615d46071 | 2 | |
jamesheavey | 1:e18615d46071 | 3 | Brick::Brick() |
jamesheavey | 1:e18615d46071 | 4 | { |
jamesheavey | 1:e18615d46071 | 5 | |
jamesheavey | 1:e18615d46071 | 6 | } |
jamesheavey | 1:e18615d46071 | 7 | |
jamesheavey | 1:e18615d46071 | 8 | Brick::~Brick() |
jamesheavey | 1:e18615d46071 | 9 | { |
jamesheavey | 1:e18615d46071 | 10 | |
jamesheavey | 1:e18615d46071 | 11 | } |
jamesheavey | 1:e18615d46071 | 12 | |
jamesheavey | 87:b09fb252a9cb | 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 | 1:e18615d46071 | 14 | { |
jamesheavey | 87:b09fb252a9cb | 15 | _x = x; // x value on screen is fixed and must be defined at initialisation |
jamesheavey | 87:b09fb252a9cb | 16 | _y = y; // y value on screen is fixed and must be defined at initialisation |
jamesheavey | 86:01f33d94e496 | 17 | _height = BRICK_HEIGHT; // set brick height and width defined in Brick.h |
jamesheavey | 91:c01a736fb0d9 | 18 | _width = BRICK_WIDTH; |
jamesheavey | 87:b09fb252a9cb | 19 | _lives = lives; // lives set at initialisation, will be decremented upon collisions |
jamesheavey | 87:b09fb252a9cb | 20 | _base_lives = lives; // _base_lives variable used to reset the bricks to the number of lives specified at init |
jamesheavey | 1:e18615d46071 | 21 | |
jamesheavey | 1:e18615d46071 | 22 | } |
jamesheavey | 1:e18615d46071 | 23 | |
jamesheavey | 136:04a2724f90cf | 24 | |
jamesheavey | 87:b09fb252a9cb | 25 | void Brick::draw(N5110 &lcd) // draw the brick on the LCD |
jamesheavey | 91:c01a736fb0d9 | 26 | { |
jamesheavey | 87:b09fb252a9cb | 27 | if (_x >= 0) { // only draw if on screen (more efficient?) |
jamesheavey | 77:d50fb95c012f | 28 | if (_lives <= 2) { |
jamesheavey | 87:b09fb252a9cb | 29 | lcd.drawRect(_x,_y,_width,_height,FILL_TRANSPARENT); // if lives < 3 then draw a hollow brick |
jamesheavey | 91:c01a736fb0d9 | 30 | } else if (_lives <= 4) { |
jamesheavey | 77:d50fb95c012f | 31 | lcd.drawRect(_x,_y,_width,_height,FILL_BLACK); |
jamesheavey | 87:b09fb252a9cb | 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 | 91:c01a736fb0d9 | 33 | } else { |
jamesheavey | 87:b09fb252a9cb | 34 | lcd.drawRect(_x,_y,_width,_height,FILL_BLACK); // if lives >= 5 then draw a fully black brick |
jamesheavey | 77:d50fb95c012f | 35 | } |
jamesheavey | 65:ec89c0b74a71 | 36 | } |
jamesheavey | 136:04a2724f90cf | 37 | } |
jamesheavey | 91:c01a736fb0d9 | 38 | |
jamesheavey | 1:e18615d46071 | 39 | |
jamesheavey | 87:b09fb252a9cb | 40 | bool Brick::hit() // decrement the number of lives by one (triggered by collisions), returns a bool |
jamesheavey | 1:e18615d46071 | 41 | { |
jamesheavey | 87:b09fb252a9cb | 42 | _lives = _lives - 1; // decrement lives |
jamesheavey | 67:c362df66fac9 | 43 | if (_lives <= 0) { |
jamesheavey | 87:b09fb252a9cb | 44 | return true; // if lives <=0 brick is destroyed and will need to be moved off screen |
jamesheavey | 91:c01a736fb0d9 | 45 | } else { |
jamesheavey | 87:b09fb252a9cb | 46 | return false; // else the brick is still alive and can remain where it is |
jamesheavey | 32:4dba7a85dbb2 | 47 | } |
jamesheavey | 1:e18615d46071 | 48 | } |
jamesheavey | 1:e18615d46071 | 49 | |
jamesheavey | 136:04a2724f90cf | 50 | |
jamesheavey | 91:c01a736fb0d9 | 51 | void Brick::reset_lives(int inc) // reset the lives after victory, takes inc |
jamesheavey | 60:63d69184ec0a | 52 | { |
jamesheavey | 87:b09fb252a9cb | 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 | 60:63d69184ec0a | 54 | } |
jamesheavey | 1:e18615d46071 | 55 | |
jamesheavey | 91:c01a736fb0d9 | 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 | 91:c01a736fb0d9 | 57 | { |
jamesheavey | 91:c01a736fb0d9 | 58 | return _x; |
jamesheavey | 34:07ded1f83c59 | 59 | } |
jamesheavey | 34:07ded1f83c59 | 60 | |
jamesheavey | 136:04a2724f90cf | 61 | |
jamesheavey | 91:c01a736fb0d9 | 62 | int Brick::get_y() // return the brick's y coordinate |
jamesheavey | 91:c01a736fb0d9 | 63 | { |
jamesheavey | 91:c01a736fb0d9 | 64 | return _y; |
jamesheavey | 12:f714a9e8c55b | 65 | } |
jamesheavey | 12:f714a9e8c55b | 66 | |
jamesheavey | 136:04a2724f90cf | 67 | |
jamesheavey | 88:ddb9fe0db91b | 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 | 12:f714a9e8c55b | 69 | { |
jamesheavey | 34:07ded1f83c59 | 70 | _x = x; |
jamesheavey | 34:07ded1f83c59 | 71 | } |