[Univ of Cambridge] [Dept of Engineering]
next up previous contents
Next: Arithmetic Routines Up: More C++ Previous: Exercises

Dodgems: A longer exercise

We'll now try to simulate fairground dodgems using what we know so far. Our dodgems will be circular, and our arena square. To make things more interesting we'll have 3 teams of dodgems with different sizes, speeds and colours.

Direction will be in degrees (going clockwise from 'north'). The coordinate system will have (0,0) bottom left.

We'll start by setting some constants and creating some enumerated types.

const float PI = 3.14159;
const int arena_size =400; // side of arena in pixels
const int turn_amount=20;  // how much direction can change by each time

enum Team {Green, Red, Blue};
enum Quadrant {NWQuadrant, NEQuadrant, SEQuadrant, SWQuadrant};
enum Direction {NWDirection, NEDirection, SEDirection, SWDirection};
enum Turn {Left, Straight, Right};

Now to write some little routines. None of them will be more than 10 lines or so. You may like to test them as you go along.



 

Tim Love
2001-07-05