The game is finished
Dependencies: mbed Gamepad N5110 mbed-rtos
MiniEnemy/MiniEnemy.cpp@7:574c66ebd8b0, 2019-05-09 (annotated)
- Committer:
- RexRoshan
- Date:
- Thu May 09 09:54:50 2019 +0000
- Revision:
- 7:574c66ebd8b0
Documentation has been completed and the code has been slightly modified
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
RexRoshan | 7:574c66ebd8b0 | 1 | #include "MiniEnemy.h" |
RexRoshan | 7:574c66ebd8b0 | 2 | |
RexRoshan | 7:574c66ebd8b0 | 3 | // nothing doing in the constructor and destructor |
RexRoshan | 7:574c66ebd8b0 | 4 | MiniEnemy::MiniEnemy() |
RexRoshan | 7:574c66ebd8b0 | 5 | { |
RexRoshan | 7:574c66ebd8b0 | 6 | |
RexRoshan | 7:574c66ebd8b0 | 7 | } |
RexRoshan | 7:574c66ebd8b0 | 8 | |
RexRoshan | 7:574c66ebd8b0 | 9 | MiniEnemy::~MiniEnemy() |
RexRoshan | 7:574c66ebd8b0 | 10 | { |
RexRoshan | 7:574c66ebd8b0 | 11 | |
RexRoshan | 7:574c66ebd8b0 | 12 | } |
RexRoshan | 7:574c66ebd8b0 | 13 | |
RexRoshan | 7:574c66ebd8b0 | 14 | // first enemy sprite |
RexRoshan | 7:574c66ebd8b0 | 15 | int e1 [7][8] = { |
RexRoshan | 7:574c66ebd8b0 | 16 | |
RexRoshan | 7:574c66ebd8b0 | 17 | {0,1,1,0,0,0,0,1}, |
RexRoshan | 7:574c66ebd8b0 | 18 | {0,0,0,1,1,1,1,1}, |
RexRoshan | 7:574c66ebd8b0 | 19 | {0,0,1,0,0,1,1,0}, |
RexRoshan | 7:574c66ebd8b0 | 20 | {0,1,0,0,1,1,1,0}, |
RexRoshan | 7:574c66ebd8b0 | 21 | {0,0,1,0,0,1,1,0}, |
RexRoshan | 7:574c66ebd8b0 | 22 | {0,0,0,1,1,1,1,1}, |
RexRoshan | 7:574c66ebd8b0 | 23 | {0,1,1,0,0,0,0,1}, |
RexRoshan | 7:574c66ebd8b0 | 24 | |
RexRoshan | 7:574c66ebd8b0 | 25 | }; |
RexRoshan | 7:574c66ebd8b0 | 26 | |
RexRoshan | 7:574c66ebd8b0 | 27 | void MiniEnemy:: init() |
RexRoshan | 7:574c66ebd8b0 | 28 | { |
RexRoshan | 7:574c66ebd8b0 | 29 | _score = 0; // initial score |
RexRoshan | 7:574c66ebd8b0 | 30 | _fast = 1; // initial speed |
RexRoshan | 7:574c66ebd8b0 | 31 | _health = 0; // initial health |
RexRoshan | 7:574c66ebd8b0 | 32 | } |
RexRoshan | 7:574c66ebd8b0 | 33 | |
RexRoshan | 7:574c66ebd8b0 | 34 | Vector2D MiniEnemy::location() |
RexRoshan | 7:574c66ebd8b0 | 35 | { |
RexRoshan | 7:574c66ebd8b0 | 36 | |
RexRoshan | 7:574c66ebd8b0 | 37 | int a = rand() % 33; // randomise initial direction. |
RexRoshan | 7:574c66ebd8b0 | 38 | int b = rand() % 40; // randomise initial direction. |
RexRoshan | 7:574c66ebd8b0 | 39 | |
RexRoshan | 7:574c66ebd8b0 | 40 | _location.x = a + 42; // starts from halfway of the screen |
RexRoshan | 7:574c66ebd8b0 | 41 | _location.y = b; |
RexRoshan | 7:574c66ebd8b0 | 42 | |
RexRoshan | 7:574c66ebd8b0 | 43 | Vector2D velocity = {_location.x, _location.y}; |
RexRoshan | 7:574c66ebd8b0 | 44 | return velocity; |
RexRoshan | 7:574c66ebd8b0 | 45 | } |
RexRoshan | 7:574c66ebd8b0 | 46 | |
RexRoshan | 7:574c66ebd8b0 | 47 | void MiniEnemy::enemy(N5110 &lcd) |
RexRoshan | 7:574c66ebd8b0 | 48 | { |
RexRoshan | 7:574c66ebd8b0 | 49 | |
RexRoshan | 7:574c66ebd8b0 | 50 | // draws the first enemy |
RexRoshan | 7:574c66ebd8b0 | 51 | lcd.drawSprite(_location.x,_location.y,7,8,(int *)e1); |
RexRoshan | 7:574c66ebd8b0 | 52 | } |
RexRoshan | 7:574c66ebd8b0 | 53 | |
RexRoshan | 7:574c66ebd8b0 | 54 | void MiniEnemy::update() // moves the position of the enemy when the enemy dies |
RexRoshan | 7:574c66ebd8b0 | 55 | { |
RexRoshan | 7:574c66ebd8b0 | 56 | |
RexRoshan | 7:574c66ebd8b0 | 57 | _location.x -=_fast; // moves the y-position downwards |
RexRoshan | 7:574c66ebd8b0 | 58 | |
RexRoshan | 7:574c66ebd8b0 | 59 | |
RexRoshan | 7:574c66ebd8b0 | 60 | } |
RexRoshan | 7:574c66ebd8b0 | 61 | void MiniEnemy::add_score() |
RexRoshan | 7:574c66ebd8b0 | 62 | { |
RexRoshan | 7:574c66ebd8b0 | 63 | // increments the value of score by 1 |
RexRoshan | 7:574c66ebd8b0 | 64 | _score++; |
RexRoshan | 7:574c66ebd8b0 | 65 | } |
RexRoshan | 7:574c66ebd8b0 | 66 | int MiniEnemy::get_score() |
RexRoshan | 7:574c66ebd8b0 | 67 | { |
RexRoshan | 7:574c66ebd8b0 | 68 | // gets the value of score |
RexRoshan | 7:574c66ebd8b0 | 69 | return _score; |
RexRoshan | 7:574c66ebd8b0 | 70 | } |
RexRoshan | 7:574c66ebd8b0 | 71 | |
RexRoshan | 7:574c66ebd8b0 | 72 | void MiniEnemy::add_health() |
RexRoshan | 7:574c66ebd8b0 | 73 | { |
RexRoshan | 7:574c66ebd8b0 | 74 | // increments the value of score by 1 |
RexRoshan | 7:574c66ebd8b0 | 75 | _health++; |
RexRoshan | 7:574c66ebd8b0 | 76 | } |
RexRoshan | 7:574c66ebd8b0 | 77 | |
RexRoshan | 7:574c66ebd8b0 | 78 | void MiniEnemy::add_fast() |
RexRoshan | 7:574c66ebd8b0 | 79 | { |
RexRoshan | 7:574c66ebd8b0 | 80 | // increments the value of fast by 1 |
RexRoshan | 7:574c66ebd8b0 | 81 | if (_fast <= 7){ |
RexRoshan | 7:574c66ebd8b0 | 82 | _fast++; |
RexRoshan | 7:574c66ebd8b0 | 83 | } |
RexRoshan | 7:574c66ebd8b0 | 84 | } |
RexRoshan | 7:574c66ebd8b0 | 85 | |
RexRoshan | 7:574c66ebd8b0 | 86 | int MiniEnemy::get_fast() |
RexRoshan | 7:574c66ebd8b0 | 87 | { |
RexRoshan | 7:574c66ebd8b0 | 88 | // gets the value of score |
RexRoshan | 7:574c66ebd8b0 | 89 | return _fast; |
RexRoshan | 7:574c66ebd8b0 | 90 | } |
RexRoshan | 7:574c66ebd8b0 | 91 | |
RexRoshan | 7:574c66ebd8b0 | 92 | int MiniEnemy::set_fast() |
RexRoshan | 7:574c66ebd8b0 | 93 | { |
RexRoshan | 7:574c66ebd8b0 | 94 | // sets the fast to be zero |
RexRoshan | 7:574c66ebd8b0 | 95 | _fast = 0; |
RexRoshan | 7:574c66ebd8b0 | 96 | return _fast; |
RexRoshan | 7:574c66ebd8b0 | 97 | } |
RexRoshan | 7:574c66ebd8b0 | 98 | |
RexRoshan | 7:574c66ebd8b0 | 99 | int MiniEnemy::get_health() |
RexRoshan | 7:574c66ebd8b0 | 100 | { |
RexRoshan | 7:574c66ebd8b0 | 101 | // gets the value of score |
RexRoshan | 7:574c66ebd8b0 | 102 | return _health; |
RexRoshan | 7:574c66ebd8b0 | 103 | } |
RexRoshan | 7:574c66ebd8b0 | 104 | |
RexRoshan | 7:574c66ebd8b0 | 105 | int MiniEnemy::set_health() |
RexRoshan | 7:574c66ebd8b0 | 106 | { |
RexRoshan | 7:574c66ebd8b0 | 107 | // sets the health to be zero |
RexRoshan | 7:574c66ebd8b0 | 108 | _health = 0; |
RexRoshan | 7:574c66ebd8b0 | 109 | return _health; |
RexRoshan | 7:574c66ebd8b0 | 110 | } |
RexRoshan | 7:574c66ebd8b0 | 111 | |
RexRoshan | 7:574c66ebd8b0 | 112 | Vector2D MiniEnemy::get_enemy_pos() { |
RexRoshan | 7:574c66ebd8b0 | 113 | //gets the position of the first enemy |
RexRoshan | 7:574c66ebd8b0 | 114 | Vector2D e = {_location.x,_location.y}; |
RexRoshan | 7:574c66ebd8b0 | 115 | return e; |
RexRoshan | 7:574c66ebd8b0 | 116 | } |
RexRoshan | 7:574c66ebd8b0 | 117 | |
RexRoshan | 7:574c66ebd8b0 | 118 | void MiniEnemy::set_enemy_pos(Vector2D e) |
RexRoshan | 7:574c66ebd8b0 | 119 | { |
RexRoshan | 7:574c66ebd8b0 | 120 | //sets the position of the first enemy of stage 2 |
RexRoshan | 7:574c66ebd8b0 | 121 | _location.x = e.x; |
RexRoshan | 7:574c66ebd8b0 | 122 | _location.y = e.y; |
RexRoshan | 7:574c66ebd8b0 | 123 | } |