forked from arowayasmeen/Arcade
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpong.cpp
334 lines (247 loc) · 6.85 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
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#include <SFML/Graphics.hpp>
#include <iostream>
#include <sstream>
#include <string>
#include <sstream>
#include <SFML/Audio.hpp>
#include "pong.h"
using namespace std;
int pong::pongf() {
sf::RenderWindow window(sf::VideoMode(1000, 600), "POKE PONG!");//create window
window.setFramerateLimit(60);
window.setKeyRepeatEnabled(false);
sf::Event event;
//font
sf::Font font;
if (font.loadFromFile("Resources/a.ttf") == 0) {
return 0;
}
sf::Font font2;
if (font2.loadFromFile("Resources/pokefont.ttf") == 0) {
return 0;
}
sf::Text score;
score.setFont(font2);
score.setCharacterSize(30);
score.setFillColor(sf::Color::Black);
score.setPosition(450, 15);
score.setString("0 : 0");
sf::Text player1;
player1.setFont(font2);
player1.setCharacterSize(50);
player1.setFillColor(sf::Color::Black);
player1.setPosition(350, 250);
player1.setString("Player 1 Wins!");
sf::Text player2;
player2.setFont(font2);
player2.setCharacterSize(50);
player2.setFillColor(sf::Color::Black);
player2.setPosition(300, 250);
player2.setString("Computer Wins!");
sf::Text esc;
esc.setFont(font);
esc.setCharacterSize(30);
esc.setFillColor(sf::Color::Black);
esc.setPosition(260, 350);
esc.setString("press any key to exit");
//images
sf::Texture texBall;
sf::Texture texBackground;
if (texBall.loadFromFile("Resources/ball2.png") == 0)return 0;
if (texBackground.loadFromFile("Resources/poke2.png") == 0)return 0;
//sounds
sf::SoundBuffer hitBuff;
if (hitBuff.loadFromFile("Resources/hit.wav") == 0)return 0;
sf::Sound hit;
hit.setBuffer(hitBuff);
//states
bool play = true;
bool up = false;
bool down = false;
//Variables
int yVelocityPad1 = 0;
int yVelocityBall = -6;
int xVelocityBall = -6;
int yVelocityPad2 = 0;
int pad1Score = 0;
int pad2Score = 0;
////Shape
//background
sf::RectangleShape background;
background.setSize(sf::Vector2f(1000, 600));
background.setPosition(0, 0);
background.setTexture(&texBackground);
//Pad1
sf::RectangleShape pad1;
pad1.setSize(sf::Vector2f(20, 120));
pad1.setPosition(30, 200);
pad1.setFillColor(sf::Color::Black);
//background.setTexture(&texBackground);
//pad2
sf::RectangleShape pad2;
pad2.setSize(sf::Vector2f(20, 120));
pad2.setPosition(950, 200);
pad2.setFillColor(sf::Color::Black);
//ball
sf::RectangleShape ball;
ball.setSize(sf::Vector2f(30, 30));
ball.setPosition(500, 300);
ball.setTexture(&texBall);
//game loop
while (play) {
//events
//sf::Event event;
//events stored in queue
while (window.pollEvent(event)) {
//cross to close window
if (event.type == sf::Event::Closed) {
play = false;
}
//KeyPressed
if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Up) {
up = true;
}
if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Down) {
down = true;
}
//KeyReleased
if (event.type == sf::Event::KeyReleased && event.key.code == sf::Keyboard::Up) {
up = false;
}
if (event.type == sf::Event::KeyReleased && event.key.code == sf::Keyboard::Down) {
down = false;
}
}
//////logic
////pad1
if (up)yVelocityPad1 = -7;
if (down)yVelocityPad1 = 7;
if (up && down)yVelocityPad1 = 0;
if (!up && !down)yVelocityPad1 = 0;
//move
pad1.move(0, yVelocityPad1);
//check bounds
if (pad1.getPosition().y < 0)pad1.setPosition(30, 0);
if (pad1.getPosition().y > 480)pad1.setPosition(30, 480);
////ball
//move
ball.move(xVelocityBall, yVelocityBall);
//check bounds
if (ball.getPosition().y < 0)yVelocityBall = -yVelocityBall;
if (ball.getPosition().y > 570)yVelocityBall = -yVelocityBall;
if (ball.getPosition().x < -1) {
pad2Score++;
ball.setPosition(500, 300);
}
if (ball.getPosition().x > 1000) {
pad1Score++;
ball.setPosition(500, 300);
}
////pad2
if (ball.getPosition().y < pad2.getPosition().y)yVelocityPad2 = -6;
if (ball.getPosition().y > pad2.getPosition().y)yVelocityPad2 = 6;
pad2.move(0, yVelocityPad2);
if (pad2.getPosition().y < 0)pad2.setPosition(950, 0);
if (pad2.getPosition().y > 480)pad2.setPosition(950, 480);
//collision for pad1
if (ball.getGlobalBounds().intersects(pad1.getGlobalBounds()) == true) {
hit.play();
//ball.setPosition(pad1.getPosition().x + 20, ball.getPosition().y);
//dsdh bbv cout << ball.getPosition().x << endl;
if (ball.getPosition().y < pad1.getPosition().y && ball.getPosition().x < 44)yVelocityBall = -yVelocityBall;
else if (ball.getPosition().y + 30 > pad1.getPosition().y + 120 && ball.getPosition().x < 44)yVelocityBall = -yVelocityBall;
else {
xVelocityBall = -xVelocityBall;
pad1Score++;
}
}
//collison for pad2
if (ball.getGlobalBounds().intersects(pad2.getGlobalBounds()) == true) {
ball.setPosition(pad2.getPosition().x - 17 * 2, ball.getPosition().y);
xVelocityBall = -xVelocityBall;
hit.play();
pad2Score++;
}
if (pad1Score == 10 & pad2Score != 10) {
bool play = true;
while (play) {
sf::Event ev;
while (window.pollEvent(ev)) {
if (ev.type == sf::Event::Closed)
{
play = false;
window.close();
return 0;
}
if ((ev.type == sf::Event::KeyPressed))
{
play = false;
window.close();
return 0;
}
}
window.clear();
window.draw(background);
window.draw(pad1);
window.draw(pad2);
window.draw(ball);
//score
ostringstream ss;
ss << pad1Score << " : " << pad2Score;
score.setString(ss.str());
window.draw(score);
window.draw(player1);
window.draw(esc);
window.display();
}
}
if (pad1Score != 10 & pad2Score == 10) {
bool play = true;
while (play) {
sf::Event ev;
while (window.pollEvent(ev)) {
if (ev.type == sf::Event::Closed)
{
play = false;
window.close();
return 0;
}
if ((ev.type == sf::Event::KeyPressed))
{
play = false;
window.close();
return 0;
}
}
window.clear();
window.draw(background);
window.draw(pad1);
window.draw(pad2);
window.draw(ball);
//score
ostringstream ss;
ss << pad1Score << " : " << pad2Score;
score.setString(ss.str());
window.draw(score);
window.draw(player2);
window.draw(esc);
window.display();
}
}
//rendering
window.clear();
window.draw(background);
window.draw(pad1);
window.draw(pad2);
window.draw(ball);
//score
ostringstream ss;
ss << pad1Score << " : " << pad2Score;
score.setString(ss.str());
window.draw(score);
window.display();
}
//clean up
window.close();
return 0;
}