SFML и C++ Уроки \ Разработка игр › Форумы › SFML Graphics › Проблема со следованием камеры за персоонажем. › Ответ в теме: Проблема со следованием камеры за персоонажем.
C++
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 |
void main() { LoadTrack(); Clock clock; RenderWindow window(sf::VideoMode(640, 480), "2D Racing"); view.reset(sf::FloatRect(0, 0, 640, 480)); Player p("car.png", 25, 50, 206, 142, 1, 0, 0, 0, 0, 0, 0); p.sprite.setPosition(p.x, p.y); p.sprite.setOrigin(p.w / 3, p.h / 2); Image map_image; map_image.loadFromFile("images/map.png"); Texture map; map.loadFromImage(map_image); Sprite s_map; s_map.setTexture(map); while (window.isOpen()) { float time = clock.getElapsedTime().asMicroseconds(); clock.restart(); time = time / 800; sf::Event event; while (window.pollEvent(event)) { if (event.type == sf::Event::Closed) window.close(); } if (Keyboard::isKeyPressed(Keyboard::Up)) { p.sprite.move(cos(PI / 180 * p.ang)*0.3*time, sin(PI / 180 * p.ang)*0.3*time); } if (Keyboard::isKeyPressed(Keyboard::Down)) { p.sprite.move(cos(PI / 180 * p.ang)*-0.1*time, sin(PI / 180 * p.ang)*-0.1*time); } if (Keyboard::isKeyPressed(Keyboard::Left)) { p.ang = p.ang - 0.2; p.sprite.setRotation(p.ang); } if (Keyboard::isKeyPressed(Keyboard::Right)) { p.ang = p.ang + 0.2; p.sprite.setRotation(p.ang); } view.setCenter(p.sprite.getPosition()); window.setView(view); window.clear(Color(100,20,250)); for (int i = 0; i < WIDTH_MAP; i++) for (int j = 0; j < HEIGHT_MAP; j++) { if (trackMaxrix[i][j] == 0) s_map.setTextureRect(IntRect(0, 0, 32, 32)); if (trackMaxrix[i][j] == 1) s_map.setTextureRect(IntRect(32, 0, 32, 32)); if ((trackMaxrix[i][j] == 2)) s_map.setTextureRect(IntRect(64, 0, 32, 32)); if ((trackMaxrix[i][j] == 3)) s_map.setTextureRect(IntRect(96, 0, 96, 96)); if ((trackMaxrix[i][j] == 4)) s_map.setTextureRect(IntRect(128, 0, 128, 128)); s_map.setPosition(j * 32, i * 32); window.draw(s_map); } window.draw(p.sprite); window.display(); } } |