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 Gamepad N5110 mbed-rtos
Diff: Enemy/Enemy.cpp
- Revision:
- 0:d9cf94b41df3
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Enemy/Enemy.cpp Thu May 09 09:49:35 2019 +0000 @@ -0,0 +1,98 @@ +#include "Enemy.h" + +// nothing doing in the constructor and destructor +Enemy::Enemy() +{ + +} + +Enemy::~Enemy() +{ + +} + +// first enemy sprite +int enemy1 [7][8] = { + +{0,1,1,0,0,0,0,1}, +{0,0,0,1,1,1,1,1}, +{0,0,1,0,0,1,1,0}, +{0,1,0,0,1,1,1,0}, +{0,0,1,0,0,1,1,0}, +{0,0,0,1,1,1,1,1}, +{0,1,1,0,0,0,0,1}, + +}; + + +void Enemy::init(int a,int b) // initialising the x and y position of the enemy +{ + + _a = a; // x position of the enemy + _b = b; // y position of the enemy + _health = 0; // start health from zero + +} + +void Enemy::location() +{ + int _speed = 5; // distance the enemy moves + srand(time(NULL)); + int direction = rand() % 4; // randomise initial direction. + + // 4 possibilities. Get random modulo and set location accordingly + if (direction == 0) { // North + _a = _a; + _b += _speed; + } else if (direction == 1) { //South + _a = _a; + _b -= _speed; + } else if (direction == 2) { //West + _a -= _speed; + _b = _b; + } else { // East + _a += _speed; + _b = _b; + } +} + +void Enemy::enemy(N5110 &lcd) +{ + + // Draws the first enemy + lcd.drawSprite(_a,_b,7,8,(int *)enemy1); +} + +void Enemy::update() // Moves the position of the enemy when the enemy dies +{ + + _fast = 2.0; // Movement speed = 2 so that it is not too fast + + _b+=_speed; // moves the y-position downwards + + +} +void Enemy::add_health() +{ + // adds health + _health++; +} +int Enemy::get_health() +{ + // gets the value of health + return _health; +} + + +Vector2D Enemy::get_enemy_pos() { + //gets the position of the first enemy + Vector2D e = {_a,_b}; + return e; +} + +void Enemy::set_enemy_pos(Vector2D e) +{ + //sets the position of the first enemy of stage 2 + _a = e.x; + _b = e.y; +} \ No newline at end of file