#include "library.h" struct Actor { string name, saying; double xpos, ypos, size, speed; int colour, target; Actor() { } Actor(string n, double xp, double yp, double sz, double sp, int c, int t, string s) { name = n; xpos = xp; ypos = yp; size = sz; speed = sp; colour = c; target = t; saying = s; } }; double Distance(Actor a, Actor b) { return sqrt(pow(a.xpos-b.xpos, 2) + pow(a.ypos-b.ypos, 2)); } bool can_reach(Actor a, Actor b) { const bool r = Distance(a, b) < a.size/2 + b.size/2; if (r) cout << "distance= " << Distance(a, b) << ", sizeA=" << a.size << ", sizeB=" << b.size << "\n"; return r; } const int treeidx = 0, bullidx = 1, boyidx = 2, dinoidx = 3, nactors = 4; struct universe { int fieldw, fieldh; Actor A[nactors]; }; void report(Actor a) { cout << a.name << " is at " << a.xpos << ", " << a.ypos << "\n"; } void draw(universe u) { set_pen_color(color::green); move_to(0, 0); draw_to(u.fieldw, 0); draw_to(u.fieldw, u.fieldh); draw_to(0, u.fieldh); draw_to(0, 0); for (int i = 0; i < nactors; i += 1) { report(u.A[i]); set_pen_color(u.A[i].colour); set_pen_width(u.A[i].size); draw_point(u.A[i].xpos, u.A[i].ypos); } } void update(Actor & a, Actor t) { double dir = atan2(t.xpos-a.xpos, t.ypos-a.ypos); double dx = a.speed * sin(dir); double dy = a.speed * cos(dir); a.xpos += dx; a.ypos += dy; } bool update(universe & u) // returns true if someone wins { for (int i = 0; i < nactors; i += 1) { update(u.A[i], u.A[u.A[i].target]); if (can_reach(u.A[i], u.A[u.A[i].target])) { write_string(u.A[i].saying); return true; } } return false; } void main() { make_window(850, 650); universe U; U.fieldw = 800; U.fieldh = 600; U.A[treeidx] = Actor("tree", 600, 300, 50, 0, color::green, boyidx, "Tree wins"); U.A[bullidx] = Actor("bull", 50, 50, 7, 8, color::red, boyidx, "Boy Eaten!"); U.A[boyidx] = Actor("boy", 100, 550, 4, 6, color::blue, treeidx, "Boy Safe"); U.A[dinoidx] = Actor("dinosaur", 500, 50, 4, 6, color::brown, bullidx, "Graaaaaaaah!"); draw(U); while (true) { wait(0.1); if (update(U)) break; draw(U); } }