Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
Enemy/Enemy.cpp@15:3dbb3f4d7ae6, 2020-05-26 (annotated)
- Committer:
- Albutt
- Date:
- Tue May 26 19:11:26 2020 +0000
- Revision:
- 15:3dbb3f4d7ae6
- Parent:
- 14:2d7e41f46879
Game Finished Copy 1
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| Albutt | 5:51fd6635141f | 1 | #include "Enemy.h" |
| Albutt | 5:51fd6635141f | 2 | #include <Bitmap.h> |
| Albutt | 6:546eba371942 | 3 | Serial pce(USBTX, USBRX); |
| Albutt | 14:2d7e41f46879 | 4 | //Constructor with seed, to spawn enemies at random places on edge of screen |
| Albutt | 6:546eba371942 | 5 | Enemy::Enemy(int seed) |
| Albutt | 5:51fd6635141f | 6 | { |
| Albutt | 6:546eba371942 | 7 | srand(seed); |
| Albutt | 14:2d7e41f46879 | 8 | _four = (rand()%4)+1; |
| Albutt | 14:2d7e41f46879 | 9 | //pce.printf("%d",_four); |
| Albutt | 14:2d7e41f46879 | 10 | if(_four == 1){ |
| Albutt | 6:546eba371942 | 11 | _x = 0; |
| Albutt | 6:546eba371942 | 12 | _y = rand()%48; |
| Albutt | 6:546eba371942 | 13 | } |
| Albutt | 14:2d7e41f46879 | 14 | else if(_four == 2){ |
| Albutt | 6:546eba371942 | 15 | _x = 84; |
| Albutt | 6:546eba371942 | 16 | _y = rand()%48; |
| Albutt | 6:546eba371942 | 17 | } |
| Albutt | 14:2d7e41f46879 | 18 | else if(_four == 3){ |
| Albutt | 6:546eba371942 | 19 | _y = 0; |
| Albutt | 6:546eba371942 | 20 | _x = rand()%84; |
| Albutt | 6:546eba371942 | 21 | } |
| Albutt | 14:2d7e41f46879 | 22 | else if(_four == 4){ |
| Albutt | 6:546eba371942 | 23 | _y = 48; |
| Albutt | 6:546eba371942 | 24 | _x = rand()%84; |
| Albutt | 6:546eba371942 | 25 | } |
| Albutt | 5:51fd6635141f | 26 | } |
| Albutt | 5:51fd6635141f | 27 | |
| Albutt | 5:51fd6635141f | 28 | Enemy::~Enemy() |
| Albutt | 5:51fd6635141f | 29 | { |
| Albutt | 5:51fd6635141f | 30 | |
| Albutt | 5:51fd6635141f | 31 | } |
| Albutt | 5:51fd6635141f | 32 | void Enemy::draw(N5110 &lcd) |
| Albutt | 5:51fd6635141f | 33 | { |
| Albutt | 5:51fd6635141f | 34 | lcd.drawRect(_x,_y,3,3,FILL_TRANSPARENT); |
| Albutt | 5:51fd6635141f | 35 | } |
| Albutt | 5:51fd6635141f | 36 | |
| Albutt | 14:2d7e41f46879 | 37 | //updates enemy with player x and y so it can walk towards the player |
| Albutt | 5:51fd6635141f | 38 | void Enemy::update(int player_x, int player_y) |
| Albutt | 5:51fd6635141f | 39 | { |
| Albutt | 5:51fd6635141f | 40 | if(_x<player_x){ |
| Albutt | 5:51fd6635141f | 41 | _x++; |
| Albutt | 5:51fd6635141f | 42 | } |
| Albutt | 5:51fd6635141f | 43 | else if(_x > player_x){ |
| Albutt | 5:51fd6635141f | 44 | _x--; |
| Albutt | 5:51fd6635141f | 45 | } |
| Albutt | 5:51fd6635141f | 46 | |
| Albutt | 5:51fd6635141f | 47 | if(_y < player_y){ |
| Albutt | 5:51fd6635141f | 48 | _y++; |
| Albutt | 5:51fd6635141f | 49 | } |
| Albutt | 5:51fd6635141f | 50 | else if (_y > player_y){ |
| Albutt | 5:51fd6635141f | 51 | _y--; |
| Albutt | 8:0c6d6ed55851 | 52 | } |
| Albutt | 10:71ced616a64f | 53 | |
| Albutt | 5:51fd6635141f | 54 | } |
| Albutt | 14:2d7e41f46879 | 55 | //accessor functions |
| Albutt | 5:51fd6635141f | 56 | int Enemy::get_x() |
| Albutt | 5:51fd6635141f | 57 | { |
| Albutt | 5:51fd6635141f | 58 | return _x; |
| Albutt | 5:51fd6635141f | 59 | |
| Albutt | 5:51fd6635141f | 60 | } |
| Albutt | 5:51fd6635141f | 61 | int Enemy::get_y() |
| Albutt | 5:51fd6635141f | 62 | { |
| Albutt | 5:51fd6635141f | 63 | return _y; |
| Albutt | 5:51fd6635141f | 64 | |
| Albutt | 5:51fd6635141f | 65 | } |
| Albutt | 14:2d7e41f46879 | 66 | //resets enemies each time it gets shot |
| Albutt | 14:2d7e41f46879 | 67 | void Enemy::reset(int seed, N5110 &lcd){ |
| Albutt | 15:3dbb3f4d7ae6 | 68 | death_animation(lcd); |
| Albutt | 8:0c6d6ed55851 | 69 | srand(seed); |
| Albutt | 14:2d7e41f46879 | 70 | _four = (rand()%4)+1; |
| Albutt | 14:2d7e41f46879 | 71 | //pce.printf("%d", _four); |
| Albutt | 14:2d7e41f46879 | 72 | if(_four == 1){ |
| Albutt | 8:0c6d6ed55851 | 73 | _x = 0; |
| Albutt | 8:0c6d6ed55851 | 74 | _y = rand()%48; |
| Albutt | 8:0c6d6ed55851 | 75 | } |
| Albutt | 14:2d7e41f46879 | 76 | else if(_four == 2){ |
| Albutt | 8:0c6d6ed55851 | 77 | _x = 84; |
| Albutt | 8:0c6d6ed55851 | 78 | _y = rand()%48; |
| Albutt | 8:0c6d6ed55851 | 79 | } |
| Albutt | 14:2d7e41f46879 | 80 | else if(_four == 3){ |
| Albutt | 8:0c6d6ed55851 | 81 | _y = 0; |
| Albutt | 8:0c6d6ed55851 | 82 | _x = rand()%84; |
| Albutt | 8:0c6d6ed55851 | 83 | } |
| Albutt | 14:2d7e41f46879 | 84 | else if(_four == 4){ |
| Albutt | 8:0c6d6ed55851 | 85 | _y = 48; |
| Albutt | 8:0c6d6ed55851 | 86 | _x = rand()%84; |
| Albutt | 8:0c6d6ed55851 | 87 | } |
| Albutt | 8:0c6d6ed55851 | 88 | } |
| Albutt | 14:2d7e41f46879 | 89 | //Eenmy Death Animation |
| Albutt | 15:3dbb3f4d7ae6 | 90 | void Enemy::death_animation(N5110 &lcd){ |
| Albutt | 14:2d7e41f46879 | 91 | int sprite_data[] = { |
| Albutt | 14:2d7e41f46879 | 92 | 1,0,1,0,1, |
| Albutt | 14:2d7e41f46879 | 93 | 0,1,1,1,0, |
| Albutt | 14:2d7e41f46879 | 94 | 1,1,0,1,1, |
| Albutt | 14:2d7e41f46879 | 95 | 0,1,1,1,0, |
| Albutt | 14:2d7e41f46879 | 96 | 1,0,1,0,1 |
| Albutt | 14:2d7e41f46879 | 97 | }; |
| Albutt | 14:2d7e41f46879 | 98 | Bitmap death(sprite_data, 5, 5); |
| Albutt | 14:2d7e41f46879 | 99 | //Render Bitmap at x & y |
| Albutt | 14:2d7e41f46879 | 100 | death.render(lcd, _x - 1, _y - 1); |
| Albutt | 14:2d7e41f46879 | 101 | lcd.refresh(); |
| Albutt | 14:2d7e41f46879 | 102 | } |