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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
| //---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
Graphics::TBitmap*ShipPic;
Graphics::TBitmap*InvadersPic[2][3];
Graphics::TBitmap*BackScreen;
TList*Invaders;
int InvadersDirect;
bool LEFT,RIGHT;
float InvadersSpeed;
int Lifes;
int Scores;
//---------------------------------------------------------------------------
class TInvaders
{
public:
float x;int y;
int Color,State;
int Maxtime;
int time;
TInvaders(int tx,int ty)
{
x=tx;y=ty;
State=random(2);
Color=random(3);
time=0;
Maxtime=random(20)+20;
}
void Draw()
{
BackScreen->Canvas->Draw(x,y,InvadersPic[State][Color]);
}
};
class TShip
{
public:
int x;int y;
int TimeToNextBullet;
int Time;
bool Fired;
TPoint BulletPos;
TShip()
{
Fired=false;
TimeToNextBullet=30;
Time=50;
x=185;
y=365;
BulletPos=Point(199,359);
}
void Draw()
{
BackScreen->Canvas->Pen->Color=clBlue;
BackScreen->Canvas->Pen->Width=3;
BackScreen->Canvas->Draw(x,y,ShipPic);
BackScreen->Canvas->MoveTo(BulletPos.x,BulletPos.y);
BackScreen->Canvas->LineTo(BulletPos.x,BulletPos.y+10);
}
void Fire()
{
if(!Fired)
{
if(Time>TimeToNextBullet)
{
Fired=true;
Time=0;
}
}
}
};
TShip Ship;
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
randomize();
BackScreen=new Graphics::TBitmap();
BackScreen->Width =400;
BackScreen->Height=400;
BackScreen->Canvas->Font->Color=clBlue;
BackScreen->Canvas->Font->Size=15;
Invaders=new TList();
ShipPic=new Graphics::TBitmap();
ShipPic->LoadFromFile("Ship.bmp");
//ShipPic->Transparent=true;
Graphics::TBitmap*Temp=new Graphics::TBitmap();
Temp->LoadFromFile("Invaders.bmp");
for(int j=0;j<3;j++)
for(int i=0;i<2;i++)
{
InvadersPic[i][j]=new Graphics::TBitmap();
InvadersPic[i][j]->Width =30;
InvadersPic[i][j]->Height=30;
InvadersPic[i][j]->Canvas->CopyRect(Rect(0,0,30,30),Temp->Canvas,Rect(i*30,j*30,i*30+30,j*30+30));
InvadersPic[i][j]->Transparent=true;
}
InvadersSpeed=0;
Lifes=3;
Scores=0;
NewGame();
InvadersDirect=1;//Вправо
LEFT=false;RIGHT=false;
}
//---------------------------------------------------------------------------
void TForm1::NewGame()
{
Invaders->Clear();
int x=5,y=5;
for(int j=0;j<4;j++)
{
for(int i=0;i<8;i++,x+=40)
{
TInvaders*temp=new TInvaders(x,y);
Invaders->Add((void*)temp);
}
x=5;
y+=30;
}
InvadersSpeed+=0.5;
}
TForm1::DrawAllInvaders()
{
for(int i=0;i<Invaders->Count;i++)
{
TInvaders*temp=(TInvaders*)Invaders->Items[i];
temp->Draw();
}
return 0;
}
void __fastcall TForm1::FormPaint(TObject *Sender)
{
ClearBack();
DrawAllInvaders();
Ship.Draw();
Flip();
}
//---------------------------------------------------------------------------
void TForm1::ClearBack()
{
BackScreen->Canvas->Brush->Color=clBlack;
BackScreen->Canvas->FillRect(Rect(0,0,400,400));
}
void TForm1::Flip()
{
Canvas->Draw(0,0,BackScreen);
}
bool __fastcall TForm1::MoveInvaders()
{
bool MustMoveDown=false;
TInvaders*temp;
for(int i=0;i<Invaders->Count;i++)
{
temp=(TInvaders*)Invaders->Items[i];
if(InvadersDirect==1)
{
if((temp->x)>370)
{
InvadersDirect=2;
MustMoveDown=true;
}
}
if(InvadersDirect==2)
{
if((temp->x)<0)
{
InvadersDirect=1;
MustMoveDown=true;
}
}
}
for(int i=0;i<Invaders->Count;i++)
{
temp=(TInvaders*)Invaders->Items[i];
temp->time++;
if(temp->time>temp->Maxtime)
{
temp->time=0;
if(temp->State==0)temp->State=1;else temp->State=0;
}
if(InvadersDirect==1)temp->x+=InvadersSpeed;
if(InvadersDirect==2)temp->x-=InvadersSpeed;
if(MustMoveDown)temp->y+=20;
if(temp->y>370)return false;
}
return true;
}
void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
if(!MoveInvaders())
{
Timer1->Enabled=false;
ShowMessage("Вы проиграли");
if(Lifes>0)
{
Lifes--;
InvadersSpeed-=0.5;
NewGame();
}else
{
ShowMessage("У вас нет больше жизней, прийдется начать сначала");
InvadersSpeed=0;
Lifes=3;
Scores=0;
NewGame();
}
Timer1->Enabled=true;
}
if(LEFT)Ship.x-=3;
if(RIGHT)Ship.x+=3;
MoveBullet();
if(IsPobeda())
{
Timer1->Enabled=false;
ShowMessage("Вы победили");
NewGame();
Timer1->Enabled=true;
}
ClearBack();
DrawAllInvaders();
Ship.Draw();
BackScreen->Canvas->Brush->Style=bsClear;
BackScreen->Canvas->TextOut(10,10,"Жизней:"+IntToStr(Lifes));
BackScreen->Canvas->TextOut(10,30,"Очков:"+IntToStr(Scores));
BackScreen->Canvas->Brush->Style=bsSolid;
Flip();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormKeyDown(TObject *Sender, WORD &Key,
TShiftState Shift)
{
if(Key==VK_RIGHT)RIGHT=true;
if(Key==VK_LEFT)LEFT=true;
if(Key==VK_SPACE)Ship.Fire();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormKeyUp(TObject *Sender, WORD &Key,
TShiftState Shift)
{
if(Key==VK_RIGHT)RIGHT=false;
if(Key==VK_LEFT) LEFT =false;
}
//---------------------------------------------------------------------------
void TForm1::MoveBullet()
{
if(!Ship.Fired)
{
Ship.BulletPos.x=Ship.x+14;
Ship.BulletPos.y=Ship.y-6;
}else
{
Ship.BulletPos.y-=10;
Ship.Time++;
if(Ship.Time>40) //При выходе за экран
{
Ship.Fired=false;
}
TInvaders*temp;
for(int i=0;i<Invaders->Count;i++)
{
temp=(TInvaders*)Invaders->Items[i];
if((Ship.BulletPos.x>=temp->x)&&(Ship.BulletPos.x<=temp->x+30)&&
(Ship.BulletPos.y>=temp->y)&&(Ship.BulletPos.y<=temp->y+30))
{
Ship.Fired=false;
Ship.Time=100;
Invaders->Delete(i);
Scores+=10;
delete temp;
break;
}
}
}
}
bool __fastcall TForm1::IsPobeda()
{
if(Invaders->Count>0)return false;else return true;
} |