Online Book Reader

Home Category

HTML5 Canvas [195]

By Root 6381 0
tile types are added.

It then looks to see where to add the left and right directions. It does this based on which way will put it closer to the player.

If the horizontal direction and not the vertical direction is the shortest, it runs through the same type of logic, but this time using left and then right, then up and then down.

When this is complete, all four directions will be in the directionsToTest array.

Next, the logic finds a number between 0 and 99, and checks to see whether it is less than the chanceRandomEnemyMovement value. If it is, it will ignore the directionsToTest array and simply try to find a random direction to move in. In either case, all the directions (either in the directionsToTest array or in order up, down, left, and right) are tested until the testBounds() function returns true.

That’s all there is to this code. In Example 9-3, you will find the entire set of code for this game.

Micro Tank Maze Complete Game Code


Example 9-3. Micro Tank Maze full source code listing

CH(EX3: Micro Tank Maze Game