Hu Dongyu
/
HuDongyu
publish my project
Attacker/Attacker.cpp@7:4b92c1ee6231, 2020-04-29 (annotated)
- Committer:
- dongyuhu
- Date:
- Wed Apr 29 10:51:02 2020 +0000
- Revision:
- 7:4b92c1ee6231
- Parent:
- 0:9e95a5ef2659
publish
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dongyuhu | 0:9e95a5ef2659 | 1 | #include "Attacker.h" |
dongyuhu | 0:9e95a5ef2659 | 2 | Attacker::Attacker() |
dongyuhu | 0:9e95a5ef2659 | 3 | { |
dongyuhu | 0:9e95a5ef2659 | 4 | |
dongyuhu | 0:9e95a5ef2659 | 5 | } |
dongyuhu | 0:9e95a5ef2659 | 6 | Attacker::~Attacker() |
dongyuhu | 0:9e95a5ef2659 | 7 | { |
dongyuhu | 0:9e95a5ef2659 | 8 | |
dongyuhu | 0:9e95a5ef2659 | 9 | } |
dongyuhu | 0:9e95a5ef2659 | 10 | |
dongyuhu | 0:9e95a5ef2659 | 11 | void Attacker::init1(int size,int speed) |
dongyuhu | 0:9e95a5ef2659 | 12 | { |
dongyuhu | 0:9e95a5ef2659 | 13 | _size = size; |
dongyuhu | 0:9e95a5ef2659 | 14 | |
dongyuhu | 0:9e95a5ef2659 | 15 | _x = 30; //attacker1 x-coordinate |
dongyuhu | 0:9e95a5ef2659 | 16 | _y = 11; // y-coordinate |
dongyuhu | 0:9e95a5ef2659 | 17 | |
dongyuhu | 0:9e95a5ef2659 | 18 | srand(time(NULL)); |
dongyuhu | 0:9e95a5ef2659 | 19 | int direction = rand() % 2; // randomise initial direction. |
dongyuhu | 0:9e95a5ef2659 | 20 | |
dongyuhu | 0:9e95a5ef2659 | 21 | if (direction == 0) { |
dongyuhu | 0:9e95a5ef2659 | 22 | _velocity.x = speed; |
dongyuhu | 0:9e95a5ef2659 | 23 | _velocity.y = speed; |
dongyuhu | 0:9e95a5ef2659 | 24 | } else { |
dongyuhu | 0:9e95a5ef2659 | 25 | _velocity.x = speed; |
dongyuhu | 0:9e95a5ef2659 | 26 | _velocity.y = speed+1; |
dongyuhu | 0:9e95a5ef2659 | 27 | } |
dongyuhu | 0:9e95a5ef2659 | 28 | } |
dongyuhu | 0:9e95a5ef2659 | 29 | |
dongyuhu | 0:9e95a5ef2659 | 30 | void Attacker::init2(int size,int speed) |
dongyuhu | 0:9e95a5ef2659 | 31 | { |
dongyuhu | 0:9e95a5ef2659 | 32 | _size = size; |
dongyuhu | 0:9e95a5ef2659 | 33 | |
dongyuhu | 0:9e95a5ef2659 | 34 | _x = 54; //attacker2 x-coordinate |
dongyuhu | 0:9e95a5ef2659 | 35 | _y = 10; // y-coordinate |
dongyuhu | 0:9e95a5ef2659 | 36 | |
dongyuhu | 0:9e95a5ef2659 | 37 | srand(time(NULL)); |
dongyuhu | 0:9e95a5ef2659 | 38 | int direction = rand() % 2; // randomise initial direction. |
dongyuhu | 0:9e95a5ef2659 | 39 | |
dongyuhu | 0:9e95a5ef2659 | 40 | if (direction == 0) { |
dongyuhu | 0:9e95a5ef2659 | 41 | _velocity.x = -speed+1; |
dongyuhu | 0:9e95a5ef2659 | 42 | _velocity.y = speed; |
dongyuhu | 0:9e95a5ef2659 | 43 | } else { |
dongyuhu | 0:9e95a5ef2659 | 44 | _velocity.x = -speed; |
dongyuhu | 0:9e95a5ef2659 | 45 | _velocity.y = speed; |
dongyuhu | 0:9e95a5ef2659 | 46 | } |
dongyuhu | 0:9e95a5ef2659 | 47 | } |
dongyuhu | 0:9e95a5ef2659 | 48 | void Attacker::init3(int size,int speed) |
dongyuhu | 0:9e95a5ef2659 | 49 | { |
dongyuhu | 0:9e95a5ef2659 | 50 | _size = size; |
dongyuhu | 0:9e95a5ef2659 | 51 | |
dongyuhu | 0:9e95a5ef2659 | 52 | _x = 10; //attacker3 x-coordinate |
dongyuhu | 0:9e95a5ef2659 | 53 | _y = 17; // y-coordinate |
dongyuhu | 0:9e95a5ef2659 | 54 | |
dongyuhu | 0:9e95a5ef2659 | 55 | srand(time(NULL)); |
dongyuhu | 0:9e95a5ef2659 | 56 | int direction = rand() % 2; // randomise initial direction. |
dongyuhu | 0:9e95a5ef2659 | 57 | |
dongyuhu | 0:9e95a5ef2659 | 58 | if (direction == 0) { |
dongyuhu | 0:9e95a5ef2659 | 59 | _velocity.x = speed; |
dongyuhu | 0:9e95a5ef2659 | 60 | _velocity.y = speed; |
dongyuhu | 0:9e95a5ef2659 | 61 | } else { |
dongyuhu | 0:9e95a5ef2659 | 62 | _velocity.x = speed+1; |
dongyuhu | 0:9e95a5ef2659 | 63 | _velocity.y = speed; |
dongyuhu | 0:9e95a5ef2659 | 64 | } |
dongyuhu | 0:9e95a5ef2659 | 65 | } |
dongyuhu | 0:9e95a5ef2659 | 66 | void Attacker::init4(int size,int speed) |
dongyuhu | 0:9e95a5ef2659 | 67 | { |
dongyuhu | 0:9e95a5ef2659 | 68 | _size = size; |
dongyuhu | 0:9e95a5ef2659 | 69 | |
dongyuhu | 0:9e95a5ef2659 | 70 | _x = 10; |
dongyuhu | 0:9e95a5ef2659 | 71 | _y = 31; |
dongyuhu | 0:9e95a5ef2659 | 72 | |
dongyuhu | 0:9e95a5ef2659 | 73 | srand(time(NULL)); |
dongyuhu | 0:9e95a5ef2659 | 74 | int direction = rand() % 2; // randomise initial direction. |
dongyuhu | 0:9e95a5ef2659 | 75 | |
dongyuhu | 0:9e95a5ef2659 | 76 | if (direction == 0) { |
dongyuhu | 0:9e95a5ef2659 | 77 | _velocity.x = speed+1; |
dongyuhu | 0:9e95a5ef2659 | 78 | _velocity.y = -speed; |
dongyuhu | 0:9e95a5ef2659 | 79 | } else { |
dongyuhu | 0:9e95a5ef2659 | 80 | _velocity.x = speed; |
dongyuhu | 0:9e95a5ef2659 | 81 | _velocity.y = -speed; |
dongyuhu | 0:9e95a5ef2659 | 82 | } |
dongyuhu | 0:9e95a5ef2659 | 83 | } |
dongyuhu | 0:9e95a5ef2659 | 84 | void Attacker::init5(int size,int speed) |
dongyuhu | 0:9e95a5ef2659 | 85 | { |
dongyuhu | 0:9e95a5ef2659 | 86 | _size = size; |
dongyuhu | 0:9e95a5ef2659 | 87 | |
dongyuhu | 0:9e95a5ef2659 | 88 | _x = 30; |
dongyuhu | 0:9e95a5ef2659 | 89 | _y = 36; |
dongyuhu | 0:9e95a5ef2659 | 90 | |
dongyuhu | 0:9e95a5ef2659 | 91 | srand(time(NULL)); |
dongyuhu | 0:9e95a5ef2659 | 92 | int direction = rand() % 2; // randomise initial direction. |
dongyuhu | 0:9e95a5ef2659 | 93 | |
dongyuhu | 0:9e95a5ef2659 | 94 | if (direction == 0) { |
dongyuhu | 0:9e95a5ef2659 | 95 | _velocity.x = speed+1; |
dongyuhu | 0:9e95a5ef2659 | 96 | _velocity.y = -speed; |
dongyuhu | 0:9e95a5ef2659 | 97 | } else { |
dongyuhu | 0:9e95a5ef2659 | 98 | _velocity.x = speed; |
dongyuhu | 0:9e95a5ef2659 | 99 | _velocity.y = -speed; |
dongyuhu | 0:9e95a5ef2659 | 100 | } |
dongyuhu | 0:9e95a5ef2659 | 101 | } |
dongyuhu | 0:9e95a5ef2659 | 102 | void Attacker::init6(int size,int speed) |
dongyuhu | 0:9e95a5ef2659 | 103 | { |
dongyuhu | 0:9e95a5ef2659 | 104 | _size = size; |
dongyuhu | 0:9e95a5ef2659 | 105 | |
dongyuhu | 0:9e95a5ef2659 | 106 | _x = 54; |
dongyuhu | 0:9e95a5ef2659 | 107 | _y = 36; |
dongyuhu | 0:9e95a5ef2659 | 108 | |
dongyuhu | 0:9e95a5ef2659 | 109 | srand(time(NULL)); |
dongyuhu | 0:9e95a5ef2659 | 110 | int direction = rand() % 2; // randomise initial direction. |
dongyuhu | 0:9e95a5ef2659 | 111 | |
dongyuhu | 0:9e95a5ef2659 | 112 | if (direction == 0) { |
dongyuhu | 0:9e95a5ef2659 | 113 | _velocity.x = -speed-1; |
dongyuhu | 0:9e95a5ef2659 | 114 | _velocity.y = -speed; |
dongyuhu | 0:9e95a5ef2659 | 115 | } else { |
dongyuhu | 0:9e95a5ef2659 | 116 | _velocity.x = -speed; |
dongyuhu | 0:9e95a5ef2659 | 117 | _velocity.y = -speed; |
dongyuhu | 0:9e95a5ef2659 | 118 | } |
dongyuhu | 0:9e95a5ef2659 | 119 | } |
dongyuhu | 0:9e95a5ef2659 | 120 | void Attacker::init7(int size,int speed) |
dongyuhu | 0:9e95a5ef2659 | 121 | { |
dongyuhu | 0:9e95a5ef2659 | 122 | _size = size; |
dongyuhu | 0:9e95a5ef2659 | 123 | |
dongyuhu | 0:9e95a5ef2659 | 124 | _x = 71; |
dongyuhu | 0:9e95a5ef2659 | 125 | _y = 31; |
dongyuhu | 0:9e95a5ef2659 | 126 | |
dongyuhu | 0:9e95a5ef2659 | 127 | srand(time(NULL)); |
dongyuhu | 0:9e95a5ef2659 | 128 | int direction = rand() % 2; // randomise initial direction. |
dongyuhu | 0:9e95a5ef2659 | 129 | |
dongyuhu | 0:9e95a5ef2659 | 130 | if (direction == 0) { |
dongyuhu | 0:9e95a5ef2659 | 131 | _velocity.x = -speed-2; |
dongyuhu | 0:9e95a5ef2659 | 132 | _velocity.y = -speed; |
dongyuhu | 0:9e95a5ef2659 | 133 | } else { |
dongyuhu | 0:9e95a5ef2659 | 134 | _velocity.x = -speed; |
dongyuhu | 0:9e95a5ef2659 | 135 | _velocity.y = -speed; |
dongyuhu | 0:9e95a5ef2659 | 136 | } |
dongyuhu | 0:9e95a5ef2659 | 137 | } |
dongyuhu | 0:9e95a5ef2659 | 138 | void Attacker::init8(int size,int speed) |
dongyuhu | 0:9e95a5ef2659 | 139 | { |
dongyuhu | 0:9e95a5ef2659 | 140 | _size = size; |
dongyuhu | 0:9e95a5ef2659 | 141 | |
dongyuhu | 0:9e95a5ef2659 | 142 | _x = 71; |
dongyuhu | 0:9e95a5ef2659 | 143 | _y = 17; |
dongyuhu | 0:9e95a5ef2659 | 144 | |
dongyuhu | 0:9e95a5ef2659 | 145 | srand(time(NULL)); |
dongyuhu | 0:9e95a5ef2659 | 146 | int direction = rand() % 2; // randomise initial direction. |
dongyuhu | 0:9e95a5ef2659 | 147 | |
dongyuhu | 0:9e95a5ef2659 | 148 | if (direction == 0) { |
dongyuhu | 0:9e95a5ef2659 | 149 | _velocity.x = -speed; |
dongyuhu | 0:9e95a5ef2659 | 150 | _velocity.y = speed; |
dongyuhu | 0:9e95a5ef2659 | 151 | } else { |
dongyuhu | 0:9e95a5ef2659 | 152 | _velocity.x = -speed-1; |
dongyuhu | 0:9e95a5ef2659 | 153 | _velocity.y = speed; |
dongyuhu | 0:9e95a5ef2659 | 154 | } |
dongyuhu | 0:9e95a5ef2659 | 155 | } |
dongyuhu | 0:9e95a5ef2659 | 156 | |
dongyuhu | 0:9e95a5ef2659 | 157 | void Attacker::draw(N5110 &lcd) |
dongyuhu | 0:9e95a5ef2659 | 158 | { |
dongyuhu | 0:9e95a5ef2659 | 159 | lcd.drawRect(_x,_y,_size,_size,FILL_BLACK); |
dongyuhu | 0:9e95a5ef2659 | 160 | } |
dongyuhu | 0:9e95a5ef2659 | 161 | |
dongyuhu | 0:9e95a5ef2659 | 162 | void Attacker::update() |
dongyuhu | 0:9e95a5ef2659 | 163 | { |
dongyuhu | 0:9e95a5ef2659 | 164 | _x += _velocity.x; |
dongyuhu | 0:9e95a5ef2659 | 165 | _y += _velocity.y; |
dongyuhu | 0:9e95a5ef2659 | 166 | ///////////////////////////////// |
dongyuhu | 0:9e95a5ef2659 | 167 | } |
dongyuhu | 0:9e95a5ef2659 | 168 | |
dongyuhu | 0:9e95a5ef2659 | 169 | void Attacker::set_velocity(Vector2D v) |
dongyuhu | 0:9e95a5ef2659 | 170 | { |
dongyuhu | 0:9e95a5ef2659 | 171 | _velocity.x = v.x; |
dongyuhu | 0:9e95a5ef2659 | 172 | _velocity.y = v.y; |
dongyuhu | 0:9e95a5ef2659 | 173 | } |
dongyuhu | 0:9e95a5ef2659 | 174 | |
dongyuhu | 0:9e95a5ef2659 | 175 | Vector2D Attacker::get_velocity() |
dongyuhu | 0:9e95a5ef2659 | 176 | { |
dongyuhu | 0:9e95a5ef2659 | 177 | Vector2D v = {_velocity.x,_velocity.y}; |
dongyuhu | 0:9e95a5ef2659 | 178 | return v; |
dongyuhu | 0:9e95a5ef2659 | 179 | } |
dongyuhu | 0:9e95a5ef2659 | 180 | |
dongyuhu | 0:9e95a5ef2659 | 181 | Vector2D Attacker::get_pos() |
dongyuhu | 0:9e95a5ef2659 | 182 | { |
dongyuhu | 0:9e95a5ef2659 | 183 | Vector2D p = {_x,_y}; |
dongyuhu | 0:9e95a5ef2659 | 184 | return p; |
dongyuhu | 0:9e95a5ef2659 | 185 | } |
dongyuhu | 0:9e95a5ef2659 | 186 | |
dongyuhu | 0:9e95a5ef2659 | 187 | void Attacker::set_pos(Vector2D p) |
dongyuhu | 0:9e95a5ef2659 | 188 | { |
dongyuhu | 0:9e95a5ef2659 | 189 | _x = p.x; |
dongyuhu | 0:9e95a5ef2659 | 190 | _y = p.y; |
dongyuhu | 0:9e95a5ef2659 | 191 | } |