
Simple vertical scrolling space shooter game using navigation switch (joystick), uLCD-144-G2, and a pushbutton switch.
Dependencies: 4DGL-uLCD-SE PinDetect mbed
Display/Player.cpp@0:5c666e5cd22d, 2016-03-17 (annotated)
- Committer:
- jaspinall3
- Date:
- Thu Mar 17 20:47:11 2016 +0000
- Revision:
- 0:5c666e5cd22d
March 17, 2016 Vertical Scrolling Space Shooter
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jaspinall3 | 0:5c666e5cd22d | 1 | #include "Player.h" |
jaspinall3 | 0:5c666e5cd22d | 2 | |
jaspinall3 | 0:5c666e5cd22d | 3 | Player::Player(int x, int y, uLCD_4DGL *uLCD) { |
jaspinall3 | 0:5c666e5cd22d | 4 | _health = 4; |
jaspinall3 | 0:5c666e5cd22d | 5 | _x = x; |
jaspinall3 | 0:5c666e5cd22d | 6 | _y = y; |
jaspinall3 | 0:5c666e5cd22d | 7 | _uLCDptr = uLCD; |
jaspinall3 | 0:5c666e5cd22d | 8 | _cockpit.init(0,0,uLCD); |
jaspinall3 | 0:5c666e5cd22d | 9 | _cockpit.setRadius(6); |
jaspinall3 | 0:5c666e5cd22d | 10 | _glare.init(1,-2,uLCD); |
jaspinall3 | 0:5c666e5cd22d | 11 | _glare.setRadius(2); |
jaspinall3 | 0:5c666e5cd22d | 12 | _body.init(-4,0,uLCD); |
jaspinall3 | 0:5c666e5cd22d | 13 | _body.setDimensions(16,8); |
jaspinall3 | 0:5c666e5cd22d | 14 | _leftWing.init(-14,11,uLCD); |
jaspinall3 | 0:5c666e5cd22d | 15 | _leftWing.setDimensions(4,10); |
jaspinall3 | 0:5c666e5cd22d | 16 | _rightWing.init(4,11,uLCD); |
jaspinall3 | 0:5c666e5cd22d | 17 | _rightWing.setDimensions(4,10); |
jaspinall3 | 0:5c666e5cd22d | 18 | _leftGun.init(-11,9,uLCD); |
jaspinall3 | 0:5c666e5cd22d | 19 | _leftGun.setDimensions(2,4); |
jaspinall3 | 0:5c666e5cd22d | 20 | _rightGun.init(8,9,uLCD); |
jaspinall3 | 0:5c666e5cd22d | 21 | _rightGun.setDimensions(2,4); |
jaspinall3 | 0:5c666e5cd22d | 22 | _engine1.init(-2,16,uLCD); |
jaspinall3 | 0:5c666e5cd22d | 23 | _engine1.setDimensions(2,3); |
jaspinall3 | 0:5c666e5cd22d | 24 | _engine2.init(-3,16,uLCD); |
jaspinall3 | 0:5c666e5cd22d | 25 | _engine2.setDimensions(5,6); |
jaspinall3 | 0:5c666e5cd22d | 26 | _healthBar.init(-10, -10,uLCD); |
jaspinall3 | 0:5c666e5cd22d | 27 | _healthBar.setDimensions(1,20); |
jaspinall3 | 0:5c666e5cd22d | 28 | } |
jaspinall3 | 0:5c666e5cd22d | 29 | |
jaspinall3 | 0:5c666e5cd22d | 30 | void Player::addX(int dx) { |
jaspinall3 | 0:5c666e5cd22d | 31 | if((dx > 0) && (_x + 14 <= 128)) { |
jaspinall3 | 0:5c666e5cd22d | 32 | _x += dx; |
jaspinall3 | 0:5c666e5cd22d | 33 | } |
jaspinall3 | 0:5c666e5cd22d | 34 | if((dx < 0) && (_x - 14 >= 0)) { |
jaspinall3 | 0:5c666e5cd22d | 35 | _x += dx; |
jaspinall3 | 0:5c666e5cd22d | 36 | } |
jaspinall3 | 0:5c666e5cd22d | 37 | } |
jaspinall3 | 0:5c666e5cd22d | 38 | |
jaspinall3 | 0:5c666e5cd22d | 39 | void Player::addY(int dy) { |
jaspinall3 | 0:5c666e5cd22d | 40 | if((dy > 0) && (_y + 21 <= 128)) { |
jaspinall3 | 0:5c666e5cd22d | 41 | _y += dy; |
jaspinall3 | 0:5c666e5cd22d | 42 | } |
jaspinall3 | 0:5c666e5cd22d | 43 | if((dy < 0) && (_y - 3 >= 0)) { |
jaspinall3 | 0:5c666e5cd22d | 44 | _y += dy; |
jaspinall3 | 0:5c666e5cd22d | 45 | } |
jaspinall3 | 0:5c666e5cd22d | 46 | } |
jaspinall3 | 0:5c666e5cd22d | 47 | |
jaspinall3 | 0:5c666e5cd22d | 48 | Point Player::hitBoxStart() { |
jaspinall3 | 0:5c666e5cd22d | 49 | Point hitStart; |
jaspinall3 | 0:5c666e5cd22d | 50 | hitStart.x = _x - 3; |
jaspinall3 | 0:5c666e5cd22d | 51 | hitStart.y = _y - 3; |
jaspinall3 | 0:5c666e5cd22d | 52 | return hitStart; |
jaspinall3 | 0:5c666e5cd22d | 53 | } |
jaspinall3 | 0:5c666e5cd22d | 54 | |
jaspinall3 | 0:5c666e5cd22d | 55 | Point Player::hitBoxDim() { |
jaspinall3 | 0:5c666e5cd22d | 56 | Point hitDim; |
jaspinall3 | 0:5c666e5cd22d | 57 | hitDim.x = 8; |
jaspinall3 | 0:5c666e5cd22d | 58 | hitDim.y = 24; |
jaspinall3 | 0:5c666e5cd22d | 59 | return hitDim; |
jaspinall3 | 0:5c666e5cd22d | 60 | } |
jaspinall3 | 0:5c666e5cd22d | 61 | |
jaspinall3 | 0:5c666e5cd22d | 62 | Point Player::getLeftGunLoc() { |
jaspinall3 | 0:5c666e5cd22d | 63 | Point gunLoc; |
jaspinall3 | 0:5c666e5cd22d | 64 | gunLoc.x = _x - 10; |
jaspinall3 | 0:5c666e5cd22d | 65 | gunLoc.y = _y + 9; |
jaspinall3 | 0:5c666e5cd22d | 66 | return gunLoc; |
jaspinall3 | 0:5c666e5cd22d | 67 | } |
jaspinall3 | 0:5c666e5cd22d | 68 | |
jaspinall3 | 0:5c666e5cd22d | 69 | Point Player::getRightGunLoc() { |
jaspinall3 | 0:5c666e5cd22d | 70 | Point gunLoc; |
jaspinall3 | 0:5c666e5cd22d | 71 | gunLoc.x = _x + 9; |
jaspinall3 | 0:5c666e5cd22d | 72 | gunLoc.y = _y + 9; |
jaspinall3 | 0:5c666e5cd22d | 73 | return gunLoc; |
jaspinall3 | 0:5c666e5cd22d | 74 | } |
jaspinall3 | 0:5c666e5cd22d | 75 | |
jaspinall3 | 0:5c666e5cd22d | 76 | void Player::drawPlayer() { |
jaspinall3 | 0:5c666e5cd22d | 77 | _engine2.drawRect(_x,_y,Engine1Color); |
jaspinall3 | 0:5c666e5cd22d | 78 | _engine1.drawRect(_x,_y,Engine2Color); |
jaspinall3 | 0:5c666e5cd22d | 79 | _leftGun.drawRect(_x,_y,GunColor); |
jaspinall3 | 0:5c666e5cd22d | 80 | _rightGun.drawRect(_x,_y,GunColor); |
jaspinall3 | 0:5c666e5cd22d | 81 | _leftWing.drawRect(_x,_y,WingColor); |
jaspinall3 | 0:5c666e5cd22d | 82 | _rightWing.drawRect(_x,_y,WingColor); |
jaspinall3 | 0:5c666e5cd22d | 83 | _body.drawRect(_x,_y,BodyColor); |
jaspinall3 | 0:5c666e5cd22d | 84 | _cockpit.drawCircle(_x, _y, CockpitColor); |
jaspinall3 | 0:5c666e5cd22d | 85 | _glare.drawCircle(_x,_y,GlareColor); |
jaspinall3 | 0:5c666e5cd22d | 86 | if(_health == 4) { |
jaspinall3 | 0:5c666e5cd22d | 87 | _healthBar.setDimensions(1,20); |
jaspinall3 | 0:5c666e5cd22d | 88 | } |
jaspinall3 | 0:5c666e5cd22d | 89 | else if(_health == 3) { |
jaspinall3 | 0:5c666e5cd22d | 90 | _healthBar.setDimensions(1,15); |
jaspinall3 | 0:5c666e5cd22d | 91 | } |
jaspinall3 | 0:5c666e5cd22d | 92 | else if(_health == 2) { |
jaspinall3 | 0:5c666e5cd22d | 93 | _healthBar.setDimensions(1,10); |
jaspinall3 | 0:5c666e5cd22d | 94 | } |
jaspinall3 | 0:5c666e5cd22d | 95 | else if(_health == 1) { |
jaspinall3 | 0:5c666e5cd22d | 96 | _healthBar.setDimensions(1,5); |
jaspinall3 | 0:5c666e5cd22d | 97 | } |
jaspinall3 | 0:5c666e5cd22d | 98 | else { |
jaspinall3 | 0:5c666e5cd22d | 99 | _healthBar.setDimensions(1,1); |
jaspinall3 | 0:5c666e5cd22d | 100 | } |
jaspinall3 | 0:5c666e5cd22d | 101 | _healthBar.drawRect(_x,_y, HealthColor); |
jaspinall3 | 0:5c666e5cd22d | 102 | } |
jaspinall3 | 0:5c666e5cd22d | 103 | |
jaspinall3 | 0:5c666e5cd22d | 104 | void Player::damage(int dmg) { |
jaspinall3 | 0:5c666e5cd22d | 105 | _health -= dmg; |
jaspinall3 | 0:5c666e5cd22d | 106 | } |
jaspinall3 | 0:5c666e5cd22d | 107 | |
jaspinall3 | 0:5c666e5cd22d | 108 | int Player::getHealth() { |
jaspinall3 | 0:5c666e5cd22d | 109 | return _health; |
jaspinall3 | 0:5c666e5cd22d | 110 | } |