Sign in to follow this  

making a bitmap follow a character

Recommended Posts

I have created a function for an enemy and I'm trying to make the bitmap follow my character and when they interact the character dies and the game ends.
Can someone give me heads up?
Here is the class code:
#ifndef _ENEMY_H_
#define _ENEMY_H_

#include <allegro.h>

class Enemy
int x;
int y;
BITMAP *enemy;
void blitenemy(BITMAP*buffer);




and here is the cpp code:
#include <allegro.h>
#include "Enemy.h"

x = rand() % 400 + 1;
y = rand() % 300 + 1;
enemy = load_bitmap("enemy.bmp",NULL); 


void Enemy::blitenemy(BITMAP*buffer)



Share this post

Link to post
Share on other sites

Didn't really got the question.


You want the enemy to get to the character? If that is the case, on an open world (in other words, a scenario with no obstacles) all you have to do is update the enemy position by this formula:


    angle = atan2f(playerY - monsterY, playerX - monsterX);
    nextX = monsterX + (cos(angle) * moveSpeed * dt);
    nextY = monsterY + (sin(angle)  * moveSpeed * dt);
    setMonsterPosition(nextX, nextY);


moveSpeed is the monster speed and dt is the amount of time passed since the last frame (your library probably gives you that with some function).



If the world has obstacles, then you must search for pathfinding, particularly A*.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this