-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPong.cpp
250 lines (231 loc) · 5.52 KB
/
Pong.cpp
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
// Copyright Milan Oberkirch
// <[email protected]>
#include <curses.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <vector>
#include "./Pong.h"
#include "./Pad.h"
#include "./Bat.h"
#include "./Ball.h"
using std::vector;
// ____________________________________________________________________________
Pong::Pong()
{
}
// ____________________________________________________________________________
void Pong::Init(
int relativeSpeed,
int batSize,
int pointsToWin,
char ballTrace,
int speedup,
bool bell)
{
_relativeSpeed = relativeSpeed;
_batSize = batSize;
_pointsToWin = pointsToWin;
_ballTrace = ballTrace;
_speedup = speedup;
_bell = bell;
// Initialicing Pad
_pad.Init();
// Initialicing Ball
vector<float> direction;
direction.push_back(0.8 * _relativeSpeed * 0.01);
direction.push_back(0.3 * _relativeSpeed * 0.01);
_ball.Init(_pad._xMid, _pad._yMid, direction, _ballTrace, speedup);
// Initialicing Bats
_batLeft.Init(_pad._xMinPlay, _pad._yMid, _pad._yMinPlay, _pad._yMaxPlay,
_batSize);
_batRight.Init(_pad._xMaxPlay, _pad._yMid, _pad._yMinPlay, _pad._yMaxPlay,
_batSize);
_pad.Draw();
Draw();
}
// ____________________________________________________________________________
void Pong::Run()
{
while (true)
{
// Handling all events.
Event();
// Moving _ball one step further (refreshes screen)
_ball.Step();
// Slowing down a bit (importent for Performance)
usleep(3000);
}
}
// ____________________________________________________________________________
void Pong::Draw()
{
_batRight.Draw();
_batLeft.Draw();
}
// ____________________________________________________________________________
void Pong::printUsageAndPause()
{
_ball.toggle = true;
_pad.Clear();
printw(
"HOW TO PLAY:\n"
"Goal:\n\tMove your bat up or down to hit the ball.\n"
"Keys:\n"
"* left bat:\n"
"\tq:\t move up\n"
"\ta:\t move down\n"
"* right bat:\n"
"\tarrow key up:\t move up\n"
"\tarrow key down:\t move down\n"
"any key: print this massage and pause\n"
"STRG+C:\t Exit game!\n"
"PRESS 'ESC' TO GO BACK TO THE GAME");
}
// ____________________________________________________________________________
void Pong::Event()
{
HandleKeyEvents();
HandleBallEvents();
JumpBatIfOut(&_batRight);
JumpBatIfOut(&_batLeft);
}
void Pong::HandleKeyEvents()
{
char key = getch();
if (key < 0) key = 7; // ncurses makes it possible to have negative chars
if (_ball.toggle && (key == 7 || key != 27)) return;
switch (key)
{
// unspecified input (7 is mapped to bell -> could not be set by user)
case 7: break;
// Up-Key for right bat
case 3: _batRight.Move(true);
break;
// Down-Key for right bat
case 2: _batRight.Move(false);
break;
// Up-Key for left bat
case 'q': _batLeft.Move(true);
break;
// Down-Key for left bat
case 'a': _batLeft.Move(false);
break;
case 27: _pad.Clear();
Draw();
_pad.Draw();
_ball.toggle = false;
break;
default: printUsageAndPause();
break;
}
}
void Pong::HandleBallEvents()
{
// ball hits one of the horizontal walls
if (_ball._y == _pad._yMinPlay || _ball._y == _pad._yMaxPlay)
{
_ball.ReflectVertically();
_ball.Step();
bell();
Draw();
}
IsBallAtLeftBat();
IsBallAtRightBat();
}
void Pong::IsBallAtLeftBat()
{
const Bat &bat = _batLeft;
if (_ball.toggle) return;
// ball touches border
if (_ball._x <= _pad._xMinPlay)
{
// ball touches bat
if ((bat._y <= _ball._y) &&
(bat._y > (_ball._y - bat._length)))
{
// reflect on bat
_ball.ReflectHorizontally();
_ball.Step();
// beep and redraw bats
bell();
Draw();
}
// ball leaves at left side (not really but it would)
else
{
// Score for the other player
_pad._counterRight++;
if (_pad._counterRight >= _pointsToWin) Win();
_pad.Draw();
// reinitialice ball
_ball.Init(_pad._xMid, _pad._yMid);
// redraw bats
Draw();
}
}
}
void Pong::IsBallAtRightBat()
{
const Bat &bat = _batRight;
if (_ball.toggle) return;
// ball touches border
if (_ball._x >= _pad._xMaxPlay)
{
// ball touches bat
if ((bat._y <= _ball._y) &&
(bat._y > (_ball._y - bat._length)))
{
// reflect on bat
_ball.ReflectHorizontally();
_ball.Step();
// beep and redraw bats
bell();
Draw();
}
// ball leaves at left side (not really but it would)
else
{
// Score for the other player
_pad._counterLeft++;
if (_pad._counterLeft >= _pointsToWin) Win();
_pad.Draw();
// reinitialice ball
_ball.Init(_pad._xMid, _pad._yMid);
// redraw bats
Draw();
}
}
}
void Pong::JumpBatIfOut(Bat *bat)
{
if (bat->_y + bat->_length < _pad._yMinPlay)
{
bat->Draw(_pad._yMaxPlay);
}
if (bat->_y > _pad._yMaxPlay)
{
bat->Draw(_pad._yMinPlay);
}
}
void Pong::Win()
{
_ball.toggle = true;
_pad.Clear();
printw(
"YOU WON WITH FINEL SCORE: %d : %d\n"
"New game? (Press CTRL+C to exit or any key to start a new game)\n",
_pad._counterLeft, _pad._counterRight);
char c = 0;
while (true)
{
c = getch();
if (c > 0) break;
}
Init(_relativeSpeed, _batSize, _pointsToWin, _ballTrace, _speedup, _bell);
_ball.toggle = false;
}
void Pong::bell()
{
if (_bell) printf("\a");
}