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.
Enemy/Enemy.cpp@4:3446009e2f38, 2019-05-07 (annotated)
- Committer:
- el17ttds
- Date:
- Tue May 07 19:25:20 2019 +0000
- Revision:
- 4:3446009e2f38
- Child:
- 5:096017b99011
Free movement from engine not map
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
el17ttds | 4:3446009e2f38 | 1 | #include "Enemy.h" |
el17ttds | 4:3446009e2f38 | 2 | |
el17ttds | 4:3446009e2f38 | 3 | Enemy::Enemy() { |
el17ttds | 4:3446009e2f38 | 4 | |
el17ttds | 4:3446009e2f38 | 5 | } |
el17ttds | 4:3446009e2f38 | 6 | |
el17ttds | 4:3446009e2f38 | 7 | void Enemy::init() { // Once an enemy has been created it writes nothing new until it dies |
el17ttds | 4:3446009e2f38 | 8 | } |
el17ttds | 4:3446009e2f38 | 9 | |
el17ttds | 4:3446009e2f38 | 10 | int Enemy::write(int enemy_true, int map_x1, int map_y1) { // use collision mechanic to re-draw if both enemies are in 1 location |
el17ttds | 4:3446009e2f38 | 11 | if (enemy_true == -1) { |
el17ttds | 4:3446009e2f38 | 12 | return -1; |
el17ttds | 4:3446009e2f38 | 13 | } else if (enemy_true == 0) { // create new enemy only if required |
el17ttds | 4:3446009e2f38 | 14 | random_position(); |
el17ttds | 4:3446009e2f38 | 15 | _x = map_x1 + 42 + _col * 20; // x pos of left wall + screen width / 2 + x pos of enemy |
el17ttds | 4:3446009e2f38 | 16 | _y = map_y1 + 24 + _row * 10; // y pos of left wall + screen height / 2 + y pos of enemy |
el17ttds | 4:3446009e2f38 | 17 | return 1; |
el17ttds | 4:3446009e2f38 | 18 | } else { |
el17ttds | 4:3446009e2f38 | 19 | return 1; |
el17ttds | 4:3446009e2f38 | 20 | } |
el17ttds | 4:3446009e2f38 | 21 | } |
el17ttds | 4:3446009e2f38 | 22 | |
el17ttds | 4:3446009e2f38 | 23 | void Enemy::draw(N5110 &lcd) { |
el17ttds | 4:3446009e2f38 | 24 | lcd.drawCircle( _x, _y, 10, FILL_BLACK); |
el17ttds | 4:3446009e2f38 | 25 | |
el17ttds | 4:3446009e2f38 | 26 | |
el17ttds | 4:3446009e2f38 | 27 | /* |
el17ttds | 4:3446009e2f38 | 28 | int enemy[10][20] = { |
el17ttds | 4:3446009e2f38 | 29 | {0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0}, |
el17ttds | 4:3446009e2f38 | 30 | {0,0,0,0,0,0,1,1,1,0,0,1,1,1,0,0,0,0,0,0}, |
el17ttds | 4:3446009e2f38 | 31 | {0,0,0,0,1,1,1,0,1,0,0,1,0,1,1,1,0,0,0,0}, |
el17ttds | 4:3446009e2f38 | 32 | {0,0,1,1,1,0,0,1,0,1,1,0,1,0,0,1,1,1,0,0}, |
el17ttds | 4:3446009e2f38 | 33 | {1,1,1,0,0,0,1,0,1,1,1,1,0,1,0,0,0,1,1,1}, |
el17ttds | 4:3446009e2f38 | 34 | {1,1,1,0,0,0,1,0,1,1,1,1,0,1,0,0,0,1,1,1}, |
el17ttds | 4:3446009e2f38 | 35 | {0,0,1,1,1,0,0,1,0,1,1,0,1,0,0,1,1,1,0,0}, |
el17ttds | 4:3446009e2f38 | 36 | {0,0,0,0,1,1,1,0,1,0,0,1,0,1,1,1,0,0,0,0}, |
el17ttds | 4:3446009e2f38 | 37 | {0,0,0,0,0,0,1,1,1,0,0,1,1,1,0,0,0,0,0,0}, |
el17ttds | 4:3446009e2f38 | 38 | {0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0}, |
el17ttds | 4:3446009e2f38 | 39 | }; |
el17ttds | 4:3446009e2f38 | 40 | lcd.drawSprite( _x, _y, 20, 10, (int *)enemy); |
el17ttds | 4:3446009e2f38 | 41 | */ |
el17ttds | 4:3446009e2f38 | 42 | } |
el17ttds | 4:3446009e2f38 | 43 | |
el17ttds | 4:3446009e2f38 | 44 | void Enemy::random_position() { // finds random postion for enemy out of 50 options |
el17ttds | 4:3446009e2f38 | 45 | _col = rand() % 5; |
el17ttds | 4:3446009e2f38 | 46 | _row = rand() % 10; |
el17ttds | 4:3446009e2f38 | 47 | // spacesi = _row * 5 + _col; :: int Shows space 0 - 49. Could be useful. |
el17ttds | 4:3446009e2f38 | 48 | } |