This repository was archived by the owner on Sep 3, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
106 lines (99 loc) · 3.18 KB
/
main.cpp
File metadata and controls
106 lines (99 loc) · 3.18 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
#include <SFML/Graphics.hpp>
#include "common.hpp"
#include "Pala.hpp"
#include "Pilota.hpp"
#include "Marcador.hpp"
#include "Title.hpp"
#include "Random.hpp"
bool paused;
bool show_title;
const char* no_asset = "No s'ha pogut carregar un arxiu. Assegura't que "
"`assets` està al mateix directori que l'executable";
std::default_random_engine Random::gen;
using kbd = sf::Keyboard; // Alias per a sf::Keyboard
int main() {
sf::RenderWindow window(sf::VideoMode(WIDTH, HEIGHT),
"PING-PONG");
window.setKeyRepeatEnabled(false);
Random::init();
Title title;
Marcador marcador;
Pilota pilota(&marcador);
Pala j1(Pala::POS_X);
Pala j2(WIDTH - Pala::POS_X - Pala::DX);
paused = true;
show_title = true;
while (window.isOpen()) {
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed)
window.close();
switch (event.type) {
case sf::Event::Closed:
window.close();
break;
case sf::Event::KeyPressed:
switch (event.key.code) {
case sf::Keyboard::Space:
paused = !paused;
break;
case sf::Keyboard::Escape:
window.close();
break;
case sf::Keyboard::F4: // Alt-F4
if (event.key.alt)
window.close();
break;
default:
// No fer res (siencia advertències d'alguns
// compiladors.
;
}
break;
default:
;
}
}
if (show_title) {
if (!paused) show_title = false;
else {
title.render(window);
continue;
}
}
if (kbd::isKeyPressed(kbd::W) &&
!kbd::isKeyPressed(kbd::S)) {
if (kbd::isKeyPressed(kbd::LShift))
j1.move(-5 * Pala::MOV);
else
j1.move(-Pala::MOV);
}
if (kbd::isKeyPressed(kbd::S) &&
!kbd::isKeyPressed(kbd::W)) {
if (kbd::isKeyPressed(kbd::LShift))
j1.move(5 * Pala::MOV);
else
j1.move(Pala::MOV);
}
if (kbd::isKeyPressed(kbd::I) &&
!kbd::isKeyPressed(kbd::K)) {
if (kbd::isKeyPressed(kbd::RShift))
j2.move(-5 * Pala::MOV);
else
j2.move(-Pala::MOV);
}
if (kbd::isKeyPressed(kbd::K) &&
!kbd::isKeyPressed(kbd::I)) {
if (kbd::isKeyPressed(kbd::RShift))
j2.move(5 * Pala::MOV);
else
j2.move(Pala::MOV);
}
window.clear(sf::Color::White);
pilota.render(window, j1, j2);
j1.render(window);
j2.render(window);
marcador.render(window);
window.display();
}
}