Stick_Runner
Dependencies: FXOS8700CQ Gamepad N5110 SDFileSystem mbed
Fork of Stick_Runner by
Character/Character.cpp@7:887651afda26, 2017-05-04 (annotated)
- Committer:
- el15ss
- Date:
- Thu May 04 14:11:18 2017 +0000
- Revision:
- 7:887651afda26
- Parent:
- 6:bf601a65cb27
StickRunner v1.0
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
el15ss | 0:12cfe63faa6a | 1 | #include "Character.h" |
el15ss | 0:12cfe63faa6a | 2 | |
el15ss | 0:12cfe63faa6a | 3 | |
el15ss | 0:12cfe63faa6a | 4 | |
el15ss | 0:12cfe63faa6a | 5 | void Character::init() |
el15ss | 0:12cfe63faa6a | 6 | { |
el15ss | 6:bf601a65cb27 | 7 | //fprintf("in character init"); |
el15ss | 6:bf601a65cb27 | 8 | |
el15ss | 3:0c690f1c04d8 | 9 | //Initializing the X and Y co ordinates of the character |
el15ss | 3:0c690f1c04d8 | 10 | |
el15ss | 1:db9ff66f67c8 | 11 | charPosX = 42; |
el15ss | 1:db9ff66f67c8 | 12 | charPosY = 75; |
el15ss | 3:0c690f1c04d8 | 13 | |
el15ss | 3:0c690f1c04d8 | 14 | //variable to store the status of the character |
el15ss | 1:db9ff66f67c8 | 15 | charStatus = true; |
el15ss | 2:98a41609c827 | 16 | |
el15ss | 0:12cfe63faa6a | 17 | } |
el15ss | 0:12cfe63faa6a | 18 | |
el15ss | 0:12cfe63faa6a | 19 | void Character::draw(N5110 &lcd) |
el15ss | 0:12cfe63faa6a | 20 | { |
el15ss | 2:98a41609c827 | 21 | |
el15ss | 1:db9ff66f67c8 | 22 | |
el15ss | 5:1bf7c83f86cc | 23 | //Drawing the character |
el15ss | 6:bf601a65cb27 | 24 | //fprintf("character being drawn"); |
el15ss | 1:db9ff66f67c8 | 25 | lcd.setPixel(charPosX,charPosY); |
el15ss | 1:db9ff66f67c8 | 26 | lcd.setPixel(charPosX-1,charPosY); |
el15ss | 1:db9ff66f67c8 | 27 | lcd.setPixel(charPosX+1,charPosY); |
el15ss | 1:db9ff66f67c8 | 28 | lcd.setPixel(charPosX,charPosY+1); |
el15ss | 1:db9ff66f67c8 | 29 | lcd.setPixel(charPosX-1,charPosY+1); |
el15ss | 1:db9ff66f67c8 | 30 | lcd.setPixel(charPosX+1,charPosY+1); |
el15ss | 1:db9ff66f67c8 | 31 | lcd.setPixel(charPosX,charPosY+2); |
el15ss | 1:db9ff66f67c8 | 32 | lcd.setPixel(charPosX-1,charPosY+2); |
el15ss | 1:db9ff66f67c8 | 33 | lcd.setPixel(charPosX+1,charPosY+2); |
el15ss | 1:db9ff66f67c8 | 34 | lcd.setPixel(charPosX,charPosY+3); |
el15ss | 1:db9ff66f67c8 | 35 | lcd.setPixel(charPosX,charPosY+4); |
el15ss | 1:db9ff66f67c8 | 36 | lcd.setPixel(charPosX,charPosY+5); |
el15ss | 1:db9ff66f67c8 | 37 | lcd.setPixel(charPosX-1,charPosY+4); |
el15ss | 1:db9ff66f67c8 | 38 | lcd.setPixel(charPosX+1,charPosY+4); |
el15ss | 1:db9ff66f67c8 | 39 | lcd.setPixel(charPosX-2,charPosY+5); |
el15ss | 1:db9ff66f67c8 | 40 | lcd.setPixel(charPosX+2,charPosY+5); |
el15ss | 1:db9ff66f67c8 | 41 | lcd.setPixel(charPosX,charPosY+6); |
el15ss | 1:db9ff66f67c8 | 42 | lcd.setPixel(charPosX-1,charPosY+7); |
el15ss | 1:db9ff66f67c8 | 43 | lcd.setPixel(charPosX+1,charPosY+7); |
el15ss | 1:db9ff66f67c8 | 44 | lcd.setPixel(charPosX-2,charPosY+8); |
el15ss | 1:db9ff66f67c8 | 45 | lcd.setPixel(charPosX+2,charPosY+8); |
el15ss | 0:12cfe63faa6a | 46 | |
el15ss | 0:12cfe63faa6a | 47 | |
el15ss | 0:12cfe63faa6a | 48 | |
el15ss | 0:12cfe63faa6a | 49 | } |
el15ss | 0:12cfe63faa6a | 50 | |
el15ss | 0:12cfe63faa6a | 51 | |
el15ss | 5:1bf7c83f86cc | 52 | //Function to move the character using the joystick |
el15ss | 1:db9ff66f67c8 | 53 | void Character::updateCharacter(Direction d,float mag) |
el15ss | 0:12cfe63faa6a | 54 | { |
el15ss | 6:bf601a65cb27 | 55 | //fprintf("updating th character speed to make it move"); |
el15ss | 0:12cfe63faa6a | 56 | _speed = int(mag*10.0f); // scale is arbitrary, could be changed in future |
el15ss | 0:12cfe63faa6a | 57 | |
el15ss | 0:12cfe63faa6a | 58 | // update y value depending on direction of movement |
el15ss | 0:12cfe63faa6a | 59 | // North is decrement as origin is at the top-left so decreasing moves up |
el15ss | 1:db9ff66f67c8 | 60 | if (d == N) |
el15ss | 1:db9ff66f67c8 | 61 | { |
el15ss | 1:db9ff66f67c8 | 62 | charPosY-=_speed; |
el15ss | 1:db9ff66f67c8 | 63 | } |
el15ss | 1:db9ff66f67c8 | 64 | |
el15ss | 1:db9ff66f67c8 | 65 | else if (d == NW) |
el15ss | 1:db9ff66f67c8 | 66 | { |
el15ss | 1:db9ff66f67c8 | 67 | charPosX-=_speed; |
el15ss | 1:db9ff66f67c8 | 68 | charPosY-=_speed; |
el15ss | 1:db9ff66f67c8 | 69 | } |
el15ss | 1:db9ff66f67c8 | 70 | |
el15ss | 1:db9ff66f67c8 | 71 | else if (d == NE) |
el15ss | 1:db9ff66f67c8 | 72 | { |
el15ss | 1:db9ff66f67c8 | 73 | charPosX+=_speed; |
el15ss | 1:db9ff66f67c8 | 74 | charPosY-=_speed; |
el15ss | 1:db9ff66f67c8 | 75 | } |
el15ss | 1:db9ff66f67c8 | 76 | |
el15ss | 1:db9ff66f67c8 | 77 | else if (d == S) |
el15ss | 1:db9ff66f67c8 | 78 | { |
el15ss | 1:db9ff66f67c8 | 79 | charPosY+=_speed; |
el15ss | 0:12cfe63faa6a | 80 | } |
el15ss | 1:db9ff66f67c8 | 81 | |
el15ss | 1:db9ff66f67c8 | 82 | else if (d == SW) |
el15ss | 1:db9ff66f67c8 | 83 | { |
el15ss | 1:db9ff66f67c8 | 84 | charPosX-=_speed; |
el15ss | 1:db9ff66f67c8 | 85 | charPosY+=_speed; |
el15ss | 1:db9ff66f67c8 | 86 | } |
el15ss | 1:db9ff66f67c8 | 87 | |
el15ss | 1:db9ff66f67c8 | 88 | else if (d == SE) |
el15ss | 1:db9ff66f67c8 | 89 | { |
el15ss | 1:db9ff66f67c8 | 90 | charPosX+=_speed; |
el15ss | 1:db9ff66f67c8 | 91 | charPosY+=_speed; |
el15ss | 1:db9ff66f67c8 | 92 | } |
el15ss | 1:db9ff66f67c8 | 93 | |
el15ss | 1:db9ff66f67c8 | 94 | else if (d == E) |
el15ss | 1:db9ff66f67c8 | 95 | { |
el15ss | 1:db9ff66f67c8 | 96 | charPosX+=_speed; |
el15ss | 1:db9ff66f67c8 | 97 | } |
el15ss | 1:db9ff66f67c8 | 98 | |
el15ss | 1:db9ff66f67c8 | 99 | else if (d == W) |
el15ss | 1:db9ff66f67c8 | 100 | { |
el15ss | 1:db9ff66f67c8 | 101 | charPosX-=_speed; |
el15ss | 0:12cfe63faa6a | 102 | } |
el15ss | 0:12cfe63faa6a | 103 | |
el15ss | 0:12cfe63faa6a | 104 | // check the y origin to ensure that the Character doesn't go off screen |
el15ss | 0:12cfe63faa6a | 105 | |
el15ss | 1:db9ff66f67c8 | 106 | if(charPosX-2<0) |
el15ss | 0:12cfe63faa6a | 107 | { |
el15ss | 1:db9ff66f67c8 | 108 | charPosX = 2; |
el15ss | 1:db9ff66f67c8 | 109 | } |
el15ss | 1:db9ff66f67c8 | 110 | |
el15ss | 1:db9ff66f67c8 | 111 | |
el15ss | 1:db9ff66f67c8 | 112 | if(charPosX+2>WIDTH-1) |
el15ss | 0:12cfe63faa6a | 113 | { |
el15ss | 1:db9ff66f67c8 | 114 | charPosX = WIDTH -5; |
el15ss | 0:12cfe63faa6a | 115 | } |
el15ss | 1:db9ff66f67c8 | 116 | |
el15ss | 1:db9ff66f67c8 | 117 | |
el15ss | 1:db9ff66f67c8 | 118 | if (charPosY < 1) |
el15ss | 1:db9ff66f67c8 | 119 | { |
el15ss | 1:db9ff66f67c8 | 120 | charPosY = 1; |
el15ss | 1:db9ff66f67c8 | 121 | } |
el15ss | 1:db9ff66f67c8 | 122 | |
el15ss | 1:db9ff66f67c8 | 123 | if (charPosY+8 > HEIGHT - 1) |
el15ss | 1:db9ff66f67c8 | 124 | { |
el15ss | 1:db9ff66f67c8 | 125 | charPosY = HEIGHT-10; |
el15ss | 0:12cfe63faa6a | 126 | } |
el15ss | 0:12cfe63faa6a | 127 | } |
el15ss | 0:12cfe63faa6a | 128 | |
el15ss | 0:12cfe63faa6a | 129 | |
el15ss | 6:bf601a65cb27 | 130 | |
el15ss | 3:0c690f1c04d8 | 131 | //Returns the postion (x,y) of the character on the screen |
el15ss | 4:2fdafb53eac2 | 132 | Vector2D Character::getCharacterPos() |
el15ss | 4:2fdafb53eac2 | 133 | { |
el15ss | 6:bf601a65cb27 | 134 | //fprintf("in character get pos"); |
el15ss | 1:db9ff66f67c8 | 135 | Vector2D p = {charPosX,charPosY}; |
el15ss | 0:12cfe63faa6a | 136 | return p; |
el15ss | 0:12cfe63faa6a | 137 | } |
el15ss | 2:98a41609c827 | 138 | |
el15ss | 3:0c690f1c04d8 | 139 | |
el15ss | 3:0c690f1c04d8 | 140 | //Function to check if the character has been hit by a obstacle and update the status if yes |
el15ss | 2:98a41609c827 | 141 | void Character::characterStatus(Vector2D p) |
el15ss | 2:98a41609c827 | 142 | { |
el15ss | 6:bf601a65cb27 | 143 | //fprintf("IN character status"); |
el15ss | 3:0c690f1c04d8 | 144 | //Using the dimensions of the character we check if there any pixels near it |
el15ss | 2:98a41609c827 | 145 | if(((charPosX-5<p.x)&&(charPosX+5>p.x))&&((charPosY-5<p.y)&&(charPosY+5>p.y))) |
el15ss | 2:98a41609c827 | 146 | { |
el15ss | 5:1bf7c83f86cc | 147 | //Updating the status |
el15ss | 2:98a41609c827 | 148 | charStatus = false; |
el15ss | 2:98a41609c827 | 149 | } |
el15ss | 2:98a41609c827 | 150 | } |
el15ss | 2:98a41609c827 | 151 | |
el15ss | 3:0c690f1c04d8 | 152 | //Function to return the status of the character |
el15ss | 2:98a41609c827 | 153 | bool Character::getCharacterStatus() |
el15ss | 2:98a41609c827 | 154 | { |
el15ss | 6:bf601a65cb27 | 155 | |
el15ss | 6:bf601a65cb27 | 156 | //fprintf("in get character status"); |
el15ss | 6:bf601a65cb27 | 157 | |
el15ss | 6:bf601a65cb27 | 158 | //fprintf("The value of character status is (in 0/1) %d",charStatus); |
el15ss | 6:bf601a65cb27 | 159 | |
el15ss | 5:1bf7c83f86cc | 160 | //Used to determine whether the character has been and hit and charstatus returns false the game is over |
el15ss | 1:db9ff66f67c8 | 161 | return charStatus; |
el15ss | 0:12cfe63faa6a | 162 | |
el15ss | 2:98a41609c827 | 163 | } |
el15ss | 0:12cfe63faa6a | 164 | |
el15ss | 0:12cfe63faa6a | 165 |