Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <random>
- #include <time.h>
- #define UP 0
- #define LEFT 1
- #define RIGHT 2
- #define DOWN 3
- class Enemy {
- public:
- int startPosX;
- int startPosY;
- int boxWidth;
- int boxHeight;
- int boxCurrentPosX;
- int boxCurrentPosY;
- int currentPosX = 123;
- int currentPosY = 123;
- int velocity = 2;
- void move();
- void draw();
- int getRandomDirection();
- };
- extern Enemy enemy;
- int Enemy::getRandomDirection() {
- int direction = rand() % 400;
- if (direction > 0 && direction < 100){
- return UP;
- }
- else if (direction > 100 && direction < 200) {
- return LEFT;
- }
- else if (direction > 200 && direction < 300) {
- return RIGHT;
- }
- else if (direction > 300 && direction < 400) {
- return DOWN;
- }
- }
- void Enemy::move(){
- bool directionValid = false;
- do
- {
- int tmpVar = 0;
- switch (getRandomDirection())
- {
- case(UP) :
- tmpVar = boxCurrentPosY - 1;
- if (startPosY <= tmpVar)
- {
- boxCurrentPosY = tmpVar;
- currentPosY += velocity;
- directionValid = true;
- }
- break;
- case(DOWN) :
- tmpVar = boxCurrentPosY + 1;
- if ((startPosY + boxHeight) >= tmpVar){
- boxCurrentPosY = tmpVar;
- currentPosY -= velocity;
- directionValid = true;
- }
- break;
- case(LEFT) :
- tmpVar = boxCurrentPosX - 1;
- if (startPosX <= tmpVar)
- {
- boxCurrentPosX = tmpVar;
- currentPosX -= velocity;
- directionValid = true;
- }
- break;
- case(RIGHT) :
- tmpVar = boxCurrentPosX + 1;
- if ((startPosX + boxWidth) >= tmpVar)
- {
- boxCurrentPosX = tmpVar;
- currentPosX += velocity;
- directionValid = true;
- }
- break;
- }
- } while (directionValid == false);
- }
- void Enemy::draw(){
- // Draw using currentPosX, currentPosY sould be cords.
- }
- int main()
- {
- enemy.startPosX = 0;
- enemy.startPosY = 0;
- enemy.boxWidth = 5;
- enemy.boxHeight = 5;
- enemy.boxCurrentPosX = 0;
- enemy.boxCurrentPosY = 0;
- srand(time(NULL));
- enemy.move();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement