#include "library.h" struct actor { double xpos, ypos; double speed, reach; int hue; bool alive; string yay; }; actor create(double x, double y, double spd, double rch, int col, string s) { actor a; a.xpos = x; a.ypos = y; a.speed = spd; a.reach = rch; a.hue = col; a.yay = s; a.alive = true; return a; } bool update(actor & runner, actor target, double ti) // return = has runner reached target? { if (! runner.alive) return false; move_to(runner.xpos, runner.ypos); double a = direction_from_to_in_radians(runner.xpos, runner.ypos, target.xpos, target.ypos); double d = runner.speed * ti; runner.xpos += d * sin(a); runner.ypos -= d * cos(a); set_pen_color(runner.hue); draw_to(runner.xpos, runner.ypos); double farness = distance_to(target.xpos, target.ypos); return farness < runner.reach; } void main() { const int fieldw = 500, fieldl = 800; const double timeint = 0.001; actor boy = create(400, fieldw, 8, 2, color::blue, "Boy gets apple and is safe"); actor bull = create(fieldl, 0, 10, 4, color::red, "Moooooo! Bull eats boy!"); actor dinosaur = create(0, 0, 20, 4, color::violet, "Grrrrraaaah!"); actor tree = create(fieldl/4*3, fieldw/2, 1, 0, color::green, ""); actor anchor = create(fieldl, fieldw, 0, 0, color::green, ""); make_window(fieldl+3, fieldw+3); fill_rectangle(0, 0, fieldl, fieldw, color::green); fill_rectangle(1, 1, fieldl-2, fieldw-2, color::white); set_pen_width(3); while (true) { bool boysafe = update(boy, tree, timeint); if (boysafe) write_string(boy.yay); bool bullarrives = update(bull, boy, timeint); if (bullarrives) { if (! boysafe) { write_string(bull.yay); boy.alive = false; } break; } bool dinoarrives = update(dinosaur, bull, timeint); if (dinoarrives) { write_string(dinosaur.yay); bull.alive = false; } update(tree, anchor, timeint); } }