#include "library.h" struct actor { double xpos, ypos, speed; int color; string phrase; }; void print(actor a) { cout << "x=" << a.xpos << ", y=" << a.ypos << ", spd=" << a.speed << "\n"; } void draw(actor ac) { set_pen_color(ac.color); draw_point(ac.xpos, ac.ypos); } struct universe { int width, length, edge; actor boy, bull, tree; }; void print(universe uni) { cout << "field width= " << uni.width << ", length= " << uni.length << "\n"; cout << "Boy: "; print(uni.boy); cout << "Bull: "; print(uni.bull); cout << "Tree: "; print(uni.tree); cout << "\n"; } void draw_actors(universe uni) { draw(uni.bull); draw(uni.boy); draw(uni.tree); } void draw(universe uni) { set_pen_color(color::green); move_to(uni.edge, uni.edge); draw_to(uni.edge+uni.length, uni.edge); draw_to(uni.edge+uni.length, uni.edge+uni.width); draw_to(uni.edge, uni.edge+uni.width); draw_to(uni.edge, uni.edge); draw_actors(uni); } void initialise(universe & uni) { uni.bull.phrase = "Boy Eaten!"; uni.bull.color = color::red; uni.boy.phrase = "I'm safe"; uni.boy.color = color::blue; uni.tree.phrase = "Trouble"; uni.tree.color = color::green; ifstream datafile; datafile.open("setup.txt"); /* This is what's in setup.txt field 600 400 edge 50 bull 200 0 10 boy 200 400 7 tree 500 200 0 */ while (true) { string id; datafile >> id; if (datafile.fail()) break; if (id == "field") datafile >> uni.length >> uni.width; else if (id == "edge") datafile >> uni.edge; else if (id == "bull") datafile >> uni.bull.xpos >> uni.bull.ypos >> uni.bull.speed; else if (id == "boy") datafile >> uni.boy.xpos >> uni.boy.ypos >> uni.boy.speed; else if (id == "tree") datafile >> uni.tree.xpos >> uni.tree.ypos >> uni.tree.speed; else cout << "Error '" << id << "' in file\n"; } datafile.close(); cout << "finished reading\n"; uni.boy.xpos += uni.edge; uni.boy.ypos += uni.edge; uni.bull.xpos += uni.edge; uni.bull.ypos += uni.edge; uni.tree.xpos += uni.edge; uni.tree.ypos += uni.edge; } void main() { universe u; initialise(u); print(u); make_window(u.length+2*u.edge, u.width+2*u.edge); draw(u); }