-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpong.cpp
More file actions
124 lines (98 loc) · 2.83 KB
/
Copy pathpong.cpp
File metadata and controls
124 lines (98 loc) · 2.83 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
#include "pong.h"
#include <iostream>
using namespace sf;
int main() {
VideoMode vm({SCREEN_WIDTH, SCREEN_HEIGHT});
RenderWindow window(vm, "Pong", Style::Default);
int score = 0;
int lives = 3;
bool isCollided = false;
Bat bat(SCREEN_WIDTH / 2, SCREEN_HEIGHT - 20);
Ball ball(SCREEN_WIDTH / 2, 0);
Font font("assets/fonts/DepartureMono-Regular.otf");
Text hud(font, "");
hud.setCharacterSize(static_cast<int>(SCREEN_HEIGHT * 0.04));
hud.setFillColor(Color::White);
hud.setPosition({20, 20});
Clock clock;
while (window.isOpen()) {
/*
Handle the player input
****************************
****************************
****************************
*/
while (const std::optional event = window.pollEvent()) {
if (event->is<Event::Closed>()) {
window.close();
} else if (const auto *keyPressed = event->getIf<Event::KeyPressed>()) {
if (keyPressed->scancode == Keyboard::Scancode::Escape)
window.close();
}
}
if (Keyboard::isKeyPressed(Keyboard::Scancode::A) ||
Keyboard::isKeyPressed(Keyboard::Scancode::Left)) {
bat.moveLeft();
} else {
bat.stopLeft();
}
if (Keyboard::isKeyPressed(Keyboard::Scancode::D) ||
Keyboard::isKeyPressed(Keyboard::Scancode::Right)) {
bat.moveRight();
} else {
bat.stopRight();
}
/*
Update the bat, the ball and the HUD
*****************************
*****************************
*****************************
*/
Time dt = clock.restart();
bat.update(dt);
ball.update(dt);
std::stringstream ss;
ss << "Score: " << score << " | Live: " << lives;
hud.setString(ss.str());
if (ball.getGlobalBounds().position.y >
static_cast<float>(window.getSize().y)) {
ball.reboundBottom();
lives--;
if (lives < 1) {
score = 0;
lives = 3;
}
}
if (ball.getGlobalBounds().position.y < 0) {
ball.reboundBatOrTop();
}
if (ball.getGlobalBounds().position.x < 0 ||
ball.getGlobalBounds().position.x + ball.getGlobalBounds().size.x >
static_cast<float>(window.getSize().x)) {
ball.reboundSides();
}
if (const std::optional intersection =
ball.getGlobalBounds().findIntersection(bat.getGlobalBounds())) {
if (!isCollided &&
ball.getPosition().y >= bat.getPosition().y - ball.getSize().y) {
score++;
isCollided = true;
}
ball.reboundBatOrTop();
} else {
isCollided = false;
}
/*
Draw the bat, the ball and the HUD
*****************************
*****************************
*****************************
*/
window.clear();
window.draw(hud);
window.draw(bat.getShape());
window.draw(ball.getShape());
window.display();
}
return 0;
}