-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
86 lines (70 loc) · 2.27 KB
/
main.cpp
File metadata and controls
86 lines (70 loc) · 2.27 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
#include <cmath>
#include <iostream>
#include <SFML/Graphics.hpp>
using namespace sf;
Vector2f speed(0.,0.);
RectangleShape rect(Vector2f(100,100));
void move(){
Vector2f pos = rect.getPosition();
rect.setPosition(pos+speed);
}
int main()
{
RenderWindow app(VideoMode(800, 600), "Balles !");
app.setFramerateLimit(60);
Clock clock;
Texture i;
i.loadFromFile("bg1.jpg");
RectangleShape bg = RectangleShape(Vector2f(1600,600));
bg.setTexture(&i);
View v;
v.reset(FloatRect(0,0,800,600));
rect.setOrigin(50,50);
rect.setPosition(100, 300);
// Boucle principale
while (app.isOpen())
{
Event event;
while (app.pollEvent(event)){
switch(event.type){
case Event::Closed : app.close(); break;
case Event::KeyPressed : {
switch(event.key.code){
case Keyboard::Left :
case Keyboard::Q : {
speed=Vector2f(-10,0);
}break;
case Keyboard::Right :
case Keyboard::D : {
speed=Vector2f(10,0);
}break;
}
}break;
case Event::KeyReleased : {
switch(event.key.code){
case Keyboard::Left :
case Keyboard::Q : {
speed=Vector2f(0,0);
}break;
case Keyboard::Right :
case Keyboard::D : {
speed=Vector2f(0,0);
}break;
}
}break;
}
}
// Remplissage de l'écran (couleur noire par défaut)
app.clear();
move();
app.draw(bg);
app.draw(rect);
Vector2f pos = rect.getPosition();
if(pos.x>v.getCenter().x) v.setCenter(rect.getPosition());
if(pos.x<v.getCenter().x-300) v.move(Vector2f(-10,0));
app.setView(v);
// Affichage de la fenêtre à l'écran
app.display();
}
return EXIT_SUCCESS;
}