-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmove_ball.cpp
117 lines (83 loc) · 2.57 KB
/
move_ball.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
#include <sstream>
#include <cstdlib>
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
using namespace sf;
class TimeManager {
public:
static void update() {
deltaTime = clock.restart().asSeconds();
}
static float getDeltaTime() {
return deltaTime;
}
private:
static sf::Clock clock;
static float deltaTime;
};
// Definition der statischen Variablen
sf::Clock TimeManager::clock;
float TimeManager::deltaTime = 0.0f;
int main()
{
// Create a video mode object
VideoMode vm(1024, 768);
// Create and open a window for the game
RenderWindow window(vm, "Pong", Style::Default);
// Create a Text object called HUD
Text hud;
// Create a (Stream) Text object called stringstream
std::stringstream StreamText;
// A cool retro-style font
Font font;
font.loadFromFile("fonts/DS-DIGIT.ttf");
// Set the font to our retro-style
hud.setFont(font);
// Set Position
hud.setPosition(10,10);
hud.setCharacterSize(15);
StreamText << "Score:";
hud.setString(StreamText.str());
float myX = 10.0f;
float myY = 10.0f;
float speed = 100.0f; // Geschwindigkeit in Pixeln pro Sekunde
while (window.isOpen())
{
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();
}
// Delta-Zeit aktualisieren
TimeManager::update();
// Bewegung mit Delta-Zeit
if (Keyboard::isKeyPressed(Keyboard::Down)) {
myY += speed * TimeManager::getDeltaTime();
hud.setPosition(myX, myY);
}
if (Keyboard::isKeyPressed(Keyboard::Up)) {
myY -= speed * TimeManager::getDeltaTime();
hud.setPosition(myX, myY);
}
if (Keyboard::isKeyPressed(Keyboard::Left)) {
myX -= speed * TimeManager::getDeltaTime();
hud.setPosition(myX, myY);
}
if (Keyboard::isKeyPressed(Keyboard::Right)) {
myX += speed * TimeManager::getDeltaTime();
hud.setPosition(myX, myY);
}
window.clear();
window.draw(hud);
window.display();
}
return 0;
}
// -lsfml-graphics -lsfml-window -lsfml-system -lQt5Widgets -lQt5Core -fPIC