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 |
#include <SFML\Graphics.hpp> using namespace sf; class Player { private: float x, y; public: float x, y, w, h, dx, dy, speed; int dir; String File; Image image; Texture texture; Sprite sprite; Player(String F, int X, int Y, float W, float H) { y = 0; speed = 0; dir = 0; File = F; w = W; h = H; image.loadFromFile("Images/" + File); texture.loadFromImage(image); sprite.setTexture(texture); x = X; y = Y; sprite.setTextureRect(IntRect(w, h, w, h)); } void update(float time) { switch (dir) { case 0: dx = speed; dy = 0; break; case 1: dx = -speed; dy = 0; break; case 2: dx = 0; dy = speed; break; case 3: dx = 0; dy = -speed; break; } x += dx*time; y += dy*time; speed = 0; sprite.setPosition(x, y); } }; |
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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
#include <SFML/Graphics.hpp> #include "Player.cpp" using namespace sf; int main() { RenderWindow window(VideoMode(1280, 720), "GAMESFML I"); Player p("player.png", 0, 0, 32, 32); float CurrentFrame = 0; Clock clock; while (window.isOpen()) { float time = clock.getElapsedTime().asMicroseconds(); clock.restart(); time = time/800; Event event; while (window.pollEvent(event)) { if (event.type == Event::Closed) window.close(); } /*----УПРАВЛЕНИЕ---*/ if ((Keyboard::isKeyPressed(Keyboard::Left) || (Keyboard::isKeyPressed(Keyboard::A)))) { p.dir = 1; p.speed = 0.1; CurrentFrame += 0.005*time; if (CurrentFrame > 3) CurrentFrame -= 3; p.sprite.setTextureRect(IntRect(32 * int(CurrentFrame), 32, 32, 32)); } if ((Keyboard::isKeyPressed(Keyboard::Right) || (Keyboard::isKeyPressed(Keyboard::D)))) { p.dir = 0; p.speed = 0.1; CurrentFrame += 0.005*time; if (CurrentFrame > 3) CurrentFrame -= 3; p.sprite.setTextureRect(IntRect(32 * int(CurrentFrame), 0, 32, 32)); } if ((Keyboard::isKeyPressed(Keyboard::Up) || (Keyboard::isKeyPressed(Keyboard::W)))) { p.dir = 3; p.speed = 0.1; CurrentFrame += 0.005*time; if (CurrentFrame > 3) CurrentFrame -= 3; p.sprite.setTextureRect(IntRect(32 * int(CurrentFrame), 64, 32, 32)); } if ((Keyboard::isKeyPressed(Keyboard::Down) || (Keyboard::isKeyPressed(Keyboard::S)))) { p.dir = 2; p.speed = 0.1; CurrentFrame += 0.005*time; if (CurrentFrame > 3) CurrentFrame -= 3; p.sprite.setTextureRect(IntRect(64 * int(CurrentFrame), 32, -32, 32)); } p.update(time); window.clear(); window.draw(p.sprite); window.display(); } return 0; } |