Приложение "Стрельба по мишеням" на C++/CLI. Ошибка
29.05.2019, 17:42. Показов 1265. Ответов 1
Подскажите, пожалуйста, как исправить данные ошибки, и что-то мне подсказывает, что они имеют одну природу.
Заголовочный файл "Carnage.h"
| C++ | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
| #pragma once
#include <cmath>
#include <ctime>
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
using namespace std;
ref class BattleField
{
ref struct Element
{
Bullet ^ item;
Element ^ next;
};
private:
Element ^ first;
double width;
double height;
int score;
PictureBox ^ PB;
public:
Target ^ t;
Gun ^ p;
BattleField(PictureBox^pb, TextBox^t)
{
t->Visible = true;
PB = pb;
width = pb->Width;
height = pb->Height;
score = 0;
}
void Push(Bullet ^ b)
{
Element ^ temp = gcnew Element;
temp->item = b;
temp->next = first;
first = temp;
}
double getW()
{
return width;
}
double getH()
{
return height;
}
void Draw()
{
Bitmap ^ bmp;
bmp = gcnew Bitmap(PB->Width, PB->Height);
Graphics ^ g = Graphics::FromImage(bmp);
g->Clear(PB->BackColor);
Element ^ temp = first;
while (temp != nullptr)
{
temp->item->Draw(g);
temp = temp->next;
}
t->Draw(g);
p->Draw(g);
PB->Image = bmp;
}
void Tick()
{
Element ^ temp = first;
while (temp != nullptr)
{
temp->item->Tick();
score += (int)temp->item->Shot(t);
temp = temp->next;
if (t->shot)
t->Tick();
else
t->Replace(this);
}
}
};
ref class Gun
{
double a;
double x;
double y;
public:
Gun(double X, double Y, double A)
{
x = X / 2;
y = Y;
a = A;
}
void TurnLeft()
{
if (a - 0, 5 < 360)
a += 0, 5;
}
void TurnRight()
{
if (a - 0, 5 > 180)
a -= 0, 5;
}
void Fire()
{
}
void Draw(Graphics^g)
{
Bitmap^img = gcnew Bitmap("pig.png");
g->RotateTransform((float)a + 180);
g->DrawImage(img, (float)x, (float)y);
}
};
ref class Bullet
{
bool fly;
double a;
double x;
double nativeX;
double nativeY;
double y;
double r;
Brush ^ c;
public:
Bullet(double X, double Y, double A, double R)
{
x = X / 2;
y = Y;
nativeX = x;
nativeY = y;
a = A;
r = R;
fly = false;
}
void Draw(Graphics ^ g)
{
if (fly)
g->FillEllipse(c, (float)(x - r), (float)(y - r), (float)2 * r, (float)r * 2);
}
void traectory()
{
x = x + sin(a);
}
void Board(double w, double h)
{
if (!fly)
return;
if (x > w - r || x < r || y < r)
{
fly = false;
}
}
void Tick()
{
if (fly)
{
double delta = r / 4;
x += sin(a)*delta;
y += cos(a)*delta;
}
}
bool Shot(Target ^ t)
{
if ((t->getX() + 4 == x || t->getX() - 4 == x) && t->getY() == y)
{
fly = false;
t->shot = true;
a = 0;
x = nativeX;
y = nativeY;
return true;
}
}
};
ref class Target
{
double x;
double y;
public:
bool shot;
Target(BattleField^f)
{
srand(time(0));
y = rand() % (int)f->getH() / 2 + 1;
}
void Replace(BattleField^f)
{
srand(time(0));
y = rand() % (int)f->getH() / 2 + 1;
shot = false;
}
void Draw(Graphics^g)
{
Bitmap^img = gcnew Bitmap("pig.png");
g->DrawImage(img, (float)x, (float)y);
}
void Tick()
{
if (shot == false)
{
x = x + 0.0001;
}
}
double getX()
{
return x;
}
double getY()
{
return y;
}
}; |
|
Файл "Battle.h"- непосредственно форма
| C++ | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
| #pragma once
#include "Carnage.h"
namespace BattleRange
{
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
using namespace std;
/// <summary>
/// Сводка для Battle
/// </summary>
public ref class Battle : public System::Windows::Forms::Form
{
public:
Battle(void)
{
InitializeComponent();
BF = gcnew BattleField(pictureBox1, textBox1);
BF->Push(gcnew Bullet(pictureBox1->Width / 2, pictureBox1->Height, 0, 2));
BF->Push(gcnew Bullet(pictureBox1->Width / 2, pictureBox1->Height, 0, 2));
BF->Push(gcnew Bullet(pictureBox1->Width / 2, pictureBox1->Height, 0, 2));
BF->t = gcnew Target(BF);
BF->p = gcnew Gun(pictureBox1->Width / 2, pictureBox1->Height, 270);
//
//TODO: добавьте код конструктора
//
}
protected:
/// <summary>
/// Освободить все используемые ресурсы.
/// </summary>
~Battle()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::PictureBox^ pictureBox1;
private: System::ComponentModel::IContainer^ components;
private: System::Windows::Forms::TextBox^ textBox1;
private: System::Windows::Forms::Timer^ timer1;
protected:
protected:
private:
/// <summary>
/// Обязательная переменная конструктора.
/// </summary>
bool play = 0;
#pragma region Windows Form Designer generated code
/// <summary>
/// Требуемый метод для поддержки конструктора — не изменяйте
/// содержимое этого метода с помощью редактора кода.
/// </summary>
void InitializeComponent(void)
{
this->components = (gcnew System::ComponentModel::Container());
System::ComponentModel::ComponentResourceManager^ resources = (gcnew System::ComponentModel::ComponentResourceManager(Battle::typeid));
this->pictureBox1 = (gcnew System::Windows::Forms::PictureBox());
this->textBox1 = (gcnew System::Windows::Forms::TextBox());
this->timer1 = (gcnew System::Windows::Forms::Timer(this->components));
(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->pictureBox1))->BeginInit();
this->SuspendLayout();
//
// pictureBox1
//
this->pictureBox1->BackgroundImage = (cli::safe_cast<System::Drawing::Image^>(resources->GetObject(L"pictureBox1.BackgroundImage")));
this->pictureBox1->Cursor = System::Windows::Forms::Cursors::Arrow;
this->pictureBox1->Location = System::Drawing::Point(12, 12);
this->pictureBox1->Name = L"pictureBox1";
this->pictureBox1->Size = System::Drawing::Size(666, 444);
this->pictureBox1->TabIndex = 0;
this->pictureBox1->TabStop = false;
//
// textBox1
//
this->textBox1->BackColor = System::Drawing::Color::White;
this->textBox1->BorderStyle = System::Windows::Forms::BorderStyle::None;
this->textBox1->Font = (gcnew System::Drawing::Font(L"Papyrus", 14.25F, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->textBox1->ForeColor = System::Drawing::Color::Red;
this->textBox1->Location = System::Drawing::Point(158, 97);
this->textBox1->Name = L"textBox1";
this->textBox1->Size = System::Drawing::Size(376, 30);
this->textBox1->TabIndex = 1;
this->textBox1->Text = L"Press \"SPACE\" to start the game";
this->textBox1->TextAlign = System::Windows::Forms::HorizontalAlignment::Center;
//
// timer1
//
this->timer1->Tick += gcnew System::EventHandler(this, &Battle::timer1_Tick);
//
// Battle
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(732, 503);
this->Controls->Add(this->textBox1);
this->Controls->Add(this->pictureBox1);
this->Name = L"Battle";
this->Text = L"Battle";
this->KeyDown += gcnew System::Windows::Forms::KeyEventHandler(this, &Battle::Battle_KeyDown);
(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->pictureBox1))->EndInit();
this->ResumeLayout(false);
this->PerformLayout();
}
#pragma endregion
private: BattleField ^ BF;
private: System::Void Battle_KeyDown(System::Object^ sender, System::Windows::Forms::KeyEventArgs ^ e)
{
if (play)
{
if (e->KeyCode == Keys::D || e->KeyCode == Keys::Right)
{
BF->p->TurnRight();
}
else if (e->KeyCode == Keys::A || e->KeyCode == Keys::Left)
{
BF->p->TurnLeft();
}
else if (e->KeyCode == Keys::W || e->KeyCode == Keys::Space)
{
if (play = false)
{
textBox1->Visible = false;
timer1->Enabled = !timer1->Enabled;
}
BF->p->Fire();
}
else
{
}
}
}
private: System::Void timer1_Tick(System::Object^ sender, System::EventArgs^ e)
{
BF->Tick();
Invalidate();
}
private: void Battle_Paint(Object^sender, PaintEventArgs^e)
{
BF->Draw();
}
};
} |
|
Battle.cpp
| C++ | 1
2
3
4
5
6
7
8
9
10
| #include "Battle.h"
using namespace BattleRange;
[STAThreadAttribute]
int main(array<System::String ^> ^args)
{
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
Application::Run(gcnew Battle());
return 0;
} |
|
1
|