# Random horizontal or vertical character movement

I am creating a bomberman game and I arrived to adding the enemies but I dont know how to do it.

An enemy moves linearly, from top to bottom or from left to right, until it meets a barrier; he then reverses his direction.
any ideas?

I’d write an enemy class that tracks all the things you’d need to keep track of for one enemy. These things include where the enemy currently is, if he’s moving up/down or left/right, and if he’s moving in a positive or negative direction. When you trying to move the enemy, first check to see if he’s moving into a solid wall. If so, reverse his direction instead of moving him!

``````class Enemy {
float x, y, dx, dy;
int ttm;
Enemy(){
x = ???;
y = ???;
if (??? ){
dx = ???;
} else {
dy = ???:
}
ttm = int(random(4000));
}
void draw(){
move();
render();
}
move(){
ttm--;
if( ttm > 0 ) return;
ttm = int(random(4000));
if( is_wall( x + dx, y + dx ) ){
dx *= -1;
dy *= -1;
} else {
x += dx;
y += dy;
}
}
}

``````

The enemies are images loaded when the game starts, I am reading a file and one of the characters in the file indicates its an enemy, so I wanted to ask if I want to get the x and y of these enemies so I can move them in a random direction, I have to loop inside the grid I created using the file and get the x and y for the enemies then move them randomly as you suggested in your solution ?
First thing I want to do is getting the positions of the enemies then check for walls, then make them move an a direction and each time there is a wall i change the direction to the opposite If you are storing your enemies as blocks inside your level data, that could also work. When you load the level data and see that a space is an enemy, you could decide which one of the four types of enemies it is at random. The four types are “moves up”, “moves left’, moves right” and moves down" enemies.

I would give each of these different types of enemies their own value in your level data. Then to simulate enemy movement, look for any of those values in your level data, and at the cell adjacent to the enemy in the appropriate direction. Movement then involves swapping a couple of value, or perhaps changing the enemy type.

1 Like
``````class Valcom {
int xValcom, yValcom;
int PosX;
int PosY;

Valcom(){
PosX=64;
PosY=0;

}

void display() {
for (int x=0; x<level.length; x++) {
for (int y=0; y<level[x].length; y++) {
if (level[x][y]==3) {
xValcom=x;
yValcom=y;
//Afficher l'image correspondant au debut du jeu
PImage Pl = valcom.get(PosX, PosY, tileSize, tileSize);
image(Pl, yValcom*16, xValcom*16);

}
}
}
}

void move() {
try {

for (int x=0; x<level.length; x++) {
for (int y=0; y<level[x].length; y++) {
if (level[x][y]==3) {
// Prevent movement into solid walls.

if (level[y-1][x+0]==1 || level[y-1][x+0]==2 || level[y+1][x+0]==1 || level[y+1][x+0]==2){

if(random(100) > 50){

x += 1;
y += 0;
}
else {

x += -1;
y += 0;
}

}else if(level[yValcom+0][xValcom-1]==1 || level[yValcom+0][xValcom-1]==2 || level[yValcom+0][xValcom+1]==1 || level[yValcom+0][xValcom+1]==2){

if(random(100) > 50){
x += 0;
y += -1;
}
else {
x += 0;
y += 1;
}

}

}

}
}
}

catch (Exception e) {
print("");
}
}

}

``````

I tried to do this code I get the characters in the right place but the movement doesnt work.
In this function move(), I tried to see the cells next to my character to see if it is a wall or not so i can choose how to move