-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
104 lines (80 loc) · 2.13 KB
/
Copy pathmain.cpp
File metadata and controls
104 lines (80 loc) · 2.13 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
#include "Bat.h"
#include "Ball.h"
#include <sstream>
#include <cstdlib>
#include <vector>
int main()
{
VideoMode vm(1920, 1080);
RenderWindow window(vm, "Pong", Style::Fullscreen);
int scorePlayer1 = 0;
int scorePlayer2 = 0;
Bat player1(1920 - 50, 1080 / 2);
Bat player2(50, 1080 / 2);
Ball ball(1920 / 2, 1080 / 2);
Text hud;
Font font;
font.loadFromFile("fonts/DS-DIGIT.ttf");
hud.setFont(font);
hud.setCharacterSize(75);
hud.setFillColor(Color::White);
hud.setPosition(20, 20);
Clock clock;
while (window.isOpen())
{
Event event;
while (window.pollEvent(event))
if (event.type == Event::Closed)
window.close();
if (Keyboard::isKeyPressed(Keyboard::Escape))
window.close();
if (Keyboard::isKeyPressed(Keyboard::Up) && player1.getPosition().top > 0)
player1.moveUp();
else
player1.stopUp();
if (Keyboard::isKeyPressed(Keyboard::Down) && player1.getPosition().top < 1080 - 150)
player1.moveDown();
else
player1.stopDown();
if (Keyboard::isKeyPressed(Keyboard::W) && player2.getPosition().top > 0)
player2.moveUp();
else
player2.stopUp();
if (Keyboard::isKeyPressed(Keyboard::S) && player2.getPosition().top < 1080 - 150)
player2.moveDown();
else
player2.stopDown();
Time dt = clock.restart();
player1.update(dt);
player2.update(dt);
ball.update(dt);
std::stringstream ss;
ss << " Score: " << scorePlayer1 << " Score:" << scorePlayer2;
hud.setString(ss.str());
if (ball.getPosition().left > window.getSize().x)
{
ball.reboundSide();
scorePlayer1++;
}
if (ball.getPosition().left < 0)
{
ball.reboundSide();
scorePlayer2++;
}
if (ball.getPosition().intersects(player1.getPosition()) || ball.getPosition().intersects(player2.getPosition()))
{
ball.reboundBat();
}
if (ball.getPosition().top < 0 || ball.getPosition().top + ball.getPosition().height > window.getSize().y)
{
ball.reboundTopOrBottom();
}
window.clear();
window.draw(hud);
window.draw(player1.getShape());
window.draw(player2.getShape());
window.draw(ball.getShape());
window.display();
}
return 0;
}