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.


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.


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)
        return Math.cos(Math.toRadians(dir));
    private static double calculateY(double dir)
        return Math.sin(Math.toRadians(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.


Comments on: "Robotic Outlaws" (1)

  1. Michael Kadri said:

    It is overdue, but I think what you have here is excellent. I haven’t had the time to incorporate any of these ideas in my lessons, but I hope I can (at least some) during this summer. Please keep it up. Great stuff.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s