Отчет по практической работе Объектно-ориентированное программирование Модель полета жаворонка и пустельги. Отчет2. Отчет по практической работе Объектноориентированное программирование Модель полета жаворонка и пустельги
Скачать 3.85 Mb.
|
4 Текст программыКод класса lark ref class lark { double speedOfFly; double hungry; double fatigue;//усталость String^ obName; int obDist;//расстояние до ближайщего объекта int hig; bool dream; public: lark() { speedOfFly = 10; hungry = 100; fatigue = 100; hig = 0; obDist = 6000; obName = "Объектов нет"; dream = false; } delegate void death(Timer^ t1, PictureBox^ pbb, Button^ b1); event death^ deap; delegate void stand(PictureBox^ bi, PictureBox^ fdsz); event stand^ stup; property double p_sof { double get() { return speedOfFly; } void set(double value) { if (value >= 0 && value < 50) { speedOfFly = value; } } } property double p_hu { double get() { return hungry; } void set(double value) { if (value >= 0 && value <= 100) { hungry = value; } } } property double p_fat { double get() { return fatigue; } void set(double value) { if (value >= 0 && value <= 100) { fatigue = value; } } } property int p_dis { int get() { return obDist; } void set(int value) { if (value >= -1 && value <= 6001) { obDist = value; } } } property String^ p_obn { String^ get() { return obName; } void set(String^ value) { obName = value; } } void fly(PictureBox^ pbb, PictureBox^ pbf, PictureBox^ pbTr, Label^ lsof, Label^ lhun, Label^ lfat, Label^ lname, Label^ ldob, Timer^ t, Button^ b) { if (!dream) { if (speedOfFly < 10) { speedOfFly = 10; } fatigue -= 0.1; hungry -= 0.07; speedOfFly += 0.05; int f = Convert::ToInt32(fatigue); int h = Convert::ToInt32(hungry); int sp = Convert::ToInt16(speedOfFly); lhun->Text = Convert::ToString(h); lfat->Text = Convert::ToString(f); lsof->Text = Convert::ToString(sp); lname->Text = obName; if (obDist < 6000) { ldob->Text = Convert::ToString(obDist-200); } else { ldob->Text = "-"; } pbb->Location = System::Drawing::Point(pbb->Location.X + Convert::ToInt32(speedOfFly), pbb->Location.Y + hig); pbf->Location = System::Drawing::Point(pbf->Location.X - Convert::ToInt32(speedOfFly), 0); if (pbf->Location.X <= -3830) { pbf->Location = System::Drawing::Point(0, 0); pbb->Location = System::Drawing::Point(8, pbb->Location.Y); } if ((fatigue <= 0) || (hungry <= 0)) { speedOfFly = 10; deap(t, pbb, b); } } else { String^ sip = "\\resu\\gavaranok.gif"; slept(pbb,pbTr,lsof,lhun,lfat,lname,ldob,sip); } } void slept(PictureBox^ pbb, PictureBox^ pbTr, Label^ lsof, Label^ lhun, Label^ lfat, Label^ lname, Label^ ldob, String^ dtr) { fatigue += 1; hungry -= 0.07; speedOfFly += 0.05; int f = Convert::ToInt32(fatigue); int h = Convert::ToInt32(hungry); int sp = Convert::ToInt16(speedOfFly); lhun->Text = Convert::ToString(h); lfat->Text = Convert::ToString(f); lsof->Text = Convert::ToString(sp); lname->Text = obName; if (obDist < 6000) { ldob->Text = Convert::ToString(obDist - 200); } else if (obDist < 0) { ldob->Text = "0"; } else { ldob->Text = "-"; } if (fatigue >= 99) { dream = false; pbb->Image = Image::FromFile(Application::StartupPath + dtr); stup(pbTr, pbb); obDist = 6000; obName = "объектов нет"; } } void deshi(Timer^ t1, PictureBox^ pbb, Button^ b1) { pbb->Image = Image::FromFile(Application::StartupPath + "\\resu\\gavoronokd.png"); pbb->Location = System::Drawing::Point(pbb->Location.X, 750); t1->Enabled = false; b1->Visible = true; b1->Enabled = true; } void seeOb(PictureBox^ pb, PictureBox^ pbb) { int buff = abs(pbb->Location.X - pb->Location.X); if (buff < obDist) { obDist = buff; if (pb->Size.Width == 64) { hig = Convert::ToInt32(round(((pb->Location.Y - pbb->Location.Y) - 96) / ((abs(pb->Location.X - pbb->Location.X) - 256) / speedOfFly))); obName = "муха"; } else { hig = Convert::ToInt32((240 - pbb->Location.Y) / ((abs(pb->Location.X - pbb->Location.X) - 256) / speedOfFly)); obName = "дерево"; } } } void feed() { if (hungry > 99) { hungry = 100; } else { hungry += 0.09; } hig = 0; obDist = 6000; obName = "Нет объектов"; } void sitt(PictureBox^ pb, PictureBox^ pbb) { speedOfFly = 0; if (!dream) { pb->Image = Image::FromFile(Application::StartupPath + "\\resu\\treeG.png"); } dream = true; pbb->Visible = false; } }; Код класса kestrel ref class kastrel : public lark { double speedOfFly; double hungry; double fatigue;//усталость String^ obName; int obDist;//расстояние до ближайщего объекта bool dream; int hig; public: kastrel() { speedOfFly = 10; hungry = 100; fatigue = 100; obDist = 600; obName = "Объектов нет"; dream = false; hig = 0; obDist = 6000; } property double p_sof { double get() { return speedOfFly; } void set(double value) { if (value >= 0 && value < 60) { speedOfFly = value; } } } property double p_hu { double get() { return hungry; } void set(double value) { if (value >= 0 && value <= 100) { hungry = value; } } } property double p_fat { double get() { return fatigue; } void set(double value) { if (value >= 0 && value <= 100) { fatigue = value; } } } property int p_dis { int get() { return obDist; } void set(int value) { if (value >= -1 && value <= 1600) { obDist = value; } } } property String^ p_obn { String^ get() { return obName; } void set(String^ value) { obName = value; } } void fly(PictureBox^ pbb, PictureBox^ pbf, PictureBox^ pbTr, Label^ lhun, Label^ lfat, Label^ lsof, Label^ lname, Label^ ldob, Timer^ t, Button^ b) { if (!dream) { if (speedOfFly < 10) { speedOfFly = 10; } fatigue -= 0.1; hungry -= 0.07; speedOfFly += 0.05; int f = Convert::ToInt32(fatigue); int h = Convert::ToInt32(hungry); int sp = Convert::ToInt16(speedOfFly); lhun->Text = Convert::ToString(h); lfat->Text = Convert::ToString(f); lsof->Text = Convert::ToString(sp); lname->Text = obName; if (obDist < 6000) { ldob->Text = Convert::ToString(obDist - 200); } else { ldob->Text = "-"; } pbb->Location = System::Drawing::Point(pbb->Location.X + Convert::ToInt32(speedOfFly), pbb->Location.Y + hig); pbf->Location = System::Drawing::Point(pbf->Location.X - Convert::ToInt32(speedOfFly), 0); if (pbf->Location.X <= -3830) { pbf->Location = System::Drawing::Point(0, 0); pbb->Location = System::Drawing::Point(8, pbb->Location.Y); } if ((fatigue <= 0) || (hungry <= 0)) { speedOfFly = 10; deap(t, pbb, b); } } else { String^ sip = "\\resu\\pustelga.gif"; lark::slept(pbb, pbTr, lsof, lhun, lfat, lname, ldob, sip); } } void feed() { if (hungry > 99) { hungry = 100; } else { hungry += 0.09; } hig = 0; obDist = 6000; obName = "Нет объектов"; } void deshi(Timer^ t1, PictureBox^ pbb, Button^ b1) { pbb->Image = Image::FromFile(Application::StartupPath + "\\resu\\pustelgad.png"); t1->Enabled = false; pbb->Location = System::Drawing::Point(pbb->Location.X, 750); b1->Visible = true; b1->Enabled = true; } void appearM(PictureBox^ pbmo1) { pbmo1->Visible = true; pbmo1->Location = System::Drawing::Point(pbmo1->Location.X, 900); if (speedOfFly > 8) { speedOfFly -= 1; } } void sitt(PictureBox^ pb, PictureBox^ pbb) { speedOfFly = 0; if (!dream) { pb->Image = Image::FromFile(Application::StartupPath + "\\resu\\treeP.png"); } dream = true; obDist = 6000; pbb->Visible = false; } void seeOb(PictureBox^ pb, PictureBox^ pbb) { int buff = abs(pbb->Location.X - pb->Location.X); if (buff < obDist) { obDist = buff; if (pb->Size.Width == 60) { hig = Convert::ToInt32(round((680 – pbb->Location.Y)/((pb->Location.X – pbb->Location.X - 256)/(speedOfFly+3)+1))); pbb->Image = Image::FromFile(Application::StartupPath + "\\resu\\pustelgaPick.png"); obName = "мышь"; } else if (pb->Size.Width == 64) { hig = Convert::ToInt32(round(((pb->Location.Y - pbb->Location.Y) - 96) / ((abs(pb->Location.X - pbb->Location.X) - 256) / speedOfFly))); obName = "муха"; } else { hig = Convert::ToInt32((240 - pbb->Location.Y) / ((abs(pb->Location.X - pbb->Location.X) - 256) / speedOfFly)); obName = "дерево"; } } } void lost(PictureBox^ pbb, PictureBox^ pbm, PictureBox^ pbn) { hig = 0; pbb->Image = Image::FromFile(Application::StartupPath + "\\resu\\pustelga.gif"); pbm->Visible = false; pbm->Location = System::Drawing::Point(pbb->Location.X + 1930, pbm->Location.Y); obName = "объекта нет"; obDist = 6000; } void feedM(PictureBox^ pbb) { if (hungry > 99) { hungry = 100; } else { hungry += 0.09; } hig = 0; obDist = 6000; obName = "Нет объектов"; pbb->Image = Image::FromFile(Application::StartupPath + "\\resu\\pustelga.gif"); } }; Код классов flBl, mouse, tree ref class flBl { double speed; bool vis; public: delegate void visa(PictureBox^ p, PictureBox^ pbbb); event visa^ see; delegate void eatenm(); event eatenm^ eatm; flBl(double SpeedB) { speed = SpeedB; vis = false; } void fly(PictureBox^ pb, PictureBox^ pbb, PictureBox^ pbf, double s){ if (!vis) { pb->Location = System::Drawing::Point(pbb->Location.X + 1928, pb->Location.Y); } else { if ((pb->Location.X >= 3830)&&(pbf->Location.X <= -3829 + s)) { int yu = pb->Location.X -3830; pb->Location = System::Drawing::Point(yu, pb->Location.Y); } if (abs(pb->Location.X - pbb->Location.X) < 1500) { see(pb,pbb); if (abs(pb->Location.X - pbb->Location.X) < 256){ pb->Location = System::Drawing::Point(pbb->Location.X + 1928, pb->Location.Y); pb->Visible = false; vis = false; eatm(); } } } } void appear(PictureBox^ pb) { vis = true; pb->Visible = true; int hig = 500 * rand() / RAND_MAX + 200; pb->Location = System::Drawing::Point(pb->Location.X, hig); speed = 0; } }; ref class mouse { double speed; int dist; bool vis; public: delegate void visa(PictureBox^ p, PictureBox^ pb); event visa^ see; delegate void eatenm(PictureBox^ pbb); event eatenm^ eatm; delegate void mlose(PictureBox^ pbb, PictureBox^ pbm, PictureBox^ pbn); event mlose^ los; mouse(double SpeedB) { speed = SpeedB; vis = false; } void run(PictureBox^ pbb, PictureBox^ pb, PictureBox^ pbn, PictureBox^ pbf, double s) { if (!vis) { pb->Location = System::Drawing::Point(pbb->Location.X + 1928, pb->Location.Y); pbn->Location = System::Drawing::Point(pbb->Location.X + 1928, pbn->Location.Y); }else{ pb->Location = System::Drawing::Point(pb->Location.X + 3, pb->Location.Y); if ((pb->Location.X >= 3830) && (pbf->Location.X <= -3829 + s)) { int yu = pb->Location.X - 3830; pb->Location = System::Drawing::Point(yu, pb->Location.Y); } if ((pbn->Location.X >= 3830) && (pbf->Location.X <= -3829 + s)) { int yu = pbn->Location.X - 3830; pbn->Location = System::Drawing::Point(yu, pbn->Location.Y); } if ((abs(pb->Location.X - pbn->Location.X) > 60)) { if (abs(pb->Location.X - pbb->Location.X) < 800) { see(pb,pbb); if (abs(pb->Location.X - pbb->Location.X) < 256) { eatm(pbb); pb->Location = System::Drawing::Point(pbb->Location.X + 1928, pb->Location.Y); pb->Visible = false; pbn->Location = System::Drawing::Point(pbb->Location.X + 1928, pbn->Location.Y); pbn->Visible = false; vis = false; } } } else { pb->Visible = false; pbn->Visible = false; pb->Location = System::Drawing::Point(pbb->Location.X + 1928, pb->Location.Y); pbn->Location = System::Drawing::Point(pbb->Location.X + 1928, pbn->Location.Y); los(pbb, pb, pbn); vis = false; } } } void appear(PictureBox^ pb, PictureBox^ pbn) { pb->Visible = true; pbn->Visible = true; dist = 900 * rand() / RAND_MAX + 60; pbn->Location = System::Drawing::Point(pb->Location.X + dist, pbn->Location.Y); vis = true; } void eaten(PictureBox^ pb, PictureBox^ pbn, PictureBox^ pbb) { pb->Location = System::Drawing::Point(pbb->Location.X + 1928, pb->Location.Y); pbn->Location = System::Drawing::Point(pbb->Location.X + 1928, pbn->Location.Y); pb->Visible = false; pbn->Visible = false; } }; ref class tree { int speed; bool vis; public: delegate void seen(PictureBox^ tr, PictureBox^ pict); event seen^ sow; delegate void sitting(PictureBox^ pb, PictureBox^ pbb); event sitting^ sitdown; tree(double SpeedB) { speed = SpeedB; vis = false; } void go(PictureBox^ pb, PictureBox^ pbb,PictureBox^ pbf, double s) { if (!vis) { pb->Location = System::Drawing::Point(pbb->Location.X + 1928, pb->Location.Y); } else { if ((pb->Location.X >= 3830) && (pbf->Location.X <= -3829 + s)) { int yu = pb->Location.X - 3830; pb->Location = System::Drawing::Point(yu, pb->Location.Y); } if (abs(pb->Location.X - pbb->Location.X) < 1500) { sow(pb, pbb); if (abs(pb->Location.X - pbb->Location.X) < s+1) { sitdown(pb,pbb); } } } } void appe(PictureBox^ pb) { pb->Visible = true; vis = true; } void disappe(PictureBox^ pb, PictureBox^ pbb) { pb->Visible = false; pb->Location = System::Drawing::Point(pbb->Location.X + 1928, pb->Location.Y); pb->Image = Image::FromFile(Application::StartupPath + "\\resu\\tree.png"); pbb->Visible = true; vis = false; } }; Код «MyForm.h» public: lark^ lark1 = gcnew lark(); kastrel^ kastrel1 = gcnew kastrel(); flBl^ fly1 = gcnew flBl(lark1->p_sof); flBl^ fly2 = gcnew flBl(kastrel1->p_sof); flBl^ fly3 = gcnew flBl(kastrel1->p_sof); flBl^ fly4 = gcnew flBl(kastrel1->p_sof); flBl^ fly5 = gcnew flBl(lark1->p_sof); flBl^ fly6 = gcnew flBl(lark1->p_sof); tree^ tree1 = gcnew tree(lark1->p_sof); tree^ tree2 = gcnew tree(kastrel1->p_sof); mouse^ miki = gcnew mouse(kastrel1->p_sof); private: System::Void MyForm_Load(System::Object^ sender, System::EventArgs^ e) { timer1->Enabled = true; label1->Text = Convert::ToString(this->Size.Height); pbf1->Controls->Add(pbb1); pbf2->Controls->Add(pbb2); srand(time(NULL)); timer2->Enabled = true; pbf2->Controls->Add(pbmo); pbf1->Controls->Add(pbTree1); pbf2->Controls->Add(pbTree2); pbf1->Controls->Add(pictureBox1); pbf2->Controls->Add(pictureBox2); pbf1->Controls->Add(pictureBox5); pbf1->Controls->Add(pictureBox6); pbf2->Controls->Add(pictureBox3); pbf2->Controls->Add(pictureBox4); pbf2->Controls->Add(pictureBox7); } private: System::Void timer1_Tick(System::Object^ sender, System::EventArgs^ e) { lark1->deap += gcnew lark::death(lark1, &lark::deshi); fly1->see += gcnew flBl::visa(lark1, &lark::seeOb); fly1->eatm += gcnew flBl::eatenm(lark1, &lark::feed); fly5->see += gcnew flBl::visa(lark1, &lark::seeOb); fly5->eatm += gcnew flBl::eatenm(lark1, &lark::feed); fly6->see += gcnew flBl::visa(lark1, &lark::seeOb); fly6->eatm += gcnew flBl::eatenm(lark1, &lark::feed); tree1->sow += gcnew tree::seen(lark1, &lark::seeOb); tree1->sitdown += gcnew tree::sitting(lark1, &lark::sitt); lark1->stup += gcnew lark::stand(tree1, &tree::disappe); lark1->fly(pbb1,pbf1,pbTree1, label11, lh1, lf1,label14, label16, timer1, button1); tree1->go(pbTree1, pbb1, pbf1, lark1->p_sof); fly1->fly(pictureBox1, pbb1, pbf1,lark1->p_sof); fly5->fly( pictureBox5, pbb1, pbf1, lark1->p_sof); fly6->fly(pictureBox6, pbb1, pbf1, lark1->p_sof); } private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) { timer1->Enabled = true; button1->Visible = false; button1->Enabled = false; lark1->p_sof = 10; lark1->p_hu = 100; lark1->p_fat = 100; pbb1->Visible = true; lf1->Text = Convert::ToString(lark1->p_fat); lh1->Text = Convert::ToString(lark1->p_hu); label11->Text = Convert::ToString(lark1->p_sof); pbb1->Image = Image::FromFile(Application::StartupPath + "\\resu\\gavaranok.gif"); pbb1->Location = System::Drawing::Point(pbb1->Location.X, 600); } private: System::Void timer2_Tick(System::Object^ sender, System::EventArgs^ e) { kastrel1->deap += gcnew kastrel::death(kastrel1, &kastrel::deshi); fly2->see += gcnew flBl::visa(kastrel1, &kastrel::seeOb); fly2->eatm += gcnew flBl::eatenm(kastrel1, &kastrel::feed); fly3->see += gcnew flBl::visa(kastrel1, &kastrel::seeOb); fly3->eatm += gcnew flBl::eatenm(kastrel1, &kastrel::feed); fly4->see += gcnew flBl::visa(kastrel1, &kastrel::seeOb); fly4->eatm += gcnew flBl::eatenm(kastrel1, &kastrel::feed); tree2->sow += gcnew tree::seen(kastrel1, &kastrel::seeOb); tree2->sitdown += gcnew tree::sitting(kastrel1, &kastrel::sitt); kastrel1->stup += gcnew kastrel::stand(tree2, &tree::disappe); miki->see += gcnew mouse::visa(kastrel1, &kastrel::seeOb); miki->los += gcnew mouse::mlose(kastrel1, &kastrel::lost); miki->eatm += gcnew mouse::eatenm(kastrel1, &kastrel::feedM); kastrel1->fly(pbb2, pbf2, pbTree2, label13, label7, label8, label17, label4, timer2, button2); tree2->go(pbTree2, pbb2, pbf2, kastrel1->p_sof); fly2->fly(pictureBox2, pbb2, pbf2, kastrel1->p_sof); fly3->fly(pictureBox3, pbb2, pbf2, kastrel1->p_sof); fly4->fly(pictureBox4, pbb2, pbf2, kastrel1->p_sof); miki->run(pbb2, pbmo, pictureBox7, pbf2, kastrel1->p_sof); } private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e) { button2->Visible = false; button2->Enabled = false; kastrel1->p_sof = 10; kastrel1->p_hu = 100; kastrel1->p_fat = 100; label7->Text = Convert::ToString(kastrel1->p_fat); label8->Text = Convert::ToString(kastrel1->p_hu); label13->Text = Convert::ToString(kastrel1->p_sof); pbb2->Image = Image::FromFile(Application::StartupPath + "\\resu\\pustelga.gif"); pbb2->Location = System::Drawing::Point(pbb2->Location.X, 600); timer2->Enabled = true; } private: System::Void bMuA_Click(System::Object^ sender, System::EventArgs^ e) { if (pictureBox1->Visible == false) { fly1->appear(pictureBox1); } else if (pictureBox5->Visible == false) { fly5->appear(pictureBox5); } else if (pictureBox6->Visible == false) { fly6->appear(pictureBox6); } else { MessageBox::Show("одновременно на одном экране может находиться не более трех мух"); } } private: System::Void bMuB_Click(System::Object^ sender, System::EventArgs^ e) { if (pictureBox2->Visible == false) { fly2->appear(pictureBox2); } else if (pictureBox3->Visible == false) { fly3->appear(pictureBox3); } else if (pictureBox4->Visible == false) { fly4->appear(pictureBox4); } else { MessageBox::Show("одновременно на одном экране может находиться не более трех мух"); } } private: System::Void bTree1_Click(System::Object^ sender, System::EventArgs^ e) { if (pbTree1->Visible == false) { tree1->appe(pbTree1); } } private: System::Void bTree2_Click(System::Object^ sender, System::EventArgs^ e) { if (pbTree2->Visible == false) { tree2->appe(pbTree2); } } private: System::Void bMo_Click(System::Object^ sender, System::EventArgs^ e) { miki->appear(pbmo, pictureBox7); } }; 5 Результаты тестирования программыРеализация методов fly() и slept() для экземпляра класса lark и kestrel представлена на рисунках 39-42. Рисунок 39 – Использование метода fly() экземпляром класса lark() Рисунок 40 – Использование метода fly() экземпляром класса kastrel() Рисунок 41 – Использование метода slept() экземпляром класса lark() Рисунок 42 – Использование метода slept() экземпляром класса kastrel() Проверим работу генерации мух, реакцию птиц на них. Рисунки Рисунок 43 – Появление мухи Рисунок 44 – Отслеживание мухи Рисунок 45 – Птица меняет высоту на высоту ближайшей к ней мухи Проверим работу методов появления и отслеживания мыши Рисунок 46 – мышь появилась на случайном расстоянии от норы Рисунок 47 – пустельга приблизилась к мыши на расстояние 800 Проверяем работу методов дерева и реакцию птицы на него. Рисунок 48 – появление дерева Рисунок 49 – дерево приблизилось ближе чем на 1500 к птице Проверяем работу функции death класса lark. Рисунок 50 – птица погибла ВыводыВ результате работы были созданы один основной класс lark и производный его класс kastrel. У каждого из них есть минимум 4 свойства, 2 метода, 2 события и 1 конструктор, что удовлетворяет требования задания. |