-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pong.cpp
169 lines (131 loc) · 2.93 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
#include "Bat.h"
#include "Ball.h"
#include <sstream>
#include <cstdlib>
#include <SFML/Graphics.hpp>
int main()
{
// Create a video mode object
VideoMode vm(1920, 1080);
// Create and open a window for the game
RenderWindow window(vm, "Pong", Style::Fullscreen);
int score = 0;
int lives = 3;
// Create a bat at the bottom center of the screen
Bat bat(1920 / 2, 1080 - 20);
// Create a ball
Ball ball(1920 / 2, 0);
// Create a Text object called HUD
Text hud;
// A cool retro-style font
Font font;
font.loadFromFile("fonts/DS-DIGI.ttf");
// Set the font to our retro-style
hud.setFont(font);
// Make it nice and big
hud.setCharacterSize(75);
// Choose a color
hud.setFillColor(Color::White);
hud.setPosition(20, 20);
// Here is our clock for timing everything
Clock clock;
while (window.isOpen())
{
/*
Handle the player input
****************************
****************************
****************************
*/
Event event;
while (window.pollEvent(event))
{
if (event.type == Event::Closed)
// Quit the game when the window is closed
window.close();
}
// Handle the player quitting
if (Keyboard::isKeyPressed(Keyboard::Escape))
{
window.close();
}
// Handle the pressing and releasing of the arrow keys
if (Keyboard::isKeyPressed(Keyboard::Left))
{
bat.moveLeft();
}
else
{
bat.stopLeft();
}
if (Keyboard::isKeyPressed(Keyboard::Right))
{
bat.moveRight();
}
else
{
bat.stopRight();
}
/*
Update the bat, the ball and the HUD
*****************************
*****************************
*****************************
*/
// Update the delta time
Time dt = clock.restart();
bat.update(dt);
ball.update(dt);
// Update the HUD text
std::stringstream ss;
ss << "Score:" << score << " Lives:" << lives;
hud.setString(ss.str());
// Handle ball hitting the bottom
if (ball.getPosition().top > window.getSize().y)
{
// reverse the ball direction
ball.reboundBottom();
// Remove a life
lives--;
// Check for zero lives
if (lives < 1) {
// reset the score
score = 0;
// reset the lives
lives = 3;
}
}
// Handle ball hitting top
if (ball.getPosition().top < 0)
{
ball.reboundBatOrTop();
// Add a point to the players score
score++;
}
// Handle ball hitting sides
if (ball.getPosition().left < 0 ||
ball.getPosition().left + ball.getPosition().width> window.
getSize().x)
{
ball.reboundSides();
}
// Has the ball hit the bat?
if (ball.getPosition().intersects(bat.getPosition()))
{
// Hit detected so reverse the ball and score a point
ball.reboundBatOrTop();
}
/*
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;
}