## Learning and Applying Mathematics using Computing

### Robotic Outlaws

Recently, we’ve seen how to find your way around a grid, and how to draw a line between two points, choosing which pixels to illuminate. Now we’ll move on to game which will use both these techniques: creating a game where you must fend off some robots. In this game, a bunch of robots have gone haywire and are trying to squash you! As the player, you must fired at the robots to deactivate them, before they can reach you.

Our robot game is to going to feature a grid. Each square can be empty, have a wall, be occupied by a robot or by the player. We need to implement two main aspects of our game: moving, and shooting.

## Moving

Our game will have the player character, and several robot opponents. These robot opponents will have very simple logic: they will move towards the player each turn. We’ve already seen how to do pathfinding, so we can re-use this code to know which way the robots should move so that they head towards the player.

## Shooting

We want the main player to be able to shoot at the robots, anywhere on the grid, and decide whether the shot will hit, or whether there is an obstacle in the way.

So we have a source and target as two squares on the grid, and we need to work out which squares a straight-shot will pass through… this is identical to the problem of drawing a line between two points that we saw last post! We can use our code from the previous post, with a few trivial adjustments:

• Instead of drawing pixels at each location on the line, we will check for obstacles.
• When we find an obstacle, we will stop (because our shot has hit something) rather than going to the end of the line.

### Shaky Hands

At the moment, our planned game is a bit too easy: the player can aim and shoot perfectly, which means the robots don’t stand a chance. We can fix this by allowing the shot to vary a bit in its trajectory.

How can we vary the shot? Well, usually it’s a matter of shooting a few degrees to either side of the intended angle. To be able to vary the line of the shot by a few degrees, we can use our prior knowledge of polar coordinates. We can convert the line into polar coordinates, then randomly alter the angle a little bit, then turn it back and trace the path of the shot.

First, we’ll use a slight variant of our polar conversion methods from a while back:

```    private static double calculateDirection(double x, double y)
{
return Math.toDegrees(Math.atan2(y, x));
}

private static double calculateX(double dir)
{
}

private static double calculateY(double dir)
{
}
```

The change from last time is that we don’t need the magnitude aspect. As we’ve already seen, it doesn’t matter what we scale the direction vector of a line by, so we can just ignore the magnitude aspect. We use these functions to convert the shooting direction into an angle, vary the angle by -20 to +20 degrees, then convert it back to a vector for exploring the line:

```        double idealAngle = calculateDirection(x - getX(), y - getY());
double actualAngle = idealAngle + (Greenfoot.getRandomNumber(40) - 20);

p.followLine(getX(), getY(), calculateX(actualAngle), calculateY(actualAngle));
```

Now the shots vary a bit from where you shoot, meaning that (especially at range), you are likely to miss. You can go and play the complete scenario.