- •«Московский технический университет связи и информатики»
- •Курсовая работа
- •1. Постановка задачи 2
- •Постановка задачи
- •Логическая структура программы
- •2.1 Класс рыбы Fish
- •2.3 Класс птицы Bird
- •2.2 Класс рыбака на пирсе Fisher
- •2.4 Класс рыбака на лодке Fisherboat
- •2.5 Класс дождя Rain
- •2.6 Класс внешнего мира GameEnvironment
- •2.7 Главная часть приложения - класс «MyForm»
- •3.7 Класс MyForm
- •Текст программы
- •4.1 Класс Fisher
- •4.2 Класс Fisherboat
- •4.3 Класс Fish
- •4.4 Класс Bird
- •4.5 Класс Rain
- •4.6 Класс GameEnvironment
- •4.7 Класс MyForm
- •Результаты тестирования программы
4.7 Класс MyForm
#pragma once
#include "Actions.cpp"
#include "Definer.h"
namespace Fishing {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Collections::Generic;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
/// <summary>
/// Сводка для MyForm
/// </summary>
public ref class MyForm : public System::Windows::Forms::Form
{
Graphics^ graf;
Bitmap^ myBitmap;
Bitmap^ bg;
private: GameEnvironment^ env;
private: System::Windows::Forms::Timer^ timer1;
private: System::Windows::Forms::TableLayoutPanel^ tableLayoutPanel4;
private: System::Windows::Forms::Label^ lbl_ptrName;
private: System::Windows::Forms::PictureBox^ picBox_ptrTarg;
private: System::Windows::Forms::TableLayoutPanel^ tableLayoutPanel7;
private: System::Windows::Forms::TableLayoutPanel^ tableLayoutPanel6;
private: System::Windows::Forms::Label^ lbl_time;
private: System::Windows::Forms::Label^ lbl_timeStr;
private: System::Windows::Forms::TableLayoutPanel^ tableLayoutPanel5;
private: System::Windows::Forms::Label^ lbl_fish;
private: System::Windows::Forms::Label^ lbl_fishStr;
private: System::Windows::Forms::TableLayoutPanel^ tableLayoutPanel9;
private: System::Windows::Forms::Button^ btn_delFishboat;
private: System::Windows::Forms::Button^ btn_newFishboat;
private: System::Windows::Forms::TableLayoutPanel^ tableLayoutPanel8;
private: System::Windows::Forms::Button^ btn_changePos;
private: System::Windows::Forms::Button^ btn_changePtr;
private: System::Windows::Forms::TableLayoutPanel^ tableLayoutPanel10;
private: System::Windows::Forms::Button^ btn_restart;
private: System::Windows::Forms::Button^ btn_rainSwitch;
private: System::Windows::Forms::Label^ lbl_event;
public:
MyForm(void)
{
InitializeComponent();
myBitmap = gcnew Bitmap(SCREEN_WIDTH, SCREEN_HEIGHT);
bg = gcnew Bitmap(SCREEN_WIDTH, SCREEN_HEIGHT);
env = gcnew GameEnvironment();
env->EventFishCatched += gcnew GameEnvironment::Handler(this, &MyForm::OnEventCatchFish);
//
//TODO: добавьте код конструктора
//
}
protected:
/// <summary>
/// Освободить все используемые ресурсы.
~MyForm()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::TableLayoutPanel^ tableLayoutPanel1;
protected:
private: System::Windows::Forms::TableLayoutPanel^ tableLayoutPanel2;
private: System::Windows::Forms::Label^ lbl_formName;
private: System::Windows::Forms::Button^ btn_exit;
private: System::Windows::Forms::Button^ btn_hide;
private: System::Windows::Forms::TableLayoutPanel^ tableLayoutPanel3;
private: System::Windows::Forms::Button^ btn_mltp;
private: System::Windows::Forms::PictureBox^ pictureBox1;
private: System::ComponentModel::IContainer^ components; Void Void
DrawFisher(Drawing::Graphics^ _graf) {
for (int i = 0; i < env->Fishers->Count; i++) {
Bitmap^ bm = gcnew Bitmap(String::Format("Pictures\\fisher_{0}.png", env->Fishers[i]->state));
graf->DrawImage(bm, env->Fishers[i]->xPos + 24, env->Fishers[i]->yPos - 36, 48, 4 8); // 42 35
delete bm;
if (env->Fishers[i]->yPos > PIRSE_Y) {
bm = gcnew Bitmap("Pictures\\boat.png");
graf->DrawImage(bm, env->Fishers[i]->xPos, env->Fishers[i]->yPos, 74, 18);
delete bm;
}
if (env->Fishers[i]->state == 1) {
bm = gcnew Bitmap("Pictures\\ptr_1.png");
graf->DrawImage(bm, env->Fishers[i]->xPosPtr, env->Fishers[i]->yPosPtr, 8, 16);
delete bm; bm = gcnew Bitmap("Pictures\\ptr_2.png");
graf->DrawImage(
bm, env->Fishers[i]->xPosPtr,
env->Fishers[i]->yPosPtr + env->Fishers[i]->ptrLen, 8, 16);
delete bm;
}
}
}
Void DrawFishes(Drawing::Graphics^ _graf) {
for (int i = 0; i < env->Animals->Count; i++) {
Bitmap^ fish_img = gcnew Bitmap(String::Format("Pictures\\fish_{0}{1}.png", env->Animals[i]->level, env->Animals[i]->state));
graf->DrawImage(fish_img, env->Animals[i]->xPos, env->Animals[i]>yPos, 30, 20);
delete fish_img;
}
}
Void DrawScreen(Drawing::Graphics^ _graf) {
String^ fname_bg;
String^ fname_sky;
String^ fname_water;
fname_bg = String::Format("Pictures\\bg_raw_{0}.png", env->GetStrTOD());
fname_sky = String::Format("Pictures\\skyline_{0}.png", env->GetStrTOD());
fname_water = String::Format("Pictures\\waterline_{0}_{1}.png", env->GetStrTOD(), 1 + rand() % 2);
Bitmap^ bm = gcnew Bitmap(fname_bg);
_graf->DrawImage(bm, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
delete bm;
bm = gcnew Bitmap(fname_sky);
_graf->DrawImage(bm, -env->GetIntTime() % TIMEOFDAY_LEN, 0, SCREEN_WIDTH + TIMEOFDAY_LEN, 137);
delete bm;
bm = gcnew Bitmap(fname_water);
_graf->DrawImage(bm, 0, 138, SCREEN_WIDTH, 30);
DrawFishes(_graf);
delete bm;
bm = gcnew Bitmap("Pictures\\fishing_hut.png");
_graf->DrawImage(bm, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
delete bm;
DrawFisher(_graf);
if (env->Weather->RainTickTime > 0) {
bm = gcnew Bitmap(String::Format("Pictures\\rain_{0}.png", 1 + rand() % 2));
_graf->DrawImage(bm, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
delete bm;
}
}
Void Graphics() {
graf = graf->FromImage(myBitmap);
graf->Clear(Color::White);
DrawScreen(graf);
myBitmap->MakeTransparent(Color::White);
pictureBox1->Image = myBitmap;
delete graf;
}
private: System::Void Timer_Tick(System::Object^ sender, System::EventArgs^ e) {
env->TimerTickActions(timer1->Interval);
UpdateFishStr();
UpdateTimeStr();
Graphics();
CheckWin();
}
Void OnEventCatchFish(Fisher^ _boat, Fish^ _fish) {
lbl_event->Text = String::Format("{0} -- Поймана рыба!", env->GetStrTime());
}
Void OnEventDeleteBoat(System::Object^ sender) {
SetFishboatButtons(false);
btn_changePos->Text = "в лодку";
this->lbl_ptrName->Text = "кукуруза";
this->picBox_ptrTarg->Image = Drawing::Image::FromFile("Pictures\\fish_11.png");
lbl_event->Text = String::Format("{0} -- Рыбак закончил работу.", env->GetStrTime());
}
private: System::Void CheckWin() {
if (env->Fishers->Count > 0 && env->Fishers[0]->fishCount >= FISHTARG) {
lbl_event->Text = String::Format("{0} -- Цель в {1} рыб достигнута!!!", env->GetStrTime(), FISHTARG);
lbl_event->ForeColor = Color::Red;
timer1->Enabled = false;
SetNotRestartButtons(false);
}
}
private: System::Void UpdateFishStr() {
int fcnt = 0;
if (env->Fishers->Count > 0) fcnt = env->Fishers[0]->fishCount;
this->lbl_fishStr->Text = String::Format("{0} / {1}", fcnt, FISHTARG);
}
private: System::Void UpdateTimeStr() {
this->lbl_timeStr->Text = env->GetStrTime();
}
private: System::Void btn_exit_Click(System::Object^ sender, System::EventArgs^ e) {
this->Close();
}
private: System::Void btn_hide_Click(System::Object^ sender, System::EventArgs^ e) {
this->WindowState = FormWindowState::Minimized;
}
private: System::Void btn_mltp_Click(System::Object^ sender, System::EventArgs^ e) {
int x = int::Parse(btn_mltp->Text->Substring(1));
x *= 2;
if (x > MAXMLTP) x = 1;
btn_mltp->Text = String::Format("x{0}", x.ToString());
timer1->Interval = 128 / x;
}
private: System::Void btn_newFishboat_Click(System::Object^ sender, System::EventArgs^ e) {
if (btn_changePos->Text == "на пирс") env->FisherReplace(false);
else env->FisherReplace(true);
env->Fishers[0]->EventDeleteObject += gcnew Fisher::DeleteHandler(this, &Fishing::MyForm::OnEventDeleteBoat);
lbl_event->Text = String::Format("{0} -- Рыбак приступил к ловле.", env->GetStrTime());
SetFishboatButtons(true);
}
private: System::Void SetFishboatButtons(bool value) {
this->btn_changePos->Enabled = value;
this->btn_changePtr->Enabled = value;
this->btn_delFishboat->Enabled = value;
this->btn_newFishboat->Enabled = !value;
}
private: System::Void btn_delFishboat_Click(System::Object^ sender, System::EventArgs^ e) {
SetFishboatButtons(false);
env->FisherDeleting(env->Fishers[0]);
}
private: System::Void btn_changePtr_Click(System::Object^ sender, System::EventArgs^ e) {
SetFishboatButtons(false);
env->Fishers[0]->lvl += 1;
if (env->Fishers[0]->lvl > 4) env->Fishers[0]->lvl = 1;
switch (env->Fishers[0]->lvl) {
case 1: this->lbl_ptrName->Text = "кукуруза"; break;
case 2: this->lbl_ptrName->Text = "червь"; break;
case 3: this->lbl_ptrName->Text = "блесна"; break;
case 4: this->lbl_ptrName->Text = "воблер"; break;
}
this->picBox_ptrTarg->Image = Drawing::Image::FromFile(
String::Format("Pictures\\fish_{0}1.png", env->Fishers[0]->lvl)
);
SetFishboatButtons(true);
}
private: System::Void btn_changePos_Click(System::Object^ sender, System::EventArgs^ e) {
SetFishboatButtons(false);
if (btn_changePos->Text == "на пирс") {
btn_changePos->Text = "в лодку";
env->FisherReplace(true);
}
else {
btn_changePos->Text = "на пирс";
env->FisherReplace(false);
}
env->Fishers[0]->EventDeleteObject += gcnew Fisher::DeleteHandler(this, &Fishing::MyForm::OnEventDeleteBoat);
SetFishboatButtons(true);
}
private: System::Void btn_rainSwitch_Click(System::Object^ sender, System::EventArgs^ e) {
if (env->Weather->RainTickTime > 0) env->Weather->RainStop();
else env->Weather->RainStart();
}
private: System::Void btn_restart_Click(System::Object^ sender, System::EventArgs^ e) {
delete env;
env = gcnew GameEnvironment();
env->EventFishCatched += gcnew GameEnvironment::Handler(this, &MyForm::OnEventCatchFish);
timer1->Interval = DEFAULTTICKS;
btn_mltp->Text = "x1";
lbl_event->ForeColor = Color::Black;
this->picBox_ptrTarg->Image = Drawing::Image::FromFile("Pictures\\fish_11.png");
SetFishboatButtons(false);
SetNotRestartButtons(true);
timer1->Enabled = true;
}
private: System::Void SetNotRestartButtons(bool value) {
btn_changePos->Visible = value;
btn_changePtr->Visible = value;
btn_delFishboat->Visible = value;
btn_mltp->Visible = value;
btn_newFishboat->Visible = value;
btn_rainSwitch->Visible = value;
btn_restart->Visible = !value;
}
};
}