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
- Committer:
- el17ttds
- Date:
- 2019-05-09
- Revision:
- 7:08f78909dda7
- Parent:
- 6:e8c03f264ffc
- Child:
- 8:d1c04f0e4890
File content as of revision 7:08f78909dda7:
#include "Enemy.h" Enemy::Enemy() { } void Enemy::init() { _x = -1; _y = -1; } int Enemy::write(int enemy_true, int x1, int y1) { // use collision mechanic to re-draw if both enemies are in 1 location _map_x = x1; _map_y = y1; if (enemy_true == 1) { _x = _map_x + 42 + _col * 20; // x pos of left wall + screen width / 2 + x pos of enemy _y = _map_y + 24 + _row * 10; // y pos of left wall + screen height / 2 + y pos of enemy return 1; } else if (enemy_true == 0) { // create new enemy only if required random_position(); return 1; } else { _x = -1; _y = -1; return -1; } } bool Enemy::check_collision() { if ( (_x >= 29) && (_x <= 55) && (_y >= 11) && (_y <= 37) ) { return 1; } else { return 0; } } void Enemy::random_position() { // finds random postion for enemy out of 50 options _col = rand() % 5; _row = rand() % 10; _x = _map_x + 42 + _col * 20; _y = _map_y + 24 + _row * 10; valid_position(); // spacesi = _row * 5 + _col; :: int Shows space 0 - 49. Could be useful. } void Enemy::valid_position() { // if enemy spawns too close to hero, re write enemy if ((_x >= 24) && (_x <= 50) && (_y >= 6) && (_y <= 42)) { random_position(); } } void Enemy::draw(N5110 &lcd) { lcd.drawCircle( _x, _y, 10, FILL_BLACK); /* int enemy[10][20] = { CHANGE VALID POSITION DIMENSIONS AND COLLISION MECHANIC {0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,1,1,1,0,0,1,1,1,0,0,0,0,0,0}, {0,0,0,0,1,1,1,0,1,0,0,1,0,1,1,1,0,0,0,0}, {0,0,1,1,1,0,0,1,0,1,1,0,1,0,0,1,1,1,0,0}, {1,1,1,0,0,0,1,0,1,1,1,1,0,1,0,0,0,1,1,1}, {1,1,1,0,0,0,1,0,1,1,1,1,0,1,0,0,0,1,1,1}, {0,0,1,1,1,0,0,1,0,1,1,0,1,0,0,1,1,1,0,0}, {0,0,0,0,1,1,1,0,1,0,0,1,0,1,1,1,0,0,0,0}, {0,0,0,0,0,0,1,1,1,0,0,1,1,1,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0}, }; for (_n = 0; _n < 10; _n++) { // Bitmap library encountered errors with lcd.drawSprite for (_m = 0; _m < 20; _m++) { if (enemy[_m][_n] == 1) { lcd.setPixel(_x, _y); } else { lcd.clearPixel(_x, _y); } _x++; } _y++; } */ } int Enemy::get_x() { return _x; } int Enemy::get_y() { return _y; }