-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnit1.cpp
More file actions
210 lines (186 loc) · 7.64 KB
/
Unit1.cpp
File metadata and controls
210 lines (186 loc) · 7.64 KB
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
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
float x=-5;
float y=-3;
int hits=0;
int scorePlayer1=0;
int scorePlayer2=0;
int seconds=4;
void speedIncrement () {
hits++;
if (hits%5==0) x*=1.1;
}
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
AnsiString strHelloMessage = "Welcome in PingPong game!";
AnsiString strSteeringRules = "How to play?";
AnsiString strSteeringRulesPlayer1 = "Player 1 is using 'A' and 'Z'";
AnsiString strSteeringRulesPlayer2 = "Player 2 is using UP_ARROW and DOWN_ARROW";
AnsiString strGameRules1 = "To increase the angle hit the ball with edge of the paddle";
AnsiString strGameRules2 = "To decrease the angle hit the ball with the center part the paddle";
AnsiString strGameRules3 = "Every 5 hit speed will increase";
AnsiString strFinal = "Enjoy!";
ShowMessage(strHelloMessage + sLineBreak + sLineBreak + strSteeringRules + sLineBreak +
strSteeringRulesPlayer1 + sLineBreak + strSteeringRulesPlayer2 + sLineBreak + sLineBreak +
strGameRules1 + sLineBreak + strGameRules2 +sLineBreak + strGameRules3 + sLineBreak + sLineBreak +
strFinal);
startCounter->Enabled=true;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::mainTimer(TObject *Sender)
{
AnsiString angle;
AnsiString speed;
angle=FloatToStrF(abs(y),ffNumber, 7, 1);
speed=FloatToStrF(abs(x),ffNumber, 7, 1);
Label1->Caption="Angle="+angle+" Hits="+hits+" Speed="+speed;
ball->Left +=x;
ball->Top +=y;
//odbicie od górnej sciany;
if (ball->Top-5 <= playground->Top) y=-y;
//odbicie od dolnej sciany;
else if ((ball->Top + ball->Height+5) >= (playground->Top + playground->Height)) y=-y;
//koniec gry
if ((ball->Left < player1->Left + player1->Width - 5 && (ball->Top > player1->Top + player1->Height || ball->Top + ball->Height < player1->Top)) ||
(ball->Left + ball->Width > player2->Left + 5 && (ball->Top > player2->Top + player2->Height || ball->Top + ball->Height < player2->Top))){
main->Enabled=false;
ball->Visible=false;
p1_up->Enabled=false;
p1_down->Enabled=false;
p2_up->Enabled=false;
p2_down->Enabled=false;
if (ball->Left < 100) {
winner->Caption="Player 2 won!";
scorePlayer1++;
}
else {
winner->Caption="Player 1 won!";
scorePlayer2++;
}
winner->Visible=true;
score->Caption=IntToStr(scorePlayer1)+":"+IntToStr(scorePlayer2);
score->Visible=true;
Button1->Visible=true;
Button2->Visible=true;
Button3->Visible=true;
}
//odbicie player1
else if (ball->Top < player1->Top + player1->Height &&
ball->Top + ball->Height > player1->Top &&
ball->Left <= player1->Left + player1->Width) {
x=-x;
speedIncrement();
if (ball->Top <= player1->Top + player1->Height - 30 &&
ball->Top + ball->Height >= player1->Top + 30 &&
ball->Left <= player1->Left + player1->Width) {
if (y>0) y-=0.2;
else if (y<0) y+=0.2;
}
else {
if (y>0) y+=2;
else y-=2;
}
}
//odbicie player2
else if (ball->Top < player2->Top + player2->Height &&
ball->Top + ball->Height > player2->Top &&
ball->Left + ball->Width >= player2->Left) {
x=-x;
speedIncrement();
if (ball->Top <= player2->Top + player2->Height - 30 &&
ball->Top + ball->Height >= player2->Top + 30 &&
ball->Left + ball->Width >= player2->Left) {
if (y>0) y-=0.2;
else if (y<0) y+=0.2;
}
else {
if (y>=0) y+=2;
else y-=2;
}
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::p1_upTimer(TObject *Sender)
{
if (player1->Top > 10) player1->Top -= 10;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::p1_downTimer(TObject *Sender)
{
if (player1->Top + player1->Height < playground->Top + playground->Height - 10) player1->Top += 10;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::p2_upTimer(TObject *Sender)
{
if (player2->Top > 10) player2->Top -= 10;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::p2_downTimer(TObject *Sender)
{
if (player2->Top + player2->Height < playground->Top + playground->Height - 10) player2->Top += 10;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormKeyDown(TObject *Sender, WORD &Key,
TShiftState Shift)
{
if (Key == 0x41) p1_up->Enabled=true;
if (Key == 0x5A) p1_down->Enabled=true;
if (Key == VK_UP) p2_up->Enabled=true;
if (Key == VK_DOWN) p2_down->Enabled=true;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormKeyUp(TObject *Sender, WORD &Key,
TShiftState Shift)
{
if (Key == 0x41) p1_up->Enabled=false;
if (Key == 0x5A) p1_down->Enabled=false;
if (Key == VK_UP) p2_up->Enabled=false;
if (Key == VK_DOWN) p2_down->Enabled=false;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
winner->Visible=false;
score->Visible=false;
Button1->Visible=false;
Button2->Visible=false;
Button3->Visible=false;
ball->Top = 234; ball->Left = 484;
ball->Visible=true;
main->Enabled=true;
y=-3;
x=-5;
hits=0;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
scorePlayer1=0;
scorePlayer2=0;
Button1Click(Owner);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{
Application->Terminate();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::startCounterTimer(TObject *Sender)
{
seconds--;
startLabel->Caption=IntToStr(seconds);
if(seconds<1) {
startLabel->Visible=false;
startCounter->Enabled=false;
main->Enabled=true;
}
}
//---------------------------------------------------------------------------