Random Movement of Enemy Characters Inside the Maze

Random Movement of Enemy Characters Inside the Maze


Part D – Video

Video Transcript

The biggest obstacle to collecting the bones  will be a small number of cars.

Cars are automatically moved into the maze at the same speed as the dog moves.

In case they touch the dog then the game stops.

If we want to express more accurately the movement of the cars, we could say that:

They move forward as they are initially placed in the maze.

If they touch the maze then they stop and turn randomly either left or right.

Let's create the first car and let's try to achieve exactly this behavior we had just described.

Initially, we need to add the "Car-Bug" sprite into the stage.

The size is too large. Let's set the starting point of the game to 30% of the original.

We want the sprite to move throughout the game. So let's use the repeat instruction "FOREVER".

The instruction of the movement is next. As we have said, it will have the same speed as the dog. So we use the 'move 5 steps' instruction.

If it encounters the maze it will interact exactly in the same way the dog interacts with it. If it touches the maze it should take 5 steps in the opposite direction. This will cause it to stop.

Then it must turn. The turn should be random either to the left or to the right.

As we have seen so far in this game when a random decision has to be taken, we will use the “pick random number' instruction from the 'operators' instruction palette.

The following code succeeds exactly in this random selection in the direction of turns.

Let me try my script now..

I see.


Let's add the code where it manages the car's contact with the dog. THe game has to end there

Very good!

It would be nice to put at the beginning of the scenario the start of the car so that it always starts from there.

Then I will copy this sprite three more times in order to have a total of 4 cars.

I must not forget to make sure that new cars start from different positions than the original one.