-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
110 lines (94 loc) · 3.31 KB
/
main.cpp
File metadata and controls
110 lines (94 loc) · 3.31 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
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include "Bat.h"
#include "Ball.h"
#include <sstream>
int main() {
// init window
const int windowWidth = 1600, windowHeight = 900;
sf::VideoMode vm(windowWidth, windowHeight);
sf::RenderWindow window(vm, "pong");
// init objects
Bat bat1(100, 100, sf::Keyboard::Up, sf::Keyboard::Down);
Bat bat2(windowWidth - 100, windowHeight - 100, sf::Keyboard::Left, sf::Keyboard::Right);
Ball ball(600, 600);
int score1 = 0;
int score2 = 0;
sf::Text hud;
sf::Font font;
font.loadFromFile("assets/fonts/JetBrainsMono-Bold.ttf");
hud.setFont(font);
hud.setCharacterSize(60);
hud.setFillColor(sf::Color::White);
hud.setPosition(windowWidth / 2 - 55 , 10);
sf::SoundBuffer clackBuffer;
clackBuffer.loadFromFile("assets/sounds/tick.wav");
sf::Sound clack;
clack.setBuffer(clackBuffer);
sf::SoundBuffer scoreBuffer;
scoreBuffer.loadFromFile("assets/sounds/nope.wav");
sf::Sound score;
score.setBuffer(scoreBuffer);
sf::Clock clock;
// game loop
while (window.isOpen()) {
// handle user input event
sf::Event event{};
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed) {
window.close();
}
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)) {
window.close();
}
(!sf::Keyboard::isKeyPressed(bat1.getKeyUp()) || bat1.getPosition().top < 0)
? bat1.stopUp() : bat1.moveUp();
(!sf::Keyboard::isKeyPressed(bat1.getKeyDown())
|| bat1.getPosition().top + bat1.getPosition().height > window.getSize().y)
? bat1.stopDown(): bat1.moveDown();
(!sf::Keyboard::isKeyPressed(bat2.getKeyUp()) || bat2.getPosition().top < 0)
? bat2.stopUp() : bat2.moveUp();
(!sf::Keyboard::isKeyPressed(bat2.getKeyDown())
|| bat2.getPosition().top + bat2.getPosition().height > window.getSize().y)
? bat2.stopDown(): bat2.moveDown();
// update object state
sf::Time dt = clock.restart();
bat1.update(dt);
bat2.update(dt);
ball.update(dt);
// handle collision
if (ball.getPosition().top < 0 || ball.getPosition().top > window.getSize().y) {
ball.bounceTopOrBot();
clack.play();
}
if (ball.getPosition().intersects(bat1.getPosition()) || ball.getPosition().intersects(bat2.getPosition())) {
ball.reboundSides();
ball.speedUp();
clack.play();
}
if (ball.getPosition().left < 0) {
ball.reset(windowWidth / 2, windowHeight / 2);
score2 += 1;
score.play();
}
if (ball.getPosition().left + ball.getPosition().width > window.getSize().x) {
ball.reset(windowWidth / 2, windowHeight / 2);
score1 += 1;
score.play();
}
std::stringstream ss;
ss << score1 << ":" << score2;
hud.setString(ss.str());
// clear window
window.clear();
// then draw
window.draw(hud);
window.draw(bat1.getShape());
window.draw(bat2.getShape());
window.draw(ball.getShape());
// display window
window.display();
}
return 0;
}