-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
97 lines (67 loc) · 2.09 KB
/
main.cpp
File metadata and controls
97 lines (67 loc) · 2.09 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
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp> // Sound Header
#include<string>
using namespace sf;
int main() {
int mx = 0;
int speed = 1;
////////////// Sound /////////////////////////////
// lade eine Sounddatei (darf nicht zu groß sein)
sf::SoundBuffer soundLaser;
if (!soundLaser.loadFromFile("Sound/laser.wav"))
return -1;
sf::SoundBuffer soundTreffer;
if (!soundTreffer.loadFromFile("Sound/treffer.wav"))
return -1;
// erstellt eine Objekt mit dem Sound
sf::Sound sl(soundLaser);
sf::Sound st(soundTreffer);
////////////// Musik /////////////////////////////
// Load an ogg music file
sf::Music music;
if (!music.openFromFile("Sound/raumschiff.wav"))
return -1;
music.play(); // spielt Musik ab
RenderWindow window(VideoMode(800, 200), "Laser ");
window.setFramerateLimit(60);
Font font;
font.loadFromFile("fonts/arial.ttf");
Text text("", font);
RectangleShape laser(Vector2f(40, 1));
laser.setFillColor(Color::Red);
laser.setPosition(0, 100);
Text position("", font);
position.setCharacterSize(15);
while (window.isOpen()) {
Vector2f laserPos = laser.getPosition();
position.setPosition(10, 150);
position.setString(
"laser: X " + std::to_string((int)laserPos.x) + " Y " + std::to_string((int)laserPos.y) + '\n' +
"Speed: X " + std::to_string(mx) + " Y " + std::to_string(0) + " drücke links / rechts auf der Tastatur!"); // Debug-Ausgabe
laser.move(mx, 0);
if (Keyboard::isKeyPressed(Keyboard::Left)) {
mx = --speed;
sl.play(); // spielt das Soundobjekt "Laser" ab
}
if (Keyboard::isKeyPressed(Keyboard::Right)) {
mx = ++speed;
sl.play(); // spielt das Soundobjekt "Laser" ab
}
if (laserPos.x < 650) { // Sound ist nicht optimal
st.play(); // spielt das Soundobjekt "Treffer" ab
}
if (laserPos.x >= 800) {
laser.setPosition(0, 100);
}
Event event;
while (window.pollEvent(event)) {
if (event.type == Event::Closed) window.close();
}
window.clear();
window.draw(laser);
window.draw(text);
window.draw(position);
window.display();
}
return 0;
}