#include "library.h" const double off_x = 50, off_y = 50, timestep = 0.0033; struct actor { double xpos, ypos, speed, reach; string yell; int color, size; }; void draw(actor a) { set_pen_color(a.color); set_pen_width(a.size); draw_point(a.xpos, a.ypos); } void init_draw(double fw, double fl, actor tr, actor bu, actor bo) { set_pen_color(0.8, 1.0, 0.8); fill_rectangle(off_x, off_y, fl, fw); set_pen_color(color::dark_green); set_pen_width(50); draw_point(tr.xpos, tr.ypos); draw(tr); draw(bu); draw(bo); } void init(actor & a, double x, double y, double sp, double r, string ye, int c) { a.xpos = x; a.ypos = y; a.speed = sp; a.reach = r; a.yell = ye; a.color = c; a.size = 3; } double direction(actor from, actor to) { return atan2(to.xpos-from.xpos, to.ypos-from.ypos); } double Distance(actor a, actor b) { return sqrt((a.xpos-b.xpos)*(a.xpos-b.xpos) + (a.ypos-b.ypos)*(a.ypos-b.ypos)); } void move_towards(actor & mover, actor destiny) { double dir = direction(mover, destiny); double step_x = mover.speed * timestep * sin(dir); double step_y = mover.speed * timestep * cos(dir); move_to(mover.xpos, mover.ypos); set_pen_color(mover.color); mover.xpos += step_x; mover.ypos += step_y; draw_to(mover.xpos, mover.ypos); } bool update(actor & mover, actor dest) // return true if mover reaches destination { move_towards(mover, dest); if (Distance(mover, dest) < mover.reach) { write_string(mover.yell); return true; } return false; } void main() { double fieldw = 500, fieldl = 800; actor boy, bull, tree, dinosaur; init(tree, off_x + fieldl - 100, off_y + 150, 0.0, 0, "", color::brown); init(bull, off_x + 50, off_y + 0, 1.2, 1, "Boy Eaten!", color::red); init(boy, off_x + fieldl - 100, off_y + fieldw, 0.5, 1, "Ha ha stupid bull.", color::blue); init(dinosaur, off_x, off_y + fieldw, 1.0, 1, "Graaaaah!", color::black); tree.size = 5; make_window(fieldl+2*off_x, fieldw+2*off_y); init_draw(fieldw, fieldl, tree, bull, boy); while (true) { if (update(dinosaur, bull)) break; if (update(boy, tree)) break; if (update(bull, boy)) break; } }