include SFML Graphics hpp int main sf RenderWindow window sf VideoMode

 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
#include <SFML/Graphics.hpp>
int main() {
sf::RenderWindow window(sf::VideoMode(800, 600), "title");
sf::Font font;
font.loadFromFile("fonts/ariblk.ttf");
sf::Text text("hui", font);
sf::Texture bg;
bg.loadFromFile("images/bg.png");
sf::Texture character;
character.loadFromFile("images/character.png");
sf::RectangleShape character_rect;
character_rect.setSize(sf::Vector2f(71, 100));
character_rect.setTexture(&character);
character_rect.setPosition(100, 393);
character_rect.setOrigin(71 / 2, 0);
sf::RectangleShape bg_rect;
bg_rect.setSize(sf::Vector2f(800, 600));
bg_rect.setTexture(&bg);
int rotate = 0;
while (window.isOpen()) {
sf::Event event;
while (window.pollEvent(event)) {
switch (event.type) {
case sf::Event::Closed:
window.close();
break;
case sf::Event::KeyPressed:
if (sf::Keyboard::isKeyPressed(sf::Keyboard::A)) {
character_rect.move(sf::Vector2f(-1, 0));
if (rotate == 0) {
character_rect.scale(sf::Vector2f(-1, 1));
rotate = 1;
}
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::D)) {
character_rect.move(sf::Vector2f(1, 0));
if (rotate == 1) {
character_rect.scale(sf::Vector2f(-1, 1));
rotate = 0;
}
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Space)) {
character_rect.move(sf::Vector2f(0, -100));
character_rect.move(sf::Vector2f(0, 100));
}
break;
}
}
window.clear();
window.draw(bg_rect);
window.draw(character_rect);
window.display();
}
return 0;
}